开发者

Why the (1)JDBC code works while the (2) does not?

(1)

import java.sql.*;

public class jdbcDemo

{static Connection con=null;

    public static void main(String args[])
    { int id = 0;
    try
    { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con = DriverManager.getConnection("jdbc:odbc:login");

      Statement stat = con.createStatement();
      System.out.println("got connection");
      ResultSet rs = stat.executeQuery("Select max(UID) from mytable");
      while(rs.next())
      { id=rs.getInt(1);
      }
      id++;
      System.out.println(id);
      PreparedStatement ps = con.prepareStatement("INSERT into mytable(UID,Username,Password) values(?,?,?)");
      ps.setInt(1,id);
      ps.setString(2,"abhi");
      ps.setString(3,"bindra");


       ps.executeUpdate();





        }
    catch(Exception e)
    {

       e.printStackTrace();
    }



}
}

(2)

import java.sql.*;

public class jdbcDemo

{static Connection con=null;

    public static void main(String args[])
    { int id = 0;
    try
    { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con = DriverManager.getConnection("jdbc:odbc:login");

      Statement stat = con.createStatement();
      System.out.println("got connection");
      ResultSet rs = stat.executeQuery("Select max(UID) from mytable");
      while(rs.next())
      { id=rs.getInt(1);
      }
      id++;
      System.out.println(id);
      PreparedStatement ps = con.prepareStatement("INSERT into mytable(UID,Username,Password) values(?,?,?)");
      ps.setInt(1,id);
      ps.setString(2,"abhi");
      ps.setString(3,"bindra");


       ps.executeUpdate();
       String s1,s2;
       ResultSet rs1 = stat.executeQuery("Select  * from mytable where UID="+id+"");
        while(rs1.next())
        {
            s1=rs1.getString(2);
            s2=rs1.getString(3);
            System.out.print(s1+s2);
        }





        }
    catch(Exception e)
    {

       e.printStackTrace();
    }



}
}

The part

ResultSet rs1 = stat.executeQuery("Select  * 开发者_如何学JAVAfrom mytable where UID="+id+"");
    while(rs1.next())
    {
        s1=rs1.getString(2);
        s2=rs1.getString(3);
        System.out.print(s1+s2);
    }

why is the part so essential? why doesnt ps.executeUpdate() inserts the row into the table.?The record does not appear in the table without the part being inserted in (1).


Do you ever CLOSE your statements, resultsets and connections? They should be, in a finally clause.


The automatic commit doesn't occur until you have retrieved the result set, even on an INSERT, UPDATE or DELETE statement. See: http://download.oracle.com/javase/tutorial/jdbc/basics/transactions.html

the default is for a SQL statement to be committed when it is completed, not when it is executed. A statement is completed when all of its result sets and update counts have been retrieved.

Try sending a commit to the database via con.commit() right after you execute it if you are not interested in the update result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜