在eclipse中对数据库进行批处理语句操作时,提示java.sql.SQLException: SQL String cannot be empty错误信息,具体提示如下:

java.sql.SQLException: SQL String cannot be empty
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:85)
 at com.mysql.cj.jdbc.ClientPreparedStatement.<init>(ClientPreparedStatement.java:219)
 at com.mysql.cj.jdbc.ClientPreparedStatement.<init>(ClientPreparedStatement.java:193)
 at com.mysql.cj.jdbc.ClientPreparedStatement.getInstance(ClientPreparedStatement.java:134)
 at com.mysql.cj.jdbc.ConnectionImpl.clientPrepareStatement(ConnectionImpl.java:678)
 at com.mysql.cj.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:1655)
 at com.mysql.cj.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:1575)
 at com.test.jdbc.day23.Day23Demo1.main(Day23Demo1.java:17)
Caused by: com.mysql.cj.exceptions.WrongArgumentException: SQL String cannot be empty
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
 at com.mysql.cj.AbstractPreparedQuery.checkNullOrEmptyQuery(AbstractPreparedQuery.java:163)
 at com.mysql.cj.jdbc.ClientPreparedStatement.<init>(ClientPreparedStatement.java:215)
 ... 6 more

错误提示的意思是SQL字符串不能为空,通过检查发现,在进行预编译的时候,将SQL语句赋值为空了:

   // 预编译SQL,并返回PreparedStatement对象
   PreparedStatement pst = conn.prepareStatement("");

解决方法很简单,直接加个空格就可以:

   // 预编译SQL,并返回PreparedStatement对象
   PreparedStatement pst = conn.prepareStatement(" ");

执行后表示,结果确认无误。

Logo

更多推荐