gpt4 book ai didi

java - 从 java 执行过程时出现 JDBC 连接错误

转载 作者:行者123 更新时间:2023-11-29 11:42:10 25 4
gpt4 key购买 nike

我必须通过传递值从 java 类执行存储过程。但是当我尝试这样做时,我收到错误

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:995)
at com.coolminds.action.OperatorBillCorrection.main(OperatorBillCorrection.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

这是我的代码。

public class OperatorBillCorrection {

public static void main(String args[]) throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/webill_operator_2016","root","root123");
String opCode="KL04C019" ;

for(int i=0;i<=12;i++) {
String dt= "2015-"+i+"-1" ;
PreparedStatement stmts=con.prepareStatement("call sp_getOperatorCollection(?,?)") ;
stmts.setString(1,opCode);
stmts.setString(2,dt);
stmts.execute();

}
for(int i=0;i<=2;i++) {
String dt= "2016-"+i+"-1" ;
PreparedStatement stmts=con.prepareStatement("call sp_getOperatorCollection(?,?)") ;
stmts.setString(1,opCode);
stmts.setString(2,dt);
stmts.execute();
}

System.out.println("Completed") ;
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

当我使用Statement时它正在工作stmt=con.createStatement(); stmt.execute("call sp_getOperatorCollection('KL04C019','2015-09-01')");而不是PreparedStatement

最佳答案

使用 java.sql.Date 而不是 java.util.date。

java.util.Date date = formatter.parse(dateString);

java.sql.Date dt = new java.sql.Date(date.getTime());

关于java - 从 java 执行过程时出现 JDBC 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35597043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com