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)