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

来源:互联网 发布:药品销售人员软件 编辑:程序博客网 时间:2024/06/05 08:39
  • 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

0 0