Home>

Difference between execute, executeupdate, executequery (and return value)

First, boolean execute (string sql)

Allows execution of query statements, update statements, and ddl statements.

When the return value is true, it means that the query is executed.You can get the results through the getresultset method;When the return value is false, the update statement or ddl statement is executed, and the getupdatecount method obtains the number of updated records.

example:

public static void main (string [] args) {
 connection conn=null;
 statement stm=null;
 resultset rs=null;
 try {
  class.forname ("com.microsoft.sqlserver.jdbc.sqlserverdriver");
  conn=drivermanager.getconnection ("jdbc:sqlserver://localhost:1433;databasename=test;user=sa;password=sasa");
  stm=conn.createstatement ();
  boolean ret=stm.execute ("select * from stuinfo");
  if (ret) {
  rs=stm.getresultset ();
  while (rs.next ()) {
   system.out.println ("Name:" + rs.getstring ("stuname") + "\ tAge:" + rs.getstring ("stuscore"));
  }
  }
  ret=stm.execute ("update stuinfo set stuscore=62 where stuname ="Zhang San "");
  int count=stm.getupdatecount ();
  if (! ret) {
  system.out.println (count + "Successfully modified data!");
  }
 } catch (classnotfoundexception e) {
  e.printstacktrace ();
 } catch (sqlexception e) {
  e.printstacktrace ();
 }
 }

Second, int executeupdate (string sql)

Executes a given sql statement, which may be an insert, update, or delete statement, or a sql statement that returns nothing (such as a sql ddl statement).

The return value is the number of records updated

Third, resultset executequery (string sql)

Executes the given sql statement, which returns a single resultset object.

execute is a combination of executeupdate and executequery

  • Previous JS to achieve image zoom and drag special code sharing
  • Next JS production with mask pop-up layer to achieve login registration form special effects code sharing