Mysql create stored procedure:
create procedure s_add(in a int, in b int, out sum int)
begin
set sum = a + b;
end
After , this is the Java code
package ch04.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CstmtExample extends HttpServlet{
private String url;
private String user;
private String password;
public void init() throws ServletException {
ServletContext sc = getServletContext();
String driverClass = sc.getInitParameter("driverClass");
url = sc.getInitParameter("url");
user = sc.getInitParameter("user");
password = sc.getInitParameter("password");
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Connection conn = null;
CallableStatement cstmt = null;
try {
conn = DriverManager.getConnection(url, user, password);
cstmt = conn.prepareCall("{call s_add(?, ?, ?)}");
cstmt.setInt(1, 5);
cstmt.setInt("b", 6);
cstmt.registerOutParameter(3, Types.INTEGER);
cstmt.execute();
int ret = cstmt.getInt(3);
PrintWriter out = resp.getWriter();
out.println(ret);
out.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
there is no problem with the connection, but I don"t know how to correct the error in the console output.
java.sql.SQLException: Parameter index out of range (98 > number of parameters, which is 3).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
this is a console error.