简体   繁体   中英

Running a stored procedure from Java using Statement Execute?

My Java code goes

Statement statement = connection.createStatement();
String storedProc = "exec stored_proc(" + someVariable + ")";
statement.execute(storedProc);

But Java throws an SQLSyntaxException. Any idea what I am doing wrong?

Try this query:

In your current approach you can use:

String storedProc = "{call stored_proc(" + someVariable + ")}";

Note that I have used call instead of exec , and I have surrounded query with curly braces.

But to avoid sql injection you can use parametrised query like:

String storedProc="{call stored_proc(?)}";
PreparedStatement pstmt=connection.prepareStatement(storedProc);
pstmt.setString(1,someVariable);
pstmt.execute(storedProc);

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