在Netbeans上为SQL Server创建GUI时出错

时间:2017-12-15 02:38:10

标签: java sql-server netbeans

您好我正在开发一个SQL项目,我正在Netbeans上创建一个GUI。我找到了一个人使用的代码,我希望改变它。但是,我目前收到的错误是我不知道如何修复它。我已经联系过创建此代码的人,但我没有得到回复。运行我的代码后得到的错误是:

com.microsoft.sqlserver.jbdc.SQLServerException: The method executeQuery() cannot take arguements on a PreparedStatement or CallableStatement

以下是我复制的代码部分

private void signinActionPerformed(java.awt.event.ActionEvent evt) {                                       
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://SQL2.cis245.mc3.edu:1433;\" + \"databaseName=zz_CIS245_16;user=tpatel;password=tpatel";   

            Connection con = DriverManager.getConnection(url);
            System.out.println("connection created");

            String sql="select * from test where Username=? and Passeword=?";
            PreparedStatement pst = con.prepareStatement(sql);
            pst.setString(1, username.getText());
            pst.setString(2, password.getText());
            ResultSet rs=pst.executeQuery(sql);
            if(rs.next())
            {
                JOptionPane.showMessageDialog(null, "password and username matched");
                //System.out.println("Address : "+rs.getString(2));
            }
            else {
            JOptionPane.showMessageDialog(null,"password or username not corrected");
            username.setText("");
            password.setText("");
            }
                con.close();
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }

1 个答案:

答案 0 :(得分:1)

您已在此处将查询传递给PreparedStatement变量:

            PreparedStatement pst = con.prepareStatement(sql);

从exectuteQuery()方法中删除参数

            ResultSet rs=pst.executeQuery();