鍍金池/ 問(wèn)答/云計(jì)算  Java  數(shù)據(jù)庫(kù)/ java.sql.SQLException: Parameter index o

java.sql.SQLException: Parameter index out of range

Mysql創(chuàng)建存儲(chǔ)過(guò)程:

create procedure s_add(in a int, in b int, out sum int)  
begin  
    set sum = a + b;  
end

之后這是Java代碼

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

連接沒(méi)問(wèn)題,就是對(duì)于控制臺(tái)輸出的報(bào)錯(cuò)不知道怎么改?

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)

這是控制臺(tái)報(bào)錯(cuò)。

回答
編輯回答
菊外人

應(yīng)該是sql寫(xiě)的有問(wèn)題.
另外. DriverManager鏈接數(shù)據(jù)庫(kù)的代碼提到外面,寫(xiě)在靜態(tài)代碼塊里或init方法里. 防止多次請(qǐng)求初始化多次鏈接.

2018年7月25日 19:03
編輯回答
心夠野
cstmt.setInt('b', 6);是什么?‘b’十進(jìn)制是98
改為
cstmt.setInt(2, 6);
2018年4月9日 13:01