主题:[讨论]如何编写登录按钮的身份验证事件
package 图书管理系统;
import java.sql.*;
import java.sql.Connection; //导入进行数据库连接时所使用的类
import java.sql.DriverManager; //导入进行数据库连接时使用的类
import java.sql.ResultSet; //导入进行数据表查询时所使用的类
import java.sql.SQLException; //导入进行数据库操作时捕捉异常使用的类
import java.net.URL;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.borland.jbcl.layout.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2009</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Frame6 extends JFrame {
JLabel yhm = new JLabel();
JTextField username = new JTextField();
JLabel mm = new JLabel();
JButton dl = new JButton();
JButton cz = new JButton();
XYLayout xYLayout1 = new XYLayout();
JPasswordField password = new JPasswordField();
public Frame6() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
yhm.setText("用 户 名:");
this.setIconImage(null);
this.setTitle("图书管理系统登录");
this.getContentPane().setLayout(xYLayout1);
username.setText("");
mm.setText("密 码:");
password.setText("");
password.addActionListener(new Frame6_password_actionAdapter(this));
dl.setText("登录");
dl.addActionListener(new Frame6_dl_actionAdapter(this));
cz.setText("重置");
cz.addActionListener(new Frame6_jButton2_actionAdapter(this));
this.getContentPane().add(yhm, new XYConstraints(76, 86, 63, 25));
this.getContentPane().add(username, new XYConstraints(160, 88, 90, -1));
this.getContentPane().add(mm, new XYConstraints(76, 134, -1, 18));
this.getContentPane().add(dl, new XYConstraints(73, 216, 72, 30));
this.getContentPane().add(cz, new XYConstraints(219, 213, 74, 31));
this.getContentPane().add(password, new XYConstraints(158, 135, 89, -1));
}
//连接数据库
public class dbManager {
ResultSet rs;
private String strurl = "jdbc:microsoft:sqlserver://localhost:1433;User=; Password=; DatebaseName=libbook";
public dbManager() {}
public ResultSet getResult(String sql) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager.getConnection(strurl);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(sql);
System.out.println("executeSql------" + sql);
return rs;
}
catch (Exception e) {
System.out.println("getResult------" + e.toString());
return null;
}
}
public boolean executeSql(String sql) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager.getConnection(strurl);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("executeSql------" + sql);
return true;
}
catch (Exception e) {
System.out.println("executeSql------" + e.toString());
return false;
}
}
}
void dl_actionPerformed(ActionEvent e) {
dbManager db=new dbManager();//数据库驱动类
String name=username.getText().trim();//获得用户输入的用户名
char[] p=password.getPassword();//获得用户输入的密码
String password=new String(p);//用户输入密码
String query="Select * From operator Where name='"+ username +"'and password='"+ password + "'and admin=1";
//SQL语句根据用户名和密码获得结果集
try {PreparedStatement ps = db.getConnection().prepareStatement(query);//此行显示有误
ps.setString(1,name);
ps.setString(2,password);
ResultSet rs=ps.executeQuery();
if(name==""||password==""){//如果密码和用户名为空则给出相应提示
JOptionPane.showMessageDialog(this,"用户名密码不能为空!");
}
else if(rs.next()){//如果结果集不为空
this.dispose();//释放当前窗体
Frame1 dlg =new Frame1();
Dimension dlgSize=dlg.getPreferredSize();
Dimension frmSize=getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width-dlgSize.width)/2+loc.x,
(frmSize.height-dlgSize.height)/2+loc.y);
dlg.pack();
dlg.show();//弹出主窗体
}
else{
username.requestFocus(true);
JOptionPane.showMessageDialog(this,"用户名密码错误!"); }
}
catch (SQLException ex) {
}
}
急!!! 请高人指点,谢谢指教~
import java.sql.*;
import java.sql.Connection; //导入进行数据库连接时所使用的类
import java.sql.DriverManager; //导入进行数据库连接时使用的类
import java.sql.ResultSet; //导入进行数据表查询时所使用的类
import java.sql.SQLException; //导入进行数据库操作时捕捉异常使用的类
import java.net.URL;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.borland.jbcl.layout.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2009</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Frame6 extends JFrame {
JLabel yhm = new JLabel();
JTextField username = new JTextField();
JLabel mm = new JLabel();
JButton dl = new JButton();
JButton cz = new JButton();
XYLayout xYLayout1 = new XYLayout();
JPasswordField password = new JPasswordField();
public Frame6() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
yhm.setText("用 户 名:");
this.setIconImage(null);
this.setTitle("图书管理系统登录");
this.getContentPane().setLayout(xYLayout1);
username.setText("");
mm.setText("密 码:");
password.setText("");
password.addActionListener(new Frame6_password_actionAdapter(this));
dl.setText("登录");
dl.addActionListener(new Frame6_dl_actionAdapter(this));
cz.setText("重置");
cz.addActionListener(new Frame6_jButton2_actionAdapter(this));
this.getContentPane().add(yhm, new XYConstraints(76, 86, 63, 25));
this.getContentPane().add(username, new XYConstraints(160, 88, 90, -1));
this.getContentPane().add(mm, new XYConstraints(76, 134, -1, 18));
this.getContentPane().add(dl, new XYConstraints(73, 216, 72, 30));
this.getContentPane().add(cz, new XYConstraints(219, 213, 74, 31));
this.getContentPane().add(password, new XYConstraints(158, 135, 89, -1));
}
//连接数据库
public class dbManager {
ResultSet rs;
private String strurl = "jdbc:microsoft:sqlserver://localhost:1433;User=; Password=; DatebaseName=libbook";
public dbManager() {}
public ResultSet getResult(String sql) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager.getConnection(strurl);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(sql);
System.out.println("executeSql------" + sql);
return rs;
}
catch (Exception e) {
System.out.println("getResult------" + e.toString());
return null;
}
}
public boolean executeSql(String sql) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager.getConnection(strurl);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("executeSql------" + sql);
return true;
}
catch (Exception e) {
System.out.println("executeSql------" + e.toString());
return false;
}
}
}
void dl_actionPerformed(ActionEvent e) {
dbManager db=new dbManager();//数据库驱动类
String name=username.getText().trim();//获得用户输入的用户名
char[] p=password.getPassword();//获得用户输入的密码
String password=new String(p);//用户输入密码
String query="Select * From operator Where name='"+ username +"'and password='"+ password + "'and admin=1";
//SQL语句根据用户名和密码获得结果集
try {PreparedStatement ps = db.getConnection().prepareStatement(query);//此行显示有误
ps.setString(1,name);
ps.setString(2,password);
ResultSet rs=ps.executeQuery();
if(name==""||password==""){//如果密码和用户名为空则给出相应提示
JOptionPane.showMessageDialog(this,"用户名密码不能为空!");
}
else if(rs.next()){//如果结果集不为空
this.dispose();//释放当前窗体
Frame1 dlg =new Frame1();
Dimension dlgSize=dlg.getPreferredSize();
Dimension frmSize=getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width-dlgSize.width)/2+loc.x,
(frmSize.height-dlgSize.height)/2+loc.y);
dlg.pack();
dlg.show();//弹出主窗体
}
else{
username.requestFocus(true);
JOptionPane.showMessageDialog(this,"用户名密码错误!"); }
}
catch (SQLException ex) {
}
}
急!!! 请高人指点,谢谢指教~