简体   繁体   中英

JDBC MySQL stored procedure throw exception “ Parameter number 2 is not an OUT parameter”?

I have this code snippet:

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();

When I run this code, it throws "Parameter number 2 is not an OUT parameter" Exception. Why?

The problem is this:

String sql = "USE"+Azonositok.getAdatbazisNev()+";";
utasitas.executeUpdate(sql);

The use context does not appear to be set for all statements following its execution. Instead of running SQL to change the schema, append it directly to your JDBC URL:

final String adatbazisURL = 
    "jdbc:mysql://localhost:3306/" +
    Azonositok.getAdatbazisNev().trim();

Alternatively, you can prepend the schema to your procedure names before executing them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM