Recently, I have encountered the following problems in designing a Web course.
Java.sql.SQLException:
Java.lang.RuntimeException:java.sql.SQLException:Can not issue executeupdate () for selects
At Com.infuze.service.subscription.workflow.SyncSubscriptionTrackerWorkflow.executeProcess ( syncsubscriptiontrackerworkflow.java:130)
At Com.infuze.service.workflow.WorkflowExecutor.execute (workflowexecutor.java:24)
At Com.infuze.service.subscription.xml.SubscriptionXmlService.syncTracker (subscriptionxmlservice.java:140)
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
At caused By:java.sql.SQLException:Can no issue executeupdate () for selects
At Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:1073)
At Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:987)
At Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:982)
At Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:927)
At Com.mysql.jdbc.PreparedStatement.executeUpdate (preparedstatement.java:2373)
I do not know a lot of novice like me have met the same problem.
I'm not sure what's going on with that question. The code sample for the problem anyway is as follows:
Example Code
Public subscriptiondto getsubscription (String subscriptionguid) throws SQLException {
Connection conn = null;
PreparedStatement PS = null;
ResultSet rs = null;
Exception ex = null;
Subscriptiondto subdto = null;
try {
Log.debug ("Subscription GUID" + Subscriptionguid);
conn = Datasource.getconnection ();
PS = conn
. Preparestatement ("Select id,created,updated,access_token,token_secret,status,guid,service_id from Subscriptions Subs where Subs.guid =? ");
Ps.setstring (1, subscriptionguid);
ResultSet rst = Ps.executequery ();
while (Rst.next ()) {
Subdto = new Subscriptiondto ();
Subdto.setid (Rst.getlong ("id"));
Subdto.setcreated (Rst.getdate ("created"));
Subdto.setupdated (Rst.getdate ("updated"));
Subdto.setaccesstoken (rst.getstring ("Access_token"));
Subdto.setaccesstokensecret (rst.getstring ("Token_secret"));
Subdto.setstatus (rst.getstring ("status"));
Subdto.setguid (rst.getstring ("GUID"));
Subdto.setserviceid (Rst.getint ("service_id"));
}
Ps.executeupdate (); Problem are here
Log.debug ("tracker updated successfully");
} catch (SQLException e) {
ex = e;
Log.error ("SQL error occured while updating subscription tracker" + Subscriptionguid, E);
} catch (Exception e) {
ex = e;
Log.error ("Error occured while updating subscription Trakcer" + Subscriptionguid, E);
} finally {
CLOSEDBOBJECTS (Conn, PS, RS);
Checkexception (ex);
}
return subdto;
}
Note that the above black part, the problem appears here.
Solution: Use Ps.execute (); Replace Ps.executeupdate. As for the reason,
Look at the difference between them:
The Statement interface provides three ways to execute SQL statements: ExecuteQuery, Executeupdate, and execute. Which method to use is determined by the content produced by the SQL statement.
Method ExecuteQuery
The statement used to produce a single result set, such as a SELECT statement. The most used method of executing SQL statements is executeQuery. This method is used to execute the SELECT statement, which is almost the most frequently used SQL statement.
Method executeupdate
Used to execute INSERT, UPDATE, or DELETE statements, as well as SQL DDL (data definition language) statements, such as CREATE table and DROP table. The effect of an INSERT, UPDATE, or DELETE statement is to modify one or more columns in 0 or more rows in a table. The return value of Executeupdate is an integer that indicates the number of rows affected (that is, the update count). For statements that do not manipulate rows such as CREATE table or DROP table, the return value of executeupdate is always zero.
The Executeupdate method is used because the SQL statement in Createtablecoffees is a DDL (data definition Language) statement. Creating tables, altering tables, and deleting tables are examples of DDL statements that are executed using the Executeupdate method. You can also see from its name that method Executeupdate is also used to execute an UPDATE table SQL statement. In fact, Executeupdate is more time-dependent for updating tables than creating tables, because tables need to be created only once, but are often updated.
Method Execute:
Used to perform statements that return multiple result sets, multiple update counts, or a combination of both. Because most programmers don't need this advanced functionality
The Execute method should only be used when the statement can return multiple ResultSet objects, multiple update counts, or a combination of ResultSet objects and update counts. When a stored procedure is executed or an unknown SQL string is executed dynamically (that is, the application programmer is not known at compile time), there is a chance that multiple results may occur, although this is rare.
Because the method execute handles unconventional situations, it is not surprising to get its results with some special handling. For example, assuming that a procedure is known to return two result sets, you must call the method Getresultset to get the first result set after you execute the procedure using method execute, and then call the appropriate GetXXX method to get the values in it. To get the second result set, you need to call the Getmoreresults method before calling the Getresultset method. If a procedure is known to return two update counts, the method Getupdatecount is called first, then Getmoreresults is called, and Getupdatecount is called again.
The situation is more complex for those who do not know what to return. If the result is a ResultSet object, the method execute returns TRUE, or False if the result is a Java int. If an int is returned, it means that the result is an update count or the statement executed is a DDL command. The first thing to do after calling the method execute is to call Getresultset or Getupdatecount. Call method Getresultset to get the first object in two or more ResultSet objects, or call the method Getupdatecount to get the contents of the first update count in two or more update counts.
When the result of the SQL statement is not a result set, the method getresultset returns NULL. This may mean that the result is an update count or no other result. In this case, the only way to judge the true meaning of NULL is to call the method Getupdatecount, which returns an integer. This integer is the number of rows affected by the invocation statement, and if 1 indicates that the result is a result set or no result. If the method Getresultset has returned null (indicating that the result is not a ResultSet object), the return value of 1 means that there is no other result. That is, when the following conditions are true, there is no result (or no other result):
((stmt.getresultset () = = null) && (stmt.getupdatecount () = =-1))
If the method Getresultset has been called and the ResultSet object it returns is processed, it is necessary to call the method getmoreresults to determine whether there are additional result sets or update counts. If Getmoreresults returns True, you will need to call Getresultset again to retrieve the next result set. As mentioned above, if Getresultset returns NULL, you need to call Getupdatecount to check whether NULL indicates whether the result is an update count or no other result.
When Getmoreresults returns FALSE, it indicates that the SQL statement returned an update count or no other result. Therefore, you need to call method Getupdatecount to check which case it is. In this case, there is no other result when the following conditions are true:
((stmt.getmoreresults () = = False) && (stmt.getupdatecount () = =-1))
The following code demonstrates a method used to confirm all result sets and update counts resulting from access to the calling method execute:
Stmt.execute (Querystringwithunknownresults);
while (true) {
int rowCount = Stmt.getupdatecount ();
if (RowCount > 0) {//It is update count
System.out.println ("Rows changed =" + count);
Stmt.getmoreresults ();
Continue
}
if (RowCount = = 0) {//DDL command or 0 updates
System.out.println ("No rows changed or statement was DDL
Command ");
Stmt.getmoreresults ();
Continue
}
Executed here, proving to have a result set
or no other result
ResultSet rs = stmt.getresultset;
if (rs! = null) {
...//Use metadata to get information about a result set column
while (Rs.next ()) {
...//Processing results
Stmt.getmoreresults ();
Continue
}
Break No other results
IDEA create error when running Web project: Can not issue executeupdate () for selects solution