Java连接MySQL用户名密码验证出现问题。请各位大侠看看。

涵笑情川 2010-12-07 11:23:08
package qxz;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;

public class Login extends JFrame{
public static void main(String[] args){
new Login();
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Frame jf;
TextField textusername=new TextField("admin");
JPasswordField textuserpassword=new JPasswordField("admin");
Label label=new Label("企业员工管理系统");
Label labelusername=new Label("用户名");
Label labeluserpassword=new Label("密码");
Button buttonenter=new Button("登录");
Button buttoncancel=new Button("重置");
public Login(){
jf=this;
this.setBounds(300,300,300,200);
this.setTitle("登录");
Font f=new Font("新宋体",Font.PLAIN,12);
Container con=this.getContentPane();
con.setLayout(null);

label.setBounds(80,10,140,20);
label.setFont(new Font("新宋体",Font.BOLD,16));
con.add(label);

labelusername.setBounds(55, 45, 55, 20);
labelusername.setFont(f);
con.add(labelusername);

labeluserpassword.setBounds(55, 65, 55, 20);
labeluserpassword.setFont(f);
con.add(labeluserpassword);

textusername.setBounds(120, 45, 120, 20);
textusername.setFont(f);
con.add(textusername);

textuserpassword.setBounds(120,65,120,20);
textuserpassword.setFont(f);
con.add(textuserpassword);

buttonenter.setBounds(90, 115, 60, 20);
con.add(buttonenter);

buttoncancel.setBounds(150,115,60,20);
con.add(buttoncancel);
jf.setVisible(true);
//重置按钮的鼠标事件监听;
buttoncancel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
textusername.setText("");
textuserpassword.setText("");
}
});
//鼠标事件监听
buttonenter.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
if(textusername.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用户名不能为空!");
}
else if(textuserpassword.getText().equals("")){
new JOptionPane().showMessageDialog(null,"密码不能为空!");
}
else{
String sql="select * from AdministratorInformation where User_Name = '" + textusername.getText() + "' and Password = '" + textuserpassword.getText()+ "'";
//sql="select * from UserInformation ";
System.out.println(sql);
try {
Analysis(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
//鼠标键盘监听。
buttonenter.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(textusername.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用户名不能为空!");
}
else if(textuserpassword.getText().equals("")){
new JOptionPane().showMessageDialog(null,"密码不能为空!");
}
else{
String sql="select * from AdministratorInformation where User_Name = '" + textusername.getText() + "' and Password = '" + textuserpassword.getText()+ "'";
System.out.println(sql);
try {
Analysis(sql);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
private void Analysis(String sqlString){
try{
Class.forName("com.mysql.jdbc.Driver");

conn=DriverManager.getConnection("jdbc:mysql://localhost/employee?" +
"user=root&password=lenovo001");
stmt = conn.createStatement();
//rs=stmt.getResultSet();
// String pass=rs.getString("Password");
// System.out.println("pass");
if(rs.isBeforeFirst()){
while(rs.next()){
rs=stmt.executeQuery(sqlString);
if(rs.getString("Password").equals(textuserpassword.getText())){
System.out.println("密码正确,欢迎登录系统");
new JOptionPane().showMessageDialog(null, "go");
conn.close();
}else{
new JOptionPane().showMessageDialog(null, "密码错误请重新输入");
//new Login();
}
}
}
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { } // ignore
stmt = null;
}
if(conn!=null){
try{
conn.close();
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
}
}

select * from AdministratorInformation where User_Name = 'admin' and Password = 'admin'
java.lang.NullPointerException
at qxz.Login.Analysis(Login.java:122)
at qxz.Login.access$0(Login.java:112)
at qxz.Login$2.mouseClicked(Login.java:82)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
...全文
255 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
小龙在线 2010-12-07
  • 打赏
  • 举报
回复
关键错误
conn=DriverManager.getConnection("jdbc:mysql://localhost/employee?" +
"user=root&password=lenovo001");
stmt = conn.createStatement();
//rs=stmt.getResultSet();
// String pass=rs.getString("Password");
// System.out.println("pass");

找到错误的地方了,看看吧,都是一些jdbc的知识
小龙在线 2010-12-07
  • 打赏
  • 举报
回复
代码有点乱,可以把操作数据库的代码封装在dao里面,单元测试很方便
茫茫大海 2010-12-07
  • 打赏
  • 举报
回复
rs初始化为null,然后后面没有赋值,所以报NullPointerException
guangxizhonghao 2010-12-07
  • 打赏
  • 举报
回复
rs.isBeforeFirst中的rs的值为null
涵笑情川 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mycdsnstudy 的回复:]

关键错误
conn=DriverManager.getConnection("jdbc:mysql://localhost/employee?" +
"user=root&password=lenovo001");
stmt = conn.createStatement();
//rs=stmt.getResultSet();
// String pass=rs.getStri……
[/Quote]
请问封装的话,我是用一个类来操作数据库,可是这样好像无法验证了。所以我才把她放在同一个类中。。。。
请指点一下,对数据库的操作要怎么样进行?

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧