javamsyqlswing图书馆管理系统java代码
时间: 2023-12-20 10:32:23 浏览: 29
以下是一个简单的Java MySQL Swing图书馆管理系统的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class LibraryManagementSystem extends JFrame implements ActionListener {
JLabel l1, l2, l3;
JTextField tf1, tf2;
JButton btn1, btn2;
LibraryManagementSystem() {
setTitle("图书馆管理系统");
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1 = new JLabel("图书馆管理系统");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 30));
l2 = new JLabel("用户名:");
l3 = new JLabel("密码:");
tf1 = new JTextField();
tf2 = new JPasswordField();
btn1 = new JButton("登录");
btn2 = new JButton("重置");
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
btn1.setBounds(150, 160, 100, 30);
btn2.setBounds(270, 160, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
int x = 0;
String s1 = tf1.getText();
String s2 = tf2.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "password");
PreparedStatement ps = con.prepareStatement("select * from login where username=? and password=?");
ps.setString(1, s1);
ps.setString(2, s2);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
x = 1;
}
} catch (Exception ex) {
System.out.println(ex);
}
if (x == 1) {
JOptionPane.showMessageDialog(btn1, "登录成功");
} else {
JOptionPane.showMessageDialog(btn1, "用户名或密码错误");
}
} else {
tf1.setText("");
tf2.setText("");
}
}
public static void main(String[] args) {
new LibraryManagementSystem();
}
}
```