Mysql-the MYSQL database is successfully connected, but the result of traversing next () is not what I want !!!

Source: Internet
Author: User
In event monitoring, check whether the content of JTextField is consistent with that in the database. if the content of JTextField is the same, the logon succeeds. Otherwise, the logon fails. However, no matter what the content of JTextField is, the logon fails, please help me... Importjava. SQL. *; importjavax. swing. JOptionPane; publicclassMysqlJdbc {publicusernewuser (); publicvoidLogin (Useruser) {Stringurlnull; Connect... mysql traverses the exception database

In event monitoring, check whether the content of JTextField is consistent with that in the database. if the content of JTextField is the same, the logon succeeds. Otherwise, the logon fails. However, no matter what the content of JTextField is, the logon fails, please help me...

Import java. SQL .*;

Import javax. swing. JOptionPane;

Public class MysqlJdbc {
Public User user = new User ();

Public void Login (User user) {String url = null; Connection con = null; Statement stmt = null; ResultSet rs = null; // String qu = "SELECT * FROM login where UserName = '" + user + "';"; try {String a = user. getUserName (); String B = user. getPassword ();/** obtain the connection object through the mySQL dedicated engine * locahost is the IP address, 3306 is the port number, and DatabaseName is the installed database service name */url = "jdbc: mysql: localhost: 3306; DatabaseName = hou "; Class. forName ("com. mysql. jdbc. driver "); S Ystem. out. println ("database connected successfully! "); Con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/hou", "root", "123456"); stmt = con. createStatement (); String query = "select UserName, Password from login where UserName = '" + a + "' and Password = '" + B + "';"; // submit a query statement to the database to obtain a result set rsw.stmt.exe cuteQuery (query) referenced by rs; // traverse the result set and display its content if (rs. next () {JOptionPane. showMessageDialog (null, "logon successful");} else {JOptionPane. showMessageDialog (null, "logon failed ") ;} Catch (Exception e) {System. out. print ("failed to connect to the database! "); E. printStackTrace () ;}finally {try {if (rs! = Null) {rs. close () ;}if (stmt! = Null) {stmt. close () ;}if (con! = Null) {con. close () ;}} catch (SQLException e ){}}}

}

BtnLogin. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
String UserName = textUserName. getText ();

String Password = passwordField. getText ();
User user = new User ();
User. setUserName (UserName );
User. setPassword (Password );

                          MysqlJdbc mysqlJdbc=new MysqlJdbc();                          mysqlJdbc.Login(user);                            }                            }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.