Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercises;
- import java.sql.*;
- public class Day26Exercise1 {
- public static void main(String[] args) throws SQLException {
- PreparedStatement pstmt1=null;
- Connection con = null;
- long currentTime=System.currentTimeMillis();
- Date d=new Date(currentTime);
- int exec1=0, exec2=0;
- String url="jdbc:oracle:thin:@localhost:1521:xe";
- String user = "system";
- String pass = "admin";
- //The method below works on local server
- try{
- Class.forName("oracle.jdbc.driver.OracleDriver");
- con = DriverManager.getConnection(url, user, pass);
- if(con != null){
- System.out.println("You are successfully connected");
- }
- else
- System.out.println("Something went wrong");
- con.setAutoCommit(false);
- currentTime=currentTime+86400000;
- d=new Date(currentTime);
- System.out.println();
- pstmt1=con.prepareStatement("insert into bugs values(?,?,?)");
- pstmt1.setString(1,"Robert");
- pstmt1.setDate(2,d);
- pstmt1.setInt(3,5);
- exec1=pstmt1.executeUpdate();
- System.out.println("Statement one complete and result is "+exec1);
- String myQuery = "update ALL_BUGS set TOT_BUGS = TOT_BUGS + ? where name=?";
- pstmt1=con.prepareStatement(myQuery);
- pstmt1.setInt(1, 5);
- pstmt1.setString(2, "Robert");
- System.out.println("Statement set");
- exec2=pstmt1.executeUpdate();
- pstmt1.close();
- System.out.println("Statement two complete");
- System.out.println("Exec1 is successful? "+exec1);
- System.out.println("Exec2 is successful? "+exec2);
- }
- catch(Exception e){
- System.out.println("Something happened");
- System.out.println(e.getMessage());
- System.out.println(e.getCause());
- }
- if (exec1 >0 && exec2 >0){
- con.commit();
- // System.out.println("Rec updated in table 1 "+exec1);
- // System.out.println("Rec updated in table 2 "+exec2);
- System.out.println("Commited successfully");
- }
- else
- {
- con.rollback();
- System.out.println("value of exec1 and exec2 are "+exec1+" "+exec2);
- System.out.println("Some issues...so rolledback");
- }
- }
- //con.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement