Home > Database > Mysql Tutorial > mysql-数据库MYSQL连接成功,可是遍历next()结果不是我想要呀!!!

mysql-数据库MYSQL连接成功,可是遍历next()结果不是我想要呀!!!

WBOY
Release: 2016-06-06 09:35:09
Original
1227 people have browsed it

mysql遍历exception数据库

事件监听中要检验JTextField的内容是否与数据库中一致,一致则登录成功,否则登录失败,但是现在无论JTextField的内容如何,都是输出登录失败,求大神解救啊。。。

import java.sql.*;

import javax.swing.JOptionPane;

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

<code>    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();    /*     * 通过mySQL专用引擎获得连接对象     * locahost为IP地址,3306为端口号,DatabaseName为安装的一个数据库服务名    */    url="jdbc:mysql:localhost:3306;DatabaseName=hou";    Class.forName("com.mysql.jdbc.Driver");    System.out.println("连接数据库成功!");    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+"';";                                   //向数据库提交查询语句,获得一个被rs引用的结果集    rs=stmt.executeQuery(query);                                   //对结果集进行遍历,显示其内容    if (rs.next()) {        JOptionPane.showMessageDialog(null,"登录成功" );              }    else{       JOptionPane.showMessageDialog(null, "登录失败");     }       }    catch (Exception e) {        System.out.print("连接数据库失败!");        e.printStackTrace();    }    finally{        try{        if(rs!=null){            rs.close();}        if(stmt!=null){            stmt.close();}        if(con!=null){            con.close();}        }        catch(SQLException e){}    }    }</code>
Copy after login

}

btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String UserName=textUserName.getText();

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

<code>                          MysqlJdbc mysqlJdbc=new MysqlJdbc();                          mysqlJdbc.Login(user);                            }                            }</code>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template