java - 检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行 '? and password = ?' 附近使用的正确语法

标签 java mysql

<分区>

我在使用 SQL 时遇到问题。这是我的代码,我不知道为什么也不知道如何修复它。我使用 Java EE 和 Eclipse 来执行此操作。

public void userLogin(String username,String password) {

   Connection conn = null;  
   PreparedStatement ps = null; 
   ResultSet rs = null;

   try {        
      conn = JDBCUtilsWithProperties.getConnection();
      String sql = "select * from users where username = ? and password = ?;";
      ps = conn.prepareStatement(sql);
      ps.setString(1,   username);
      ps.setString(2,   password);
      rs = ps.executeQuery(sql);

      if(rs.next()) {   
         System.out.println("SUCESS");
      } else {
         System.out.println("FAIL");
      }

      } catch (Exception e) {
         // TODO: handle exception
         e.printStackTrace();
      } finally {
         JDBCUtilsWithProperties.release(rs, ps, conn);
      }     
   }
}

public class JDBCUtilsWithProperties {

   private static String driverClass;
   private static String url;
   private static String username;
   private static String password;

   private JDBCUtilsWithProperties(){}

   static {
      try {
         readConfig();
         Class.forName(driverClass);
      } catch (Exception e) {
         // TODO: handle exception
         e.printStackTrace();
      }
   }

   public static void readConfig() {    
      Properties pp = new Properties();
      try { 
         pp.load(new FileInputStream("src//config.properties"));
         driverClass = pp.getProperty("driverClass");
         url = pp.getProperty("url");
         username = pp.getProperty("username");
         password = pp.getProperty("password");
      } catch (Exception e) {
         // TODO: handle exception
      }
    }

    public static Connection getConnection() {
       try {
          return DriverManager.getConnection(url,username,password);
       } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
       }
       return null;
    }

    public static Statement createStatement() {
       Connection conn = getConnection();
       Statement stat = null;
       try {
          stat = conn.createStatement();
       } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
       }
       return  stat;
    }

    public static void release(ResultSet rs,Statement stat,Connection conn){
       if(rs != null) {
          try {
             rs.close();
          } catch (Exception e) {
             // TODO: handle exception
             e.printStackTrace();
          }
          rs = null;
        }
        if(stat != null) {
           try {
              stat.close();
           } catch (Exception e) {
              // TODO: handle exception
              e.printStackTrace();
           }
           stat = null;
        }
        if(conn != null) {
           try {
              conn.close();
           } catch (Exception e) {
              // TODO: handle exception
              e.printStackTrace();
           }
           conn = null;
        }
    }

    public static void release(Statement stat,Connection conn) {
       if(stat != null){
          try {
             stat.close();
          } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
          }
          stat = null;
        }
        if(conn != null) {
           try {
              conn.close();
           } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }
           conn = null;
        }
    }
}

运行时会出现一个通知:

"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"

最佳答案

如果你使用prepared Statement,execute函数没有SQL查询作为参数:

String sql = "select * from users where username = ? and password = ?;";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
                    ^^

另见 documentation 语句

boolean execute(String sql) throws SQLException

Executes the given SQL statement, which may return multiple results. In some (uncommon) situations, a single SQL statement may return multiple result sets and/or update counts. Normally you can ignore this unless you are (1) executing a stored procedure that you know may return multiple results or (2) you are dynamically executing an unknown SQL string. The execute method executes an SQL statement and indicates the form of the first result. You must then use the methods getResultSet or getUpdateCount to retrieve the result, and getMoreResults to move to any subsequent result(s).

Note:This method cannot be called on a PreparedStatement or CallableStatement.

关于java - 检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行 '? and password = ?' 附近使用的正确语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45919455/

相关文章:

java - Android Studio 重命名原生库

java - 编辑Maven编译的存储库源代码

mysql - 在 SQL 中连接列

Java HashMap 通过 JPA 作为 MySQL 中的一个元素

php - 优化高索引表上的 INSERT/UPDATE 查询

java - 确定一周中的哪一天是一个月中的每个日期

java - 如何使用 JXL 从现有 Excel 工作表中删除行

java - 如何在嵌套类的内部类中构造和使用对象?

mysql - 由于 mysql2 或 mysql gems 无法在 Debian 上启动 Mongrel(或 Passenger)

php - 恢复mysql数据库报错