Obaid
Obaid

Reputation: 75

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?

Upvotes: 1

Views: 3223

Answers (1)

Bhushan
Bhushan

Reputation: 6181

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

Upvotes: 2

Related Questions