📄 login.java
字号:
/**
*
*/
package 图书馆;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
/**
*
* @author hp
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Login extends JFrame implements ActionListener {
private JLabel JLb1;
private JLabel JLb2;
private JButton Ok_btn;
private JButton Cancel_btn;
private JTextField Jtfld1;
private JPasswordField Jtfld2;
private JFrame frame;
private Connection con;
private Statement stmt;
private ResultSet result1;
/**
* 构造函数
*
*/
public Login() {
frame = new JFrame("登陆");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = frame.getContentPane();
/**
* content设置面板
*/
content.setLayout(new GridLayout(3, 2,20,20));
/**
* 设置组件
*/
JLb1 = new JLabel("用户名:");
JLb2 = new JLabel("密码:");
Jtfld1 = new JTextField();
Jtfld2 = new JPasswordField();
Ok_btn = new JButton("确定");
Cancel_btn = new JButton("取消");
/**
* 事件监听
*/
Ok_btn.addActionListener(this);
Cancel_btn.addActionListener(this);
/**
* content添加组件
*/
content.add(JLb1);
content.add(Jtfld1);
content.add(JLb2);
content.add(Jtfld2);
content.add(Ok_btn);
content.add(Cancel_btn);
frame.setSize(250, 150);
frame.setVisible(true);
/**
* 加载驱动器
*连接数据库
* 设置数据源
*/
try {
//加载驱动器
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//设置数据源
con = DriverManager.getConnection("jdbc:odbc:demo");
stmt = con.createStatement();
} catch (ClassNotFoundException e) {
} catch (SQLException ex) {
}
}
/* *
* 事件处理
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str1, str2, username, sqlStr;
Object obj = e.getSource();
str1 = Jtfld1.getText().trim();
str2 = Jtfld2.getText().trim();
try {
/**
* 按钮Ok_btn的处理
*/
if (obj.equals(Ok_btn))
{
if (str1.equals("")|str2.equals("")) {
JOptionPane.showMessageDialog(frame,
"username or password is error!");
return;
}
/**
* 连接表userziliao
*
*/
sqlStr = "select * from userziliao where 姓名=" + "'"
+ str1 + "'" + " and 密码=" + "'" + str2 + "'";
ResultSet result = stmt.executeQuery(sqlStr);
if (result.next()) {
//表的处理
username = result.getString("姓名");
new MainFrame();
frame.dispose();
stmt.close();
con.close();
}else if(!result.next()){
JOptionPane.showMessageDialog(frame,"用户不存在,请注册!");
frame.dispose();
new TransactCard();
}
} else if(obj.equals(Cancel_btn)){
/**
* 按钮Cancel_btn的处理
*/
frame.dispose();
stmt.close();
con.close();
}
} catch (SQLException ex) {
System.err.println(ex);
}
}
/**
* 主函数
* @param args
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Login();
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -