未重新服务器而导致的错误java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is

  • java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
    用struts2,网页的action连接到数据库,使用main开始执行,不报错,但是,通过网页执行报错
    数据库是mysql
    (后面经过运行,下面的代码是可行的)
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection=DriverManager.getConnection("jdbc:mysql://localhost","root","");
        java.sql.Statement statement=connection.createStatement();
        statement.execute("use test");
        PreparedStatement preparedStatement=(PreparedStatement) connection.prepareStatement("SELECT * FROM `login` where username=? and password=?");
        preparedStatement.setString(1, this.getUsername());
        preparedStatement.setString(2, this.getPassword());

        ResultSet resultSet=preparedStatement.executeQuery();

解决方案
我使用拼接的方式

        String command="SELECT * FROM `login` where username='"+this.getUsername()+"' and password='"+this.getPassword()+"'";
        System.out.println(command);
        ResultSet resultSet=statement.executeQuery(command);

成功了

但是

我真正的原因是,没有重启tomcat

你可能感兴趣的:(数据库,bug,struts2.0)