Posted by:soyoyo
Posted on:2004-11-30 22:30
下面这个是一个连接的类,因为后面多处用到,我就写了一个
package cdms;
import java.sql.*;
public class cardConnect {
public cardConnect() {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ee){}
}
public static Connection getconn() throws SQLException{
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=dbcard.mdb";
//String url="jdbc:odbc:jacon";
return DriverManager.getConnection(url);
}
public static void close(ResultSet rs){
try{
rs.close();
}
catch(Exception ex){}
}
public static void close(Statement stmt){
try{
stmt.close();
}
catch(Exception ex){}
}
public static void close(Connection conn){
try{
conn.close();
}
catch(Exception ex){}
}
private static cardConnect dbconn=new cardConnect();
}
下面是一个frame:
package cdms;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class loginFrame extends JFrame {
private String CardID;
private String loginQuery;
private String passwordtemp;
private PreparedStatement loginstmt;
private Connection con;
private ResultSet rs;
JPanel contentPane;
GridLayout gridLayout1 = new GridLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JLabel jLabel1 = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JTextField jTextField1 = new JTextField();
JPasswordField jPasswordField1 = new JPasswordField();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
//Construct the frame
public loginFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
con=cardConnect.getconn();
loginQuery="Select * from card where CardID=? and Password=?";
loginstmt=con.prepareStatement(loginQuery);
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(gridLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("欢迎使用银行卡管理系统");
gridLayout1.setRows(4);
jLabel1.setFont(new java.awt.Font("Serif", 1, 28));
jLabel1.setForeground(Color.blue);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setText("欢迎使用银行卡系统");
jPanel1.setLayout(borderLayout1);
jPanel1.setMinimumSize(new Dimension(34, 16));
jTextField1.setBackground(Color.white);
jTextField1.setToolTipText("");
jTextField1.setText("");
jTextField1.setColumns(15);
jPasswordField1.setText("");
jPasswordField1.setColumns(20);
jLabel2.setFont(new java.awt.Font("Serif", 0, 18));
jLabel2.setText("卡号:");
jLabel3.setFont(new java.awt.Font("Serif", 0, 18));
jLabel3.setText("密码:");
jButton1.setText("确定");
jButton1.addActionListener(new loginFrame_jButton1_actionAdapter(this));
jButton2.setText("重置");
jButton2.addActionListener(new loginFrame_jButton2_actionAdapter(this));
contentPane.add(jPanel1, null);
jPanel1.add(jLabel1, BorderLayout.CENTER);
contentPane.add(jPanel2, null);
jPanel2.add(jLabel2, null);
jPanel2.add(jTextField1, null);
contentPane.add(jPanel3, null);
jPanel3.add(jLabel3, null);
jPanel3.add(jPasswordField1, null);
contentPane.add(jPanel4, null);
jPanel4.add(jButton1, null);
jPanel4.add(jButton2, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent
;
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText("");
jPasswordField1.setText("");
}
void jButton1_actionPerformed(ActionEvent e) {
try{
CardID = jTextField1.getText();
loginstmt.setString(1, CardID);
passwordtemp="";
for(int i=0;i
passwordtemp+=jPasswordField1.getPassword()[i];
loginstmt.setString(2,passwordtemp);
rs=loginstmt.executeQuery(loginQuery);//我调试的时候,大概是到这个地方开始出问题
if(rs.next())
{
eventFrame eventFrame1=new eventFrame(CardID);//eventFrame是另一个Frame类
eventFrame1.setSize(new Dimension(400,300));//用户名密码验证正确则弹出eventFrame
this.dispose();
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=eventFrame1.getSize();
eventFrame1.setState(eventFrame1.NORMAL);
eventFrame1.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
eventFrame1.show();
}
else{
jTextField1.setText("卡号或密码有误");
jPasswordField1.setText("");
}
}
catch(SQLException ex){
System.out.println("系统有错误,请重新运行");
System.out.println(ex);
}
finally{
cardConnect.close(rs);
cardConnect.close(loginstmt);
cardConnect.close(con);
}
}
}
class loginFrame_jButton2_actionAdapter implements java.awt.event.ActionListener {
loginFrame adaptee;
loginFrame_jButton2_actionAdapter(loginFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed
;
}
}
class loginFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {
loginFrame adaptee;
loginFrame_jButton1_actionAdapter(loginFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed
;
}
}