【问题标题】:check the manual that corresponds to your MySQL server version for the right syntax to use near '? and password = ?' at line 1 [duplicate]检查与您的 MySQL 服务器版本相对应的手册,以了解在 '? 附近使用的正确语法。和密码=?在第 1 行 [重复]
【发布时间】:2018-02-05 18:00:28
【问题描述】:

我遇到了 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;
        }
    }
}

运行时会出现通知:

“您的 SQL 语法有错误;请查看手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 靠近 '?和密码=?在第 1 行"

【问题讨论】:

标签: java mysql


【解决方案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();
                    ^^

另见Statement类的documentation

布尔执行(字符串 sql) 抛出 SQLException

执行给定的 SQL 语句,该语句可能返回多个结果。 在某些(不常见的)情况下,单个 SQL 语句可能会返回 多个结果集和/或更新计数。一般可以无视 除非您 (1) 执行您知道可能的存储过程 返回多个结果或 (2) 您正在动态执行 未知的 SQL 字符串。 execute 方法执行一条 SQL 语句并 表示第一个结果的形式。然后,您必须使用这些方法 getResultSet 或 getUpdateCount 检索结果,以及 getMoreResults 移动到任何后续结果。

注意:此方法不能在 PreparedStatement 或 可调用语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    相关资源
    最近更新 更多