Advertisement
Guest User

Untitled

a guest
May 29th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.PreparedStatement;
  7.  
  8. /*
  9. NOTES
  10. -JDBC API is available in two packages, java.sql and javax.sql
  11. -If no. of rows of resultset isn't known, use ResultSet metadata to keep track of ResultSet itself. like rows, tables, columns and all.
  12. -PreparedStatement is a child class of Statement, which reduces query compilation time because
  13. it works on pre-compiled queries, unlike Statement
  14.  
  15. */
  16.  
  17. class JDBCTest {
  18.  
  19. public static void main(String[] args) throws Exception {
  20. Class.forName("oracle.jdbc.driver.OracleDriver");
  21. String connString= "jdbc:oracle:thin:@localhost:1521:xe";
  22. Connection connection= null;
  23. Statement statement= null;
  24. ResultSet rs= null;
  25. ResultSetMetaData data= null;
  26. PreparedStatement pstatement= null;
  27.  
  28. try{
  29. connection = DriverManager.getConnection(connString, "system", "manager");
  30.  
  31. ///*
  32. pstatement= connection.prepareStatement("select * from employee where empid= ?");
  33. pstatement.setInt(1, 124);
  34. rs= pstatement.executeQuery();
  35. if(rs.next()){
  36. System.out.println(rs.getString(1)+" "+ rs.getString(3));
  37. }
  38. //*/
  39. // to set autocommit off
  40. //connection.setAutoCommit(false);
  41. //update statement
  42. /*pstatement= connection.prepareStatement("update employee set salary=? where empid> ?");
  43. pstatement.setInt(1, 900);
  44. pstatement.setInt(2, 103);
  45. int rows= pstatement.executeUpdate();
  46. connection.commit();
  47. pstatement.setInt(1, 0);
  48. pstatement.setInt(2, 120);
  49. connection.rollback(); // previous DML wont save
  50. System.out.println(rows+ " rows updated");
  51. */
  52.  
  53.  
  54.  
  55. // insert into table using preparedStatement
  56. /*
  57. pstatement= connection.prepareStatement("insert into employee values(?, ?, ?)");
  58. pstatement.setInt(1, 104);
  59. pstatement.setString(2, "Voila");
  60. pstatement.setDouble(3, 18000);
  61. int rows= pstatement.executeUpdate();
  62.  
  63. pstatement.setInt(1, 105);
  64. pstatement.setString(2, "Voodo");
  65. pstatement.setDouble(3, 18500);
  66. rows+= pstatement.executeUpdate();
  67. System.out.println(rows+" rows inserted");
  68. */
  69.  
  70. // Display all records, and using metadata usage
  71. /*statement = connection.createStatement();
  72. rs = statement.executeQuery("select * from employee");
  73. data= rs.getMetaData(); //now unknown number of columns can be identified using this.
  74. int columnCount= data.getColumnCount();
  75.  
  76. while(rs.next()) {
  77. //System.out.println(rs.getInt("empid")+ "\t"+ rs.getString(2)+ "\t"+ rs.getString(3));
  78. for(int i=1; i<=columnCount; i++) {
  79. System.out.print(rs.getString(i)+"\t");
  80. }
  81. System.out.println();
  82. }
  83. */
  84.  
  85. } catch(Exception e) {
  86. System.out.println("Oops");
  87. } finally {
  88. try{
  89. if(rs!=null)
  90. rs.close();
  91. if(statement!=null)
  92. statement.close();
  93. if(pstatement!=null)
  94. pstatement.close();
  95. if(connection!=null)
  96. connection.close();
  97. } catch( Exception e) {
  98. System.out.println("Oops");
  99. }
  100. }
  101.  
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement