简体   繁体   中英

i can't connecting to mysql with java swing [on hold]

when i clicked on login button i get this message: 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 ''' at line 1

       private static String URL="";
   private static Connection con;
   public static void setURL(){



      URL = "jdbc:mysql://localhost:3306/school"
                      + "?useUnicode=true&characterEncoding=UTF-8";
   }

   public static void setConnection(){


            try {
              setURL();
             con = DriverManager.getConnection(URL,"root","");
            } catch (SQLException ex) {
          Tools.msgBox(ex.getMessage());

           }
       }



   public static boolean checkUserandPass(String username,String password) {



       try {

           setConnection();
           Statement  stmt = con.createStatement();
           String strCheck = "SELECT * from users WHERE "
                               + "username='" + username+ "' and "
                               + "pass=" +password + "'";
           stmt.executeQuery(strCheck);
           while(stmt.getResultSet().next()) {

               return true;
           }

       con.close();

   }
       catch(SQLException e) {
           Tools.msgBox(e.getMessage());
       }
       return false;

}

You forgot to put the first ` before the passowrd

  • "pass=" <<--this is what you wrote and you should write + "pass='"+...

You have missed a single quote in pass=

  String strCheck = "SELECT * from users WHERE "
                                   + "username='" + username+ "' and "
                                   + "pass=" +password + "'";

It should be

String strCheck = "SELECT * from users WHERE "
                               + "username='" + username+ "' and "
                               + "pass='" +password + "'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM