1. 程式人生 > >GUI JDBC 學生資訊管理系統

GUI JDBC 學生資訊管理系統

1.Driver.java

public class Driver {

public static void main(String[] args) {

Denglu dl = new Denglu();

dl.view();

}

}

2.Denglu.java

public class Denglu implements KeyListener{

private JFrame f = new JFrame("登入");

private JTextField text1 = new JTextField(10);

private JPasswordField text2 = new JPasswordField(10);

private JLabel infor = new JLabel();

private JButton bt1 = new JButton("登入");

private JButton bt2 = new JButton("重置");

public void view() {

f.setLayout(new BorderLayout(3, 4));

JPanel p1 = new JPanel();

JPanel p2 = new JPanel();

JPanel p3 = new JPanel();

f.add(p1, BorderLayout.NORTH);

f.add(p2, BorderLayout.CENTER);

f.add(p3, BorderLayout.SOUTH);

JLabel l1 = new JLabel("使用者登入");

l1.setLayout(new FlowLayout(FlowLayout.CENTER));

p1.add(l1);

p2.setLayout(new GridLayout(3, 2));

JPanel p4 = new JPanel();

JPanel p5 = new JPanel();

JPanel p6 = new JPanel();

p2.add(p4);

p2.add(p5);

p2.add(p6);

p4.setLayout(new FlowLayout(FlowLayout.CENTER));

p5.setLayout(new FlowLayout(FlowLayout.CENTER));

p6.setLayout(new FlowLayout(FlowLayout.CENTER));

JLabel username = new JLabel("使用者名稱:");

p4.add(username);

p4.add(text1);

JLabel password = new JLabel("密    碼:");

p5.add(password);

p5.add(text2);

p6.add(infor);

p3.setLayout(new FlowLayout(FlowLayout.CENTER));

p3.add(bt1);

p3.add(bt2);

bt1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

check();

}

});

bt2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

text1.setText(null);

text2.setText(null);

}

});

text2.requestFocus();

text2.addKeyListener(this);

f.setSize(300, 200);

f.setLocation(500, 170);

f.setVisible(true);

}

public void check() {

boolean ret = false;

String s = new String(text2.getPassword());

String sql = "select * from t_users ";

sql = sql + "where userName='" + text1.getText();

sql = sql + "' and userPassword='" + s + "'";

DbManager db = new DbManager();

ResultSet rs = db.executeQuery(sql);

try {

if (rs.next())

ret = true;

else

ret = false;

} catch (SQLException e1) {

e1.printStackTrace();

ret = false;

}

db.close();

if (ret) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Index frame = new Index();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

f.dispose();

              System.out.println("使用者名稱:" + text1.getText() + "   密碼:" + s);

} else {

infor.setText("使用者名稱或密碼錯誤!!");

text1.setText(null);

text2.setText(null);

}

}

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_ENTER){

check();

}

}

     public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}

}

3.Index.java

public class Index extends JFrame {

private JPanel contentPane;

public Index() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 631, 445);

setLocation(360, 160);

getContentPane().setLayout(null);

JLabel l1 = new JLabel("使用者登入");

l1.setLayout(new FlowLayout(FlowLayout.CENTER));

getContentPane().add(l1);

JButton btnNewButton = new JButton("新增");

btnNewButton.setBounds(76, 210, 93, 35);

getContentPane().add(btnNewButton);

JButton btnNewButton_1 = new JButton("刪除");

btnNewButton_1.setBounds(198, 210, 93, 35);

getContentPane().add(btnNewButton_1);

JButton btnNewButton_2 = new JButton("修改");

btnNewButton_2.setBounds(322, 210, 93, 35);

getContentPane().add(btnNewButton_2);

JButton btnNewButton_3 = new JButton("查詢");

btnNewButton_3.setBounds(447, 210, 93, 35);

getContentPane().add(btnNewButton_3);

JLabel label = new JLabel("歡迎進入學生管理系統");

label.setFont(new Font("黑體", Font.BOLD,20));

label.setBounds(205, 53, 210, 35);

getContentPane().add(label);

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Import it = new Import();

it.daoru();   

}

});

btnNewButton_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Delete d = new Delete();

d.delete();   

}

});

btnNewButton_2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Revise r = new Revise();

r.revise();

}

});

btnNewButton_3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

SearchBy s = new SearchBy();

s.searchBy();

}

});

}

}

4.Import.java

public class Import {

private JFrame f = new JFrame("匯入學生資訊");

private JTextField text = new JTextField(5);

private JTextField text1 = new JTextField(5);

private JRadioButton rb1 = new JRadioButton("男");

    private JRadioButton rb2 = new JRadioButton("女");

    private JComboBox jcb;

private JComboBox jcb1;

private JComboBox jcb3;

private JComboBox jc;

    private JCheckBox cb1 = new JCheckBox("籃球");

private JCheckBox cb2 = new JCheckBox("羽毛球");

private JCheckBox cb3 = new JCheckBox("排球");

public void daoru() {

f.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 30));

JLabel lname = new JLabel("姓名:");

f.add(lname);

f.add(text);

JLabel lid = new JLabel("學號:");

f.add(lid);

f.add(text1);

JLabel lgender = new JLabel("性別:");

f.add(lgender);

f.add(rb1);

f.add(rb2);

ButtonGroup group = new ButtonGroup();

group.add(rb1);

group.add(rb2);

JLabel lbirth = new JLabel("出生日期:");

f.add(lbirth);

String year[] = new String[30];

year[0] = "";

for (int i = 1; i <= 29; i++) {

year[i] = " " + (i + 1982);

if (i == 2012)

break;

}

jcb = new JComboBox(year);

f.add(jcb);

JLabel lb1 = new JLabel("年");

f.add(lb1);

String month[] = new String[13];

month[0] = "";

for (int i = 1; i <= 12; i++) {

month[i] = "" + i;

if (i == 12)

break;

}

jcb1 = new JComboBox(month);

f.add(jcb1);

JLabel lb2 = new JLabel("月");

f.add(lb2);

String day[] = new String[32];

day[0] = "";

for (int i = 1; i <= 31; i++) {

day[i] = "" + i;

// 或者寫成day[i] = Integer.toString(i);

if (i == 31)

break;

}

jcb3 = new JComboBox(day);

f.add(jcb3);

JLabel lb3 = new JLabel("日");

f.add(lb3);

String province[] = { "", "湖北", "湖南", "廣東", "陝西", "江西", "福建", "河南","山東" };

jc = new JComboBox(province);

jc.setBorder(BorderFactory.createTitledBorder("省份"));

f.add(jc);

JLabel lintrest = new JLabel("愛好:");

f.add(lintrest);

f.add(cb1);

f.add(cb2);

f.add(cb3);

JButton bt = new JButton("新增");

f.add(bt);

bt.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

insertStudent();

}       

});

f.setSize(500, 450);

f.setLocation(200, 200);

f.setVisible(true);

}

public void insertStudent(){

DbManager db = new DbManager();

String sql = "select * from t_student where id='"

+ text1.getText() + "'";

ResultSet rs = db.executeQuery(sql);

String s1 = "";

try {

if (rs.next()) {

JOptionPane.showMessageDialog(null, "學號為"

+ text1.getText() + "的學生已存在!!", null,

JOptionPane.ERROR_MESSAGE);

} else {

s1 = new String(text1.getText());// 學號

String s2 = new String(text.getText());// 姓名

String s3 = null; // 性別

if (rb1.isSelected())

s3 = "男";

if (rb2.isSelected())

s3 = "女";

String s4 = jcb.getSelectedItem().toString() + "-";

s4 = s4 + jcb1.getSelectedItem().toString() + "-";

s4 = s4 + jcb3.getSelectedItem().toString();

String s5 = jc.getSelectedItem().toString();

String s6 = "";

if (cb1.isSelected())

s6 = cb1.getText();

if (cb2.isSelected())

s6 = s6 + " , " + cb2.getText();

if (cb3.isSelected())

s6 = s6 + " , " + cb3.getText();

if (!text1.getText().isEmpty()

&& !text.getText().isEmpty()) {

String sql1 = "insert into t_student values(";

sql1 = sql1 + "'" + s1 + "',";

sql1 = sql1 + "'" + s2 + "',";

sql1 = sql1 + "'" + s3 + "',";

sql1 = sql1 + "'" + s4 + "',";

sql1 = sql1 + "'" + s5 + "',";

sql1 = sql1 + "'" + s6 + "')";

boolean ret = db.executeUpdate(sql1);

if (ret)

JOptionPane.showMessageDialog(null, "新增成功!",

null, JOptionPane.OK_CANCEL_OPTION);

    f.dispose();

} else

JOptionPane.showMessageDialog(null, "姓名和學號不能為空!",

null, JOptionPane.ERROR_MESSAGE);

}

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

db.close();

}

}

5.Delete.java

public class Delete implements KeyListener {

private JTextField t1 = new JTextField(5);

private JTextField t2 = new JTextField(5);

public void delete() {

JFrame f = new JFrame("刪除學生");

f.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 30));

JLabel l1 = new JLabel("學號:");

f.add(l1);

f.add(t1);

JLabel l2 = new JLabel("姓名:");

f.add(l2);

f.add(t2);

JButton bt = new JButton("刪除");

f.add(bt);

bt.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

deleteby();

}

});

f.setSize(400, 180);

f.setLocation(380, 210);

f.setVisible(true);

}

public void deleteby() {

boolean ret = false;

DbManager db = new DbManager();

if (!t1.getText().isEmpty() && t2.getText().isEmpty()) {

String sql = "select * from t_student ";

sql = sql + "where id= '" + t1.getText() + "'";

ResultSet rs = db.executeQuery(sql);

try {

if (rs.next()) {

String sql1 = "delete from t_student where id='"

+ t1.getText() + "'";

ret = db.executeUpdate(sql1);

} else

JOptionPane.showMessageDialog(null, "學號為" + t1.getText()

+ "的資料不在資料庫中!", null, JOptionPane.ERROR_MESSAGE);

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

if (ret) {

JOptionPane.showMessageDialog(null, "刪除成功!", null,

JOptionPane.OK_OPTION);

}

}

if (!t2.getText().isEmpty() && t1.getText().isEmpty()) {

String sql = "select * from t_student ";

sql = sql + "where name= '" + t2.getText() + "'";

ResultSet rs = db.executeQuery(sql);

try {

if (rs.next()) {

String sql1 = "delete from t_student where name='"

+ t2.getText() + "'";

ret = db.executeUpdate(sql1);

} else

JOptionPane.showMessageDialog(null, "姓名為" + t2.getText(+ "的資料不在資料庫中!", null, JOptionPane.ERROR_MESSAGE);

} catch (SQLException e1) {

e1.printStackTrace();

}

if (ret) {

JOptionPane.showMessageDialog(null, "刪除成功!", null,

JOptionPane.OK_CANCEL_OPTION);

}

}

if (t1.getText().isEmpty() && t2.getText().isEmpty())

JOptionPane.showMessageDialog(null, "資訊不能為空!", null,

JOptionPane.ERROR_MESSAGE);

db.close();

}

public void keyPressed(KeyEvent e) {

// TODO Auto-generated method stub

if (e.getKeyCode() == KeyEvent.VK_ENTER) {

deleteby();

}

}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}

}

6.Revise.java

public class Revise {

private JTextField text = new JTextField(5);

private JRadioButton rb1 = new JRadioButton("男");

    private JRadioButton rb2 = new JRadioButton("女");

    private JComboBox jcb;

private JComboBox jcb1;

private JComboBox jcb3;

private JComboBox jc;

    private JCheckBox cb1 = new JCheckBox("籃球");

private JCheckBox cb2 = new JCheckBox("羽毛球");

private JCheckBox cb3 = new JCheckBox("排球");

public void revise() {

JFrame f = new JFrame("修改學生資訊");

f.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 40));

JLabel l1 = new JLabel("學號:");

final JTextField t1 = new JTextField(5);

f.add(l1);

f.add(t1);

JButton bt = new JButton("修改");

f.add(bt);

bt.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String id = t1.getText();

if (!t1.getText().isEmpty()) {

DbManager db = new DbManager();

String sql = "select * from t_student where id ='" + id+ "'";

ResultSet rs = db.executeQuery(sql);

try {

if (rs.next())

SaveStudent(id);

else

                        JOptionPane.showMessageDialog(null, "要修改的資料不存在!", null, JOptionPane.ERROR_MESSAGE);

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

} else

JOptionPane.showMessageDialog(null, "資訊不能為空!", null,JOptionPane.OK_OPTION);

}

});

f.setSize(300, 150);

//f.getContentPane().setBackground(Color.GREEN);

f.setLocation(300, 250);

f.setVisible(true);

}

public void SaveStudent(final String id) {

JFrame f = new JFrame("輸入學生資訊");

f.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 60));

JLabel lname = new JLabel("姓名:");

f.add(lname);

f.add(text);

JLabel lgender = new JLabel("性別:");

f.add(lgender);

f.add(rb1);

f.add(rb2);

ButtonGroup group = new ButtonGroup();

group.add(rb1);

group.add(rb2);

JLabel lbirth = new JLabel("出生日期:");

f.add(lbirth);

String year[] = new String[30];

year[0] = "";

for (int i = 1; i <= 29; i++) {

year[i] = " " + (i + 1982);

if (i == 2012)

break;

}

jcb = new JComboBox(year);

f.add(jcb);

JLabel lb1 = new JLabel("年");

f.add(lb1);

String month[] = new String[13];

month[0] = "";

for (int i = 1; i <= 12; i++) {

month[i] = "" + i;

if (i == 12)

break;

}

jcb1 = new JComboBox(month);

f.add(jcb1);

JLabel lb2 = new JLabel("月");

f.add(lb2);

String day[] = new String[32];

day[0] = "";

for (int i = 1; i <= 31; i++) {

day[i] = "" + i;

if (i == 31)

break;

}

jcb3 = new JComboBox(day);

f.add(jcb3);

JLabel lb3 = new JLabel("日");

f.add(lb3);

String province[] = { "", "湖北", "湖南", "廣東", "陝西", "江西", "福建", "河南","山東" };

jc = new JComboBox(province);

jc.setBorder(BorderFactory.createTitledBorder("省份"));

f.add(jc);

JLabel lintrest = new JLabel("愛好:");

cb1 = new JCheckBox("籃球");

cb2 = new JCheckBox("羽毛球");

cb3 = new JCheckBox("排球");

f.add(lintrest);

f.add(cb1);

f.add(cb2);

f.add(cb3);

JButton bt = new JButton("儲存修改");

f.add(bt);

bt.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

reviseStu(id);

}

});

f.setSize(700, 380);

f.setLocation(310, 180);

f.setVisible(true);

}

public void reviseStu(String id){

DbManager db = new DbManager();

boolean ret = false;

String s1 = new String(text.getText());

String s2 = "";

if (rb1.isSelected())

s2 = "男";

if (rb2.isSelected())

s2 = "女";

String s3 = "";

s3 = jcb.getSelectedItem().toString() + "-";

s3 = s3 + jcb1.getSelectedItem().toString() + "-";

s3 = s3 + jcb3.getSelectedItem().toString();

String s4 = jc.getSelectedItem().toString();

String s5 = "";

if (cb1.isSelected())

s5 = cb1.getText();

if (cb2.isSelected())

s5 = s5 + " , " + cb2.getText();

if (cb3.isSelected())

s5 = s5 + " , " + cb3.getText();

String sql = "update t_student set ";

sql = sql + " Id='" + id + "',";

sql = sql + " name='" + s1 + "',";

sql = sql + " gender='" + s2 + "',";

sql = sql + " birth='" + s3 + "',";

sql = sql + " province='" + s4 + "',";

sql = sql + " interest='" + s5 + "'";

sql = sql + " where id='" + id + "'";

ret = db.executeUpdate(sql);

if (ret) {

System.out.println(sql);

JOptionPane.showMessageDialog(null, "修改成功!", null,

JOptionPane.OK_CANCEL_OPTION);

} else

JOptionPane.showMessageDialog(null, "請填寫完整的資訊!!", null,

JOptionPane.ERROR_MESSAGE);

db.close();

}

}

7.SearchBy.java

public class SearchBy {

private JFrame f = new JFrame("查詢學生資訊");

private JTextField t1 = new JTextField(5);

private JTextField t2 = new JTextField(5);

public void searchBy() {

f.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 30));

JLabel l1 = new JLabel("學號:");

f.add(l1);

f.add(t1);

JLabel l2 = new JLabel("姓名:");

f.add(l2);

f.add(t2);

JButton bt = new JButton("條件查詢");

f.add(bt);

bt.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

IdorName();

}

});

JButton bt1 = new JButton("顯示全部學生資訊");

f.add(bt1);

bt1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

searchAll();

}

});

f.setSize(550, 200);

f.setLocation(310, 210);

f.setVisible(true);

}

public void IdorName() {

DbManager db = new DbManager();

if (!t1.getText().isEmpty() && t2.getText().isEmpty()) {

String sql = "select * from t_student ";

sql = sql + "where id= '" + t1.getText() + "'";

ResultSet rs = db.executeQuery(sql);

try {

if (rs.next()) {

System.out.println("\n按學號查詢!");

show(sql);

f.dispose();

} else

JOptionPane.showMessageDialog(null, "學號為" + t1.getText()

+ "的資料不在資料庫中!", null, JOptionPane.ERROR_MESSAGE);

rs.close();

} catch (SQLException e1) {

e1.printStackTrace();

} catch (Exception e1) {

e1.printStackTrace();

}

}

if (!t2.getText().isEmpty() && t1.getText().isEmpty()) {

String sql = "select * from t_student ";

sql = sql + "where name= '" + t2.getText() + "'";

ResultSet rs = db.executeQuery(sql);

try {

if (rs.next()) {

System.out.println("\n按姓名查詢!");

show(sql);

f.dispose();

} else

JOptionPane.showMessageDialog(null, "姓名為" + t2.getText()

+ "的資料不在資料庫中!", null, JOptionPane.ERROR_MESSAGE);

rs.close();

} catch (SQLException e1) {

e1.printStackTrace();

} catch (Exception e1) {

e1.printStackTrace();

}

}

if (t2.getText().isEmpty() && t1.getText().isEmpty()) {

JOptionPane.showMessageDialog(null, "資訊不能為空!", null,

JOptionPane.ERROR_MESSAGE);

}

db.close();

}

public void searchAll() {

DbManager db = new DbManager();

String sql = "select * from t_student";

ResultSet rs = db.executeQuery(sql);

try {

if (rs.next()) {

show(sql);

System.out.println("顯示全部學生資訊!");

}

} catch (SQLException e1) {

e1.printStackTrace();

} catch (Exception e1) {

e1.printStackTrace();

}

db.close();

}

public void show(String sql) throws Exception {

JFrame f = new JFrame("顯示學生資訊");

System.out.println(sql);

DbManager db = new DbManager();

ResultSet rs = db.executeQuery(sql);

ArrayList<Student> list = new ArrayList<Student>();

Vector<Vector> rowData = new Vector<Vector>();

while (rs.next()) {

String s = "              ";

Student stu = new Student();

stu.setId(s + rs.getString("id"));

stu.setName(s + rs.getString("name"));

stu.setGender(s + rs.getString("gender"));

stu.setBirth(s + rs.getString("birth"));

stu.setProvince(s + rs.getString("province"));

stu.setInterest(s + rs.getString("interest"));

list.add(stu);

}

for (int i = 0; i < list.size(); i++) {

Vector<String> row = new Vector<String>();

row.add(list.get(i).getId());

row.add(list.get(i).getName());

row.add(list.get(i).getGender());

row.add(list.get(i).getBirth());

row.add(list.get(i).getProvince());

row.add(list.get(i).getInterest());

rowData.add(row);

}

rs.close();

db.close();

Vector<String> titles = new Vector<String>();

titles.add("學號");

titles.add("姓名");

titles.add("性別");

titles.add("生日");

titles.add("省份");

titles.add("愛好");

JTable jl = new JTable(rowData, titles) {

public boolean isCellEditable(int row, int col) {

return false;

}

};

MyUtil.makeFace(jl);

jl.setRowHeight(30);

JScrollPane scr = new JScrollPane(jl);

f.add(scr);

f.setSize(800, 270);

f.setLocation(310, 180);

f.setVisible(true);

}

}

8.DbManager.java

public class DbManager {

private static final String DBDRIVER = "com.mysql.jdbc.Driver";

Private static final String DBURL= "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8";

private static final String DBUSER = "root";

private static final String DBPASS = "hanpeng136";

private Connection conn = null;

private Statement stmt = null;

private ResultSet rs = null;

public DbManager() {

try {

Class.forName(DBDRIVER);

conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);

} catch (Exception e) {

e.printStackTrace();

}

}

public boolean executeUpdate(String sql)

{

boolean ret=false;

try {

stmt = conn.createStatement() ;

int i=stmt.executeUpdate(sql);

if(i>0)

ret=true;

else

ret=false;

} catch (Exception e) {

e.printStackTrace();

ret=false;

}

return ret;

}

public ResultSet executeQuery(String sql)

{

try {

stmt = conn.createStatement() ;

rs=stmt.executeQuery(sql);

} catch (Exception e) {

e.printStackTrace();

}

return rs;

}

public void close()

{

try {

if(rs!=null)

rs.close();

if(stmt!=null)

stmt.close();

if(conn!=null)

conn.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

9.Student.java

public Student(String id, String name, String gender, String birth,

String province, String interest) {

super();

this.id = id;

this.name = name;

this.gender = gender;

this.birth = birth;

this.province = province;

this.interest = interest;

}

public Student() {  }

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public String getBirth() {

return birth;

}

public void setBirth(String birth) {

this.birth = birth;

}

public String getProvince() {

return province;

}

public void setProvince(String province) {

this.province = province;

}

public String getInterest() {

return interest;

}

public void setInterest(String interest) {

this.interest = interest;

}

}