日期:2014-05-17  浏览次数:20765 次

prepareStatement的使用问题,我哪里错了
Java code
    public String login(){
        
        String sql = "select * from user where name=? and password= ? ";
        System.out.println(name+":"+pass);
        try {
            rs = db.executeQuery(sql, new String[]{name,pass});
        } catch (Exception e) {
            e.printStackTrace();
            new java.sql.SQLException();
        }
        if(rs==null){
            db.close();
            return "not";
        }else{
            db.close();
            return SUCCESS;
        }
    }


Java code
    public ResultSet executeQuery (String sql,String[] value) throws Exception{
        try { // 捕捉异常
            if(conn==null){
                conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
            }else{
                
            }
             System.out.println(value.length);
            stmt = conn.prepareStatement(sql);
            for(int i=1;i<=value.length;i++){
                stmt.setString(i, value[i-1]);
            }
           
            rs = stmt.executeQuery(sql);//4.执行查询
        } catch (SQLException ex) {
            System.err.println(ex.getMessage()+"aaaaa"); // 输出异常信息
        }
        return rs; 
    }
    

这是提示错误,检查了很久,没发现错误啊
XML code

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? and password= ?' at line 1



------解决方案--------------------
应该是有代参数的那个方法,那个方法就是执行你传入的那个sql,而你传入的sql是select * from user where name=? and password= ?这句,所以才报了你发的那个错误