Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. package exercises;
  2. import java.sql.*;
  3. public class Day26Exercise1 {
  4.  
  5. public static void main(String[] args) throws SQLException {
  6. PreparedStatement pstmt1=null;
  7. Connection con = null;
  8. long currentTime=System.currentTimeMillis();
  9. Date d=new Date(currentTime);
  10. int exec1=0, exec2=0;
  11.  
  12. String url="jdbc:oracle:thin:@localhost:1521:xe";
  13. String user = "system";
  14. String pass = "admin";
  15. //The method below works on local server
  16. try{
  17. Class.forName("oracle.jdbc.driver.OracleDriver");
  18. con = DriverManager.getConnection(url, user, pass);
  19. if(con != null){
  20. System.out.println("You are successfully connected");
  21.  
  22. }
  23. else
  24. System.out.println("Something went wrong");
  25.  
  26. con.setAutoCommit(false);
  27. currentTime=currentTime+86400000;
  28. d=new Date(currentTime);
  29. System.out.println();
  30. pstmt1=con.prepareStatement("insert into bugs values(?,?,?)");
  31. pstmt1.setString(1,"Robert");
  32. pstmt1.setDate(2,d);
  33. pstmt1.setInt(3,5);
  34. exec1=pstmt1.executeUpdate();
  35.  
  36. System.out.println("Statement one complete and result is "+exec1);
  37. String myQuery = "update ALL_BUGS set TOT_BUGS = TOT_BUGS + ? where name=?";
  38. pstmt1=con.prepareStatement(myQuery);
  39. pstmt1.setInt(1, 5);
  40. pstmt1.setString(2, "Robert");
  41. System.out.println("Statement set");
  42. exec2=pstmt1.executeUpdate();
  43. pstmt1.close();
  44. System.out.println("Statement two complete");
  45.  
  46. System.out.println("Exec1 is successful? "+exec1);
  47. System.out.println("Exec2 is successful? "+exec2);
  48. }
  49. catch(Exception e){
  50. System.out.println("Something happened");
  51. System.out.println(e.getMessage());
  52. System.out.println(e.getCause());
  53. }
  54.  
  55. if (exec1 >0 && exec2 >0){
  56. con.commit();
  57. // System.out.println("Rec updated in table 1 "+exec1);
  58. // System.out.println("Rec updated in table 2 "+exec2);
  59. System.out.println("Commited successfully");
  60. }
  61. else
  62. {
  63. con.rollback();
  64. System.out.println("value of exec1 and exec2 are "+exec1+" "+exec2);
  65. System.out.println("Some issues...so rolledback");
  66. }
  67. }
  68. //con.close();
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement