我有这段代码:
final String adatbazisURL = "jdbc:mysql://localhost:3306";
Connection kapcsolat = null;
Statement utasitas = null;
try {
Class.forName("com.mysql.jdbc.Driver");
kapcsolat = DriverManager.getConnection(adatbazisURL, felhNev,
jelszo);
utasitas = kapcsolat.createStatement();
String sql = "USE"+Azonositok.getAdatbazisNev()+";";
utasitas.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
String sql = "DROP PROCEDURE IF EXISTS proba ;";
utasitas.executeUpdate(sql);
sql = "CREATE PROCEDURE proba (" + "IN number1 INTEGER, "
+ "OUT number2 INTEGER) " + "BEGIN " +
"SET number2 = number1;" + "END";
utasitas.execute(sql);
CallableStatement callableStatement = null;
String callString = "{CALL proba(?,?)}";
callableStatement = kapcsolat.prepareCall(callString);
callableStatement.setInt(1, 5);
callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);
callableStatement.execute();
int number = callableStatement.getInt(2);
callableStatement.close();
当我运行此代码时,它会抛出“参数号2不是OUT参数”异常。为什么呢?
答案 0 :(得分:1)
问题在于:
String sql = "USE"+Azonositok.getAdatbazisNev()+";";
utasitas.executeUpdate(sql);
似乎没有为执行后的所有语句设置使用上下文。而不是运行SQL来更改架构,而是将其直接附加到JDBC URL:
final String adatbazisURL =
"jdbc:mysql://localhost:3306/" +
Azonositok.getAdatbazisNev().trim();
或者,您可以在执行之前将架构添加到过程名称中。