2013-03-10 46 views
0
import java.sql.*; 
public class Login 
{ 
    public static void main(String args[]) throws SQLException 
    { 
     Connection con = null; 
     PreparedStatement stmt = null; 
     Statement st = null; 
     try { 
      Driver driver = new oracle.jdbc.driver.OracleDriver(); 
      DriverManager.registerDriver(driver); 
      System.out.println("coneecting to the database:"); 
      con = DriverManager.getConnection("driverURL","usrr","pass"); 
      System.out.println("creating statement"); 
      String sql = "Update abhi SET age = ? WHERE id = ?"; 
      stmt = con.prepareStatement(sql); 
      stmt.setInt(1, 35); 
      stmt.setInt(2,102); 
      int rows = stmt.executeUpdate(); 
      S.O.P("rows updated"+rows); 
      stmt.close(); 
      String sql2; 
      sql2 = "select * from abhi"; 
      st = con.createStatement(); 
      ResultSet rs = st.executeQuery(sql2); 
      while(rs.next()) 
      { 
       System.out.println("hi"); 
       int age = rs.getInt("age"); 
       System.out.println("age is"+age); 
      } 
      rs.close(); 
      stmt.close(); 
      con.close(); 
     } 
     catch(SQLException e) { 
      e.printStackTrace(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     finally { 
      try { 
       if (st != null) 
        st.close(); 
      } 
      catch(Exception e) { 
       // nthing to do 
      } 
      try { 
       if(con != null) 
        con.close(); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     System.out.println("good bye johny"); 
    } 
} 

这是我的代码,它的prefectly乳宁没有任何错误......但在我的数据库的NT 更新我无法明白其中的道理就任何机构帮助我 与价值观为什么它的NT更新的价值年龄其中id是102 ...如何有效地使用prepared语句?

+4

我没有看到提交。 (请尽量和格式化你的代码,这真的很难跟随你的贴吧。) – Mat 2013-03-10 10:40:28

回答

0

尝试使用

con.setAutoCommit(false); 


con.commit(); 
0

在Oracle中,

的默认数据库事务模式是两阶段提交。

即插入或更新数据,

你应该明确地提交操作,在Oracle数据库中留存数据之后。

但在这里,我没有看到你的代码中有任何提交。

可以在代码中检查此问题吗?

你应该使用 -

con.setAutoCommit(false);在代码的begining

con.commit(); to commit the data 
+0

但我的代码也显示行0更新 – ricky 2013-03-10 10:53:10

+0

coneecting到数据库: 创建声明 再见约翰尼拉 我只是得到这个O/P即使我使用上面1 ......... con.commit更新后查询 – ricky 2013-03-10 11:05:06

+0

我仍然无法理解我的代码中的问题...我已经包括CON.COMMIT()后STMT.EXECUTEuPDATE() ; – ricky 2013-03-10 11:15:57

0

正如其他人所提到的,尽量使用连接提交这样。

import java.sql.*; 
public class Login 
{ 
public static void main(String args[]) throws SQLException 
{ 
    Connection con = null; 
    PreparedStatement stmt = null; 
    Statement st = null; 
    try 
    { 
     Driver driver = new oracle.jdbc.driver.OracleDriver(); 
     DriverManager.registerDriver(driver); 
     System.out.println("coneecting to the database:"); 

    con = DriverManager.getConnection("driverURL","usrr","pass");  
    con.setAutoCommit(false); 
    System.out.println("creating statement"); 

    String sql = "Update abhi SET age = ? WHERE id = ?"; 

    stmt = con.prepareStatement(sql); 

    stmt.setInt(1, 35); 

    stmt.setInt(2,102); 

    int rows = stmt.executeUpdate(); 

    S.O.P("rows updated"+rows); 

    stmt.close(); 
    con.commit(); 
    String sql2; 

    sql2 = "select * from abhi"; 

    st = con.createStatement(); 

    ResultSet rs = st.executeQuery(sql2); 

    while(rs.next()) 
    { 
     System.out.println("hi"); 


     int age = rs.getInt("age"); 

     System.out.println("age is"+age); 
    } 

       rs.close(); 

    stmt.close(); 

    con.close(); 
} 

catch(SQLException e) 
{ 
    e.printStackTrace(); 
} 
catch (Exception e) 
     { 
    e.printStackTrace();  
} 
finally 
{ 
    try 
    { 
     if(st!=null) 
      st.close(); 
    } 

    catch(Exception e) 
    { 
       // nthing to do 

       } 
    try 
    { 
     if(con!=null) 

     con.close(); 
    } 
    catch (Exception e) 
    { 

       e.printStackTrace(); 

       } 


     } 

     System.out.println("good bye johny"); 

     } 
+0

谢谢大家,但我明白了,我在写查询时犯了一些错误 – ricky 2013-03-10 15:35:57

相关问题