java.sql.SQLException:参数索引超出范围(2>参数个数,即1)

时间:2015-06-08 15:03:45

标签: java mysql jdbc prepared-statement

大家好我是新来的我面对下面提到的类中的问题所有我想做的是提供程序名称作为参数并从数据库返回程序链接这是我的代码。

public String getLink (String Program){

    String Program_Link=null;

    try {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }

        Connection mycon=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/programinformation","root","");
        PreparedStatement mystmt=mycon.prepareStatement("SELECT Program_Link from program_table where Program_Name=?"); 
        mystmt.setString(2, Program);       
        ResultSet rs=mystmt.executeQuery();

        System.out.println("The link acquired is"+rs.getString("Program_Link"));

        while(rs.next()){
            System.out.println(rs.getString(2));
        }
    } catch (Exception exc){
        exc.printStackTrace();
    }

    return Program_Link;    
}

但是我收到以下错误

Connecting to a selected database...
Connected to database successfully...
Creating statement...
Statement created ...
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
    at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3399)
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3384)
    at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4110)
    at com.TvToday.StarSystems.TSMService.getLink(TSMService.java:35)
    at com.TvToday.StarSystems.Process.doPost(Process.java:36)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Server Responding from doPost Method  !!!

1 个答案:

答案 0 :(得分:0)

错误消息准确捕获问题;你的SQL是:

SELECT Program_Link from program_table where Program_Name=?

有1个占位符(通过问号编码),但您尝试设置 2nd 占位符的值:

mystmt.setString(2, Program);

所以,它爆炸了。尝试将2更改为1

mystmt.setString(1, Program);

你也有一个错误。如果驱动程序无法加载(btw已超过10年没有要求),则会捕获异常,打印错误消息,但无论如何。如果您遇到异常,请解决问题或记录并返回。否则,根本不要抓住它。