
code1: package com.dao; import java.sql.*; import com.beans.*;
public class DbAccess {
public void createStudent(SampleBean sampleBean) throws SQLException
{
Connection con=null;
try
{
con=DbConnect.createConnection();
PreparedStatement ps=con.prepareStatement("insert into dinstudent values(?,?,?,?,?)");
ps.setString(1, sampleBean.getFname());
ps.setString(2, sampleBean.getLname());
ps.setInt(3, sampleBean.getAge());
ps.setString(4, sampleBean.getGender());
ps.setString(5, sampleBean.getPhone());
ps.executeUpdate();
System.out.println("Addition Success");
}
catch(Exception e)
{
System.out.println("Error in create access");
}
}
public SampleBean viewStudent(String fname) throws SQLException
{
SampleBean samView=new SampleBean();
Connection con=null;
ResultSet rs=null;
try
{
con=DbConnect.createConnection();
PreparedStatement ps=con.prepareStatement("select * from dinstudent where fname=?");
ps.setString(1,fname);
rs=ps.executeQuery();
while(rs.next())
{
String a=rs.getString(1);
samView.setFname(a);
samView.setLname(rs.getString(2));
samView.setAge(rs.getInt(3));
samView.setGender(rs.getString(4));
samView.setPhone(rs.getString(5));
System.out.println("View Success");
}
}
catch(Exception e)
{
System.out.println("Error in student view access");
}
return samView;
}
public void updateStudent(SampleBean sample,String firstname) throws SQLException
{
Connection con=null;
try
{
con=DbConnect.createConnection();
PreparedStatement ps=con.prepareStatement("update dinstudent set fname=?,lname=?,age=?,gender=?,phone=? where fname=?");
ps.setString(1,sample.getFname());
ps.setString(2,sample.getLname());
ps.setInt(3,sample.getAge());
ps.setString(4,sample.getGender());
ps.setString(5,sample.getPhone());
ps.setString(6,firstname);
ps.executeQuery();
System.out.println("updated");
}
catch(Exception e)
{
System.out.println("Error in student update access");
}
}
public void deleteStudent(String deleteName) throws SQLException
{
Connection con=null;
try
{
con=DbConnect.createConnection();
PreparedStatement ps=con.prepareStatement("delete dinstudent where fname=?");
ps.setString(1,deleteName);
ps.executeQuery();
System.out.println("deleted");
}
catch(Exception e)
{
System.out.println("Error in access Delete");
}
}
}
code2 :
package com.dao; import java.sql.*;
public class DbConnect {
public static Connection createConnection()
{
Connection con=null;
try
{
Class.forName("oracle.jdbc.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr", "hr");
System.out.println("Connected");
}
catch(Exception e)
{
System.out.println("Not Connected");
e.printStackTrace();
}
return con;
}
}
print("code sample");