import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class Myframe extends JFrame{
JLabel lab1,lab2,lab3;
JTextField txt1,txt2;
JButton bun1,bun2;
Font f1;
public Myframe(String str) {
super(str);
}
void inint() {
this.setLayout(null);
this.setBounds(450,200, 500, 400);
lab1=new JLabel("用戶名:");
lab2=new JLabel("密 碼:");
lab3=new JLabel("同學錄管理系統");
bun1=new JButton("確定");
bun2=new JButton("取消");
txt1=new JTextField();
txt2=new JTextField();
lab1.setFont(new Font("宋體",Font.PLAIN,17));
lab2.setFont(new Font("宋體",Font.PLAIN,17));
lab3.setFont(new Font("宋體",Font.PLAIN,17));
bun1.setFont(new Font("宋體",Font.PLAIN,16));
bun2.setFont(new Font("宋體",Font.PLAIN,16));
lab1.setBounds(160,100, 60,30);
lab2.setBounds(160,130, 80,30);
lab3.setBounds(190,30, 120,30);
txt1.setBounds(220,100,100,25);
txt2.setBounds(220,130,100,25);
bun1.setBounds(150,200,70,30);
bun2.setBounds(280,200,70,30);
this.add(lab1);
this.add(lab2);
this.add(lab3);
this.add(txt1);
this.add(txt2);
this.add(bun1);
this.add(bun2);
bun1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (txt1.getText().equals("")) {
JOptionPane.showMessageDialog(bun1, "輸入不能爲空", "提示",JOptionPane.WARNING_MESSAGE);
} else if (txt1.getText().equals("x") && txt2.getText().equals("1")) {
//JOptionPane.showMessageDialog(bun1, "輸入正確","恭喜", JOptionPane.WARNING_MESSAGE);
try {
new fuben1();
}catch(Exception ex) {
ex.getMessage();
}
Myframe.this.setVisible(false);
} else {
JOptionPane.showMessageDialog(bun1,"用戶名或者密碼輸入錯誤", "提示", JOptionPane.WARNING_MESSAGE);
}
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
public class tongxuelu1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Myframe f=new Myframe("同學錄管理系統登錄");
f.inint();
}
}
package 課程設計;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
import com.mysql.jdbc.Statement;
public class fuben1 extends JFrame {
public fuben1() throws Exception {
JFrame n1 = new JFrame("同學錄管理系統");
n1.setLayout(null);
ResultSet rs = null;
JButton b1 = new JButton("插入");
JButton b2 = new JButton("刪除");
JButton b3 = new JButton("修改");
JButton b4 = new JButton("查詢");
JButton b5 = new JButton("學號:");
JButton b6 = new JButton("姓名:");
JButton b7 = new JButton("刷新");
JTextField txt1 = new JTextField();
JTextField txt2 = new JTextField();
// JTextField txt3 = new JTextField();
JTextArea txt3 = new JTextArea();
txt1.setFont(new Font("宋體", Font.PLAIN, 20));
txt2.setFont(new Font("宋體", Font.PLAIN, 20));
txt3.setFont(new Font("宋體", Font.PLAIN, 20));
b1.setFont(new Font("宋體", Font.PLAIN, 20));
b2.setFont(new Font("宋體", Font.PLAIN, 20));
b3.setFont(new Font("宋體", Font.PLAIN, 20));
b4.setFont(new Font("宋體", Font.PLAIN, 20));
b5.setFont(new Font("宋體", Font.PLAIN, 17));
b6.setFont(new Font("宋體", Font.PLAIN, 17));
b7.setFont(new Font("宋體", Font.PLAIN, 17));
n1.setBounds(290, 50, 800, 600);
txt1.setBounds(340, 100, 120, 40);
txt2.setBounds(560, 100, 130, 40);
txt3.setBounds(250, 160, 450, 280);
b1.setBounds(150, 100, 80, 40);
b2.setBounds(150, 200, 80, 40);
b3.setBounds(150, 300, 80, 40);
b4.setBounds(150, 400, 80, 40);
b5.setBounds(259, 100, 80, 40);
b6.setBounds(479, 100, 80, 40);
b7.setBounds(400, 500, 80, 40);
n1.add(b1);
n1.add(b2);
n1.add(b3);
n1.add(b4);
n1.add(b5);
n1.add(b6);
n1.add(b7);
n1.add(txt1);
n1.add(txt2);
n1.add(txt3);
n1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
n1.setVisible(true);
// 插入操作
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school"
+ "?characterEncoding=utf-8",
"root", "0");
java.sql.Statement stmt = null;
stmt = conn.createStatement();
int rs = stmt.executeUpdate("insert stu values(" + txt1.getText() + ",'" + txt2.getText() + "');");
} catch (Exception eb) {
eb.getMessage();
} finally {
// txt1.append("插入成功");
JOptionPane.showMessageDialog(null, "插入成功");
}
}
});
// 刪除操作
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/school", "root", "0");
java.sql.Statement stmt = null;
stmt = conn.createStatement();
int rs = stmt.executeUpdate("delete from stu where id=" + txt1.getText() + ";");
} catch (Exception et) {
et.getMessage();
} finally {
// txt1.append("刪除成功");
JOptionPane.showMessageDialog(null, "刪除成功");
}
}
});
// 修改操作
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/school?characterEncoding=utf-8",
"root", "0");
java.sql.Statement stmt = null;
stmt = conn.createStatement();
int rs = stmt.executeUpdate(
"update stu set name='" + txt2.getText() + "' where id='" + txt1.getText() + "';");
} catch (Exception er) {
} finally {
// txt1.append("修改成功");
JOptionPane.showMessageDialog(null, "修改成功");
}
}
});
// 查詢操作
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/school", "root", "0");
java.sql.Statement stmt = null;
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from stu");
while (rs.next()) {
txt3.append(rs.getString("id") + " ");
txt3.append(rs.getString("name") + "\n");
}
} catch (Exception ex) {
ex.getMessage();
} finally {
// txt1.append("查詢成功")
JOptionPane.showMessageDialog(null, "查詢成功");
try {
if (rs != null) {
rs.close(); // 關閉ResultSet對象
}
} catch (SQLException ey) {
ey.printStackTrace();
}
}
}
});
// 刷新按鈕
b7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
txt3.setText("");
txt3.setText("");
txt1.setText("");
txt2.setText("");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/school", "root", "0");
java.sql.Statement stmt = null;
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from stu");
while (rs.next()) {
txt3.append(rs.getString("id") + " ");
txt3.append(rs.getString("name") + "\n");
}
} catch (Exception ex) {
ex.getMessage();
} finally {
// txt1.append("查詢成功")
// JOptionPane.showMessageDialog(null, "查詢成功");
}
}
});
}
}