1

I'm trying to execute multiple sql commands, but it gives me "error in your SQL syntax;"

Db_Connection dbconn = new Db_Connection();
Connection myconnection = dbconn.Connection();
String sqlString = "SELECT DISTINCT std_id FROM std_crs WHERE crs_id ='222123'; "
     + "SELECT * FROM cplus_grades ;";
Statement myStatement = myconnection.createStatement();

boolean results = myStatement.execute(sqlString);
do {
     if (results) {
          ResultSet rs = myStatement.getResultSet();

          while (rs.next()) {
          }
          rs.close();
     }
     results = myStatement.getMoreResults();
} while(results);

myStatement.close();
5
  • msdn.microsoft.com/en-us/library/ms378758(v=sql.110).aspx check this link .. I'm doing the same thing I guess, right ? Commented Apr 29, 2016 at 16:27
  • 1
    +1 for showing this feature. Maybe you need to remove the last semicolon in the sql string. The feature may also not be supported by every JDBC driver. What driver and database do you use? Is it a sql server like in your link? Commented Apr 29, 2016 at 16:35
  • I'm using phpMyAdmin Commented Apr 29, 2016 at 16:38
  • complete wrong two nested sql statements.. Commented Apr 29, 2016 at 16:42
  • Technically a JDBC statement should only execute one statement at a time. Some drivers allows this, but it is non-standard. Commented Apr 30, 2016 at 12:20

2 Answers 2

3

I did a small test with three JDBC drivers:

  • MS SQL: works, returns two result sets
  • MySQL: fails with a syntax error - that is what you are seeing
  • HSQLDB: runs, but returns only one result set.

So I guess it simply depends on the JDBC driver if this technique works. Maybe it works only in MS SQL JDBC.

UPDATE:

It also works with Postgres.

Sign up to request clarification or add additional context in comments.

Comments

1

please 1. String dbUrl = "jdbc:mysql://yourDatabase?allowMultiQueries=true"; this should be your jdbc connection url

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.