-1

我是 jsp 页面的新手,并试图通过检查数据库中的用户名和密码来创建登录页面。这是代码:

protected void CheckUser(String username, String password)
{

    try {
        connect();
        PreparedStatement preparedStatement = con.PreparedStatement(
        "SELECT * Users WHERE username = ? AND password = ?");
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
ResultSet rs = (ResultSet) preparedStatement.executeQuery();

    } catch (Exception e) {
        System.out.println("cannot connect");
        e.printStackTrace();
    }

}

我收到 con.PreparedStatement 的以下错误

The method PreparedStatement(String) is undefined for the type Connection

谁能帮我这个?

谢谢

编辑:这是我的连接功能:

 static String dbUrl="jdbc:mysql://localhost:3306/Bank";
  static String username="root";
  static String password="";

  static Connection con=null;

  public static void connect ()
  {
      try {
       Class.forName("com.mysql.jdbc.Driver").newInstance();
       con=(Connection) DriverManager.getConnection(dbUrl,username,password);
       System.out.println("Connected!");

      } 
      catch (Exception e) {

          e.printStackTrace();
          System.out.println("not connected");

      }

  }
4

3 回答 3

4

你拼错了方法Connection#prepareStatement(String sql) throws SQLException

应该是con.prepareStatement,不是con.PreparedStatement

于 2013-07-01T21:35:49.247 回答
2
  1. 您必须通过执行创建准备好的语句PreparedStatement preparedStatement = con.prepareStatement("your Query");
  2. 您需要from查询中的关键字。

因此,您的代码应如下所示

PreparedStatement preparedStatement = con.prepareStatement("SELECT * from Users WHERE username = ? AND password = ?");
于 2013-07-01T21:38:28.317 回答
1

不确定其他内容,但考虑到它不是拼写错误,您的查询是错误的,会抛出SQLException.

它应该是

   "SELECT * FROM Users WHERE username = ? AND password = ?"
             ^^^^ 
             ||||
             here 
于 2013-07-01T21:33:34.363 回答