Jdbc Connection:
Step 1: Loading the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Step 2: Preparing the Connection String
String cs = "jdbc:odbc:dsnName";
Step 3: Request for the connection
Connection con = DriverManager.getConnection(cs,"username","password");
Step 4: Perform the dml operations
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from Employee");
Step 4: Storing the Results in Result Set
while(rs.next())
{ rs.getInt(1); rs.getString(2);rs.getDouble(3);}
Step 5: Close the all open connections.
rs.close(); st.close(); con.close();
PreparedStatement in JDBC:
Step 1 & 2 & 3 are same
Step 4: PreparedStatement is used to store the data dynamically. We can execute the SQL statements multiple times using paramaterized statements.
String insertps = con.prepareStatement("insert into Employee values(?,?,?)");
String updateps = con.prepareStatement("update Employee set name = ?, address = ?, salary =?");
String deleteps = con.prepareStatement("delete Employee where salary =?");
String selectps = con.prepareStatement("select * from Employee where EmpNo = ?");
Step 5: For all above statements set the parameters using setXXX and send
insertps.setString(1,EmpNo);
insertps.setString(2,Address);
insertps.setDouble(3,Salary);
Step 6: Like above we can delete and update all the PreparedStaments.
Step 7: Finally Close the connections.
Step 1: Loading the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Step 2: Preparing the Connection String
String cs = "jdbc:odbc:dsnName";
Step 3: Request for the connection
Connection con = DriverManager.getConnection(cs,"username","password");
Step 4: Perform the dml operations
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from Employee");
Step 4: Storing the Results in Result Set
while(rs.next())
{ rs.getInt(1); rs.getString(2);rs.getDouble(3);}
Step 5: Close the all open connections.
rs.close(); st.close(); con.close();
PreparedStatement in JDBC:
Step 1 & 2 & 3 are same
Step 4: PreparedStatement is used to store the data dynamically. We can execute the SQL statements multiple times using paramaterized statements.
String insertps = con.prepareStatement("insert into Employee values(?,?,?)");
String updateps = con.prepareStatement("update Employee set name = ?, address = ?, salary =?");
String deleteps = con.prepareStatement("delete Employee where salary =?");
String selectps = con.prepareStatement("select * from Employee where EmpNo = ?");
Step 5: For all above statements set the parameters using setXXX and send
insertps.setString(1,EmpNo);
insertps.setString(2,Address);
insertps.setDouble(3,Salary);
Step 6: Like above we can delete and update all the PreparedStaments.
Step 7: Finally Close the connections.