我刚开始联系JDBC,遇到了有关CallableStatement的问题,这是我的代码:
Connection connection=null;
CallableStatement statement=null;
try {
connection = DriverManager.getConnection(DBTools.getDbUrl(), DBTools.getUSER(),DBTools.getPASSWD());
String getNameSQL="{call getName (?,?)}";
statement=connection.prepareCall(getNameSQL);
int empID=103;
String empName;
statement.setInt(1,empID);
statement.registerOutParameter(2, Types.VARCHAR);
System.out.println("Execute: "+statement.execute());
empName=statement.getString(2);
System.out.println("Emp Name with ID:" + empID + " is " + empName);
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
其控制台输出为:
java.sql.SQLException: The user specified as a definer ('mysql.infoschema'@'localhost') does not exist
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.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:970)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1020)
at com.mysql.cj.jdbc.DatabaseMetaDataUsingInfoSchema.executeMetadataQuery(DatabaseMetaDataUsingInfoSchema.java:70)
at com.mysql.cj.jdbc.DatabaseMetaDataUsingInfoSchema.getProcedureColumns(DatabaseMetaDataUsingInfoSchema.java:769)
at com.mysql.cj.jdbc.CallableStatement.determineParameterTypes(CallableStatement.java:790)
at com.mysql.cj.jdbc.CallableStatement.<init>(CallableStatement.java:585)
at com.mysql.cj.jdbc.CallableStatement.getInstance(CallableStatement.java:484)
at com.mysql.cj.jdbc.ConnectionImpl.parseCallableStatement(ConnectionImpl.java:1523)
at com.mysql.cj.jdbc.ConnectionImpl.prepareCall(ConnectionImpl.java:1548)
at com.mysql.cj.jdbc.ConnectionImpl.prepareCall(ConnectionImpl.java:1539)
at main.JDBCCallableStatement.main(JDBCCallableStatement.java:13)
第13行是:statement = connection.prepareCall(getNameSQL);
我确定mysql函数getName()已创建并且运行良好,看来问题出在Connection上,但我不知道如何解决它,希望您能给我一些建议。