0% found this document useful (0 votes)
17 views4 pages

P.Teja_84 Java9th program

The document contains three Java programs demonstrating JDBC database operations. The first program retrieves employee details from an Oracle database, the second inserts a new book record into a MySQL database, and the third deletes records from a MySQL employees table. Each program includes connection setup, execution of SQL commands, and output of results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

P.Teja_84 Java9th program

The document contains three Java programs demonstrating JDBC database operations. The first program retrieves employee details from an Oracle database, the second inserts a new book record into a MySQL database, and the third deletes records from a MySQL employees table. Each program includes connection setup, execution of SQL commands, and output of results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1)Write a java program that connects to a database using JDBC

import sql package to use it in our program


import java.sql.*;

public class Sample_JDBC_Program {

public static void main(String[] args) throws ClassNotFoundException, SQLException {


// store the SQL statement in a string
String QUERY = "select * from employee_details";
//register the oracle driver with DriverManager
Class.forName("oracle.jdbc.driver.OracleDriver");
//Here we have used Java 8 so opening the connection in try statement
try(Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:system/pass123@localhost:1521:XE"))
{
Statement statemnt1 = conn.createStatement();
//Created statement and execute it
ResultSet rs1 = statemnt1.executeQuery(QUERY);
{
//Get the values of the record using while loop
while(rs1.next())
{
int empNum = rs1.getInt("empNum");
String lastName = rs1.getString("lastName");
String firstName = rs1.getString("firstName");
String email = rs1.getString("email");
String deptNum = rs1.getString("deptNum");
String salary = rs1.getString("salary");
//store the values which are retrieved using ResultSet and print it
System.out.println(empNum + "," +lastName+ "," +firstName+ "," +email
+","+deptNum +"," +salary);
}
}
}
catch (SQLException e) {
//If exception occurs catch it and exit the program
e.printStackTrace();
}
}
}
Output : 1001,luther,Martin,ml@gmail.com,1,13000
1002,Murray,Keith,km@gmail.com,2,25000
1003,Brason,John,jb@gmail.com,3,15000
1004,Martin,Richard,rm@gmail.com,4,1600
1005,Hickman,David,dh@gmail.com,5,1700
B)Write a java program to connect to database using JDBC and insert values into it

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class InsertedIdExample {

public static void main(String[] args) {


try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");

// Create a PreparedStatement with the SQL statement and


RETURN_GENERATED_KEYS option
String sql = "INSERT INTO book (author, name, price) VALUES (?, ?, ?)";
PreparedStatement stmt = con.prepareStatement(sql,
PreparedStatement.RETURN_GENERATED_KEYS);

// Set values for the parameters


stmt.setString(1, "Gupta");
stmt.setString(2, "My Book");
stmt.setDouble(3, 29.99);

// Execute the insert operation


int rowsAffected = stmt.executeUpdate();

if (rowsAffected > 0) {
// Retrieve the auto-generated keys (insert ID)
ResultSet generatedKeys = stmt.getGeneratedKeys();
if (generatedKeys.next()) {
int insertId = generatedKeys.getInt(1);
System.out.println("Record inserted successfully with ID: " + insertId);
} else {
System.out.println("Failed to retrieve insert ID.");
}
} else {
System.out.println("No records inserted.");
}

con.close();
} catch (ClassNotFoundException | SQLException e)
{
e.printStackTrace();
}
}
}
Output :
Id author name price
7739 Gupta Mybook 29.99

C)Write a java program to connect to database using JDBC and delete values from it

import java.sql.*;

// This class demonstrates DELETE command with LIMIT


public class DeleteWithLimit {

static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT";


static final String USER = "root";
static final String PASS = "guest123";

public static void main(String args[]) {


try{
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sel_qry = "select * from employees ";
String del_qry = "DELETE FROM employees ORDER BY age LIMIT 3 ";
Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(sel_qry);
System.out.println(" Displaying records before deletion ");
System.out.println(" ----------------------------------" );
showResults(rs);

stmt.executeUpdate(del_qry);
System.out.println("Displaying records after deletion..");
System.out.println(" ----------------------------------" );
rs = stmt.executeQuery(sel_qry);
showResults(rs);
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}

public static void showResults(ResultSet res) {


try{
while(res.next()){
System.out.print("ID: " + res.getInt(1));
System.out.print(", AGE: " + res.getInt(2));
System.out.print(", FirstName: " + res.getString(3));
System.out.println(", LastName: " + res.getString(4));
}
System.out.println(" ----------------------------------" );
}catch(SQLException sqle){
sqle.printStackTrace();
}
}
}
Output :Displaying records before deletion
----------------------------------
ID: 1, AGE: 50, FirstName: Shahbaz, LastName: Ali
ID: 2, AGE: 25, FirstName: Mahnaz, LastName: Fatma
ID: 3, AGE: 20, FirstName: Zaid, LastName: Khan
ID: 4, AGE: 28, FirstName: Sumit, LastName: Mittal
ID: 7, AGE: 20, FirstName: Rita, LastName: Tez
ID: 8, AGE: 20, FirstName: Sita, LastName: Singh
ID: 21, AGE: 35, FirstName: Jeevan, LastName: Rao
ID: 22, AGE: 40, FirstName: Aditya, LastName: Chaube
ID: 25, AGE: 35, FirstName: Jeevan, LastName: Rao
ID: 26, AGE: 35, FirstName: Aditya, LastName: Chaube
ID: 34, AGE: 45, FirstName: Ahmed, LastName: Ali
ID: 35, AGE: 51, FirstName: Raksha, LastName: Agarwal
----------------------------------
Displaying records after deletion..
----------------------------------
ID: 1, AGE: 50, FirstName: Shahbaz, LastName: Ali
ID: 2, AGE: 25, FirstName: Mahnaz, LastName: Fatma
ID: 4, AGE: 28, FirstName: Sumit, LastName: Mittal
ID: 21, AGE: 35, FirstName: Jeevan, LastName: Rao
ID: 22, AGE: 40, FirstName: Aditya, LastName: Chaube
ID: 25, AGE: 35, FirstName: Jeevan, LastName: Rao
ID: 26, AGE: 35, FirstName: Aditya, LastName: Chaube
ID: 34, AGE: 45, FirstName: Ahmed, LastName: Ali
ID: 35, AGE: 51, FirstName: Raksha, LastName: Agarwal
—-----------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy