我的Java代码去了

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


但是Java抛出SQLSyntaxException。知道我在做什么错吗?

最佳答案

试试这个查询:

在当前方法中,您可以使用:

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


请注意,我使用了call而不是exec,并且用大括号将查询括起来。

但是要避免sql injection,可以使用参数化查询,例如:

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

09-27 17:47