Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.*;
- class DoPrepSQL
- {
- Connection con;
- PreparedStatement ps = null;
- String updateName = "UPDATE PrepStudent SET StudentName=? where ID=?";
- List<String> rightNames = new ArrayList<String>();
- List<String> IDs = new ArrayList<String>();
- public void ConnectToDB() throws SQLException, ClassNotFoundException
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- String url = "jdbc:odbc:PrepStudentDSN";
- con = DriverManager.getConnection(url, "", "");
- con.setAutoCommit(false);
- ps = con.prepareStatement(updateName);
- }
- public void PopulateNames()
- {
- rightNames.add("Vishal Agarwal");
- rightNames.add("Mohit Sachdeva");
- IDs.add("10-CS-1076");
- IDs.add("10-CS-1094");
- }
- public void UpdateDetails() throws SQLException, ClassNotFoundException
- {
- ConnectToDB();
- PopulateNames();
- for(int i = 0; i < 2; i++)
- {
- ps.setString(1, rightNames.get(i));
- ps.setString(2, IDs.get(i));
- ps.executeUpdate();
- }
- con.commit();
- if(ps != null)
- ps.close();
- }
- }
- public class PreparedStateUse
- {
- public static void main(String... args)
- {
- try
- {
- DoPrepSQL ds = new DoPrepSQL();
- ds.UpdateDetails();
- }
- catch(Exception e)
- {
- System.err.println(e.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment