WEEK-10
1.Write a Java program to connect to database and display Employee details
(eid,ename,dept and salary).
package week6;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class OracleJdbcExample {
static Statement stmtObj;
static Connection connObj;
static ResultSet resultSetObj;
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connObj =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
if(connObj!=null) {
System.out.println("!! Connected with oracle
database!!n");
}
stmtObj = connObj.createStatement();
resultSetObj = stmtObj.executeQuery("SELECT * FROM
employee");
while(resultSetObj.next()) {
System.out.println(resultSetObj.getInt(1)+"."+resultSetObj.getString(2)+"."+resultSet
Obj.getFloat(3)+"$");
}
}
catch(Exception sqlException) {
sqlException.printStackTrace();
Name:R.Harini ROLL NO:23251A1292 Page No:
}
finally {
try {
if(resultSetObj!=null) {
resultSetObj.close();
}
if(stmtObj != null){
stmtObj.close();
}
if(connObj!=null) {
connObj.close();
}
}
catch(Exception sqlException) {
sqlException.printStackTrace();
}
}
}
}
Output:
!! Connected with oracle database!!n
1291 vishwa it 2000$
1292 harini it 3000$
1294 parmita it4000$
2.Write a Java Program to insert the data in to Empoyee table
package week10;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class insertDemo {
Name:R.Harini ROLL NO:23251A1292 Page No:
public static void main(String[] args) {
Connection con;
PreparedStatement pstmt;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system",
"manager");
if(con!=null) {
System.out.println("!! connected with oracle database!!\n");
pstmt=con.prepareStatement("insert into employee
values(?,?,?,?)");
pstmt.setInt(1,123);
pstmt.setString(2,"goveri");
pstmt.setString(3, "cse");
pstmt.setFloat(4, 2000);
int res=pstmt.executeUpdate();
if(res==1)
System.out.println("Inserted successfully");
con.commit();
}
}catch(Exception sqlException) {
sqlException.printStackTrace();
}
}
}
Output:
!! Connected with oracle database!!n
Inserted successfully
Name:R.Harini ROLL NO:23251A1292 Page No:
3.Write a Java Program to Delete the details of specified employee
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class deleteDemo {
public static void main(String[] args) {
Connection con;
PreparedStatement pstmt;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
if(con!=null) {
System.out.println("!! connected with oracle database!!\n");
pstmt=con.prepareStatement("delete from employee where empno=?");
pstmt.setInt(1,1291);
int res=pstmt.executeUpdate();
if(res==1)
System.out.println("Deleteed
successfully");
con.commit();
}
}catch(Exception sqlException) {
sqlException.printStackTrace();
}
}
}
Output:
!! Connected with oracle database!!n
Deleteed successfully");
Name:R.Harini ROLL NO:23251A1292 Page No:
4.write a java program to find the employee name using procedues
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;
public class procdures {
public static void main(String[] args) {
Connection con;
CallableStatement pstmt;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jbdc:oracle:thin:@localhost:1521:xe","system","manager");
if(con != null) {
System.out.println("!! Connected With Oracle Database !!\n");
}
CallableStatement cstmt =con.prepareCall("{call findname(?,?) }");
cstmt.setInt(1, 1292);
cstmt.registerOutParameter(2, Types.VARCHAR);
cstmt.execute();
System.out.println("Employee Name " +cstmt.getString(2));
} catch(Exception sqlException) {
sqlException.printStackTrace();
}
}
}
Output:
Employee Name :Harini
Name:R.Harini ROLL NO:23251A1292 Page No:
5.Write a java program to find minimum and maximum salary of a employee using
procedure
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;
public class minmax {
public static void main(String[] args) {
Connection con;
CallableStatement pstmt;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jbdc:oracle:thin:@localhost:1521:xe","system","manager");
if(con != null) {
System.out.println("!! Connected With Oracle Database !!\n");
}
CallableStatement cstmt =con.prepareCall("{call min_max(?,?) }");
cstmt.registerOutParameter(1, Types.NUMERIC);
cstmt.registerOutParameter(2, Types.NUMERIC);
cstmt.execute();
System.out.println("Employee salary " +cstmt.getInt(1)+" "+cstmt.getInt(2));
} catch(Exception sqlException) {
sqlException.printStackTrace();
}
}
}
Output:
Employee salary 8000 9000
Name:R.Harini ROLL NO:23251A1292 Page No: