主界面:
/*
* mini 版学生管理系统
* 1. 查询
* 2. 添加
* 3. 删除
* 4. 修改
*/
package model2;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class StuManage extends JFrame implements ActionListener{
JPanel jp1, jp2;
JLabel jl1;
JButton jb1, jb2, jb3, jb4;
JTable jt = null;
JScrollPane jsp = null;
JTextField jtf;
JTable jt2 = null;
JScrollPane jsp2 = null;
Connection conn = null;
PreparedStatement ps = null;
StuModel stu;
public static void main(String[] args) {
// TODO Auto-generated method stub
StuManage demo = new StuManage();
}
public StuManage(){
jp1 = new JPanel();
jtf = new JTextField(10);
jb1 = new JButton("查询");
jb1.addActionListener(this);
jl1 = new JLabel("请输入名字");
jp1.add(jl1);
jp1.add(jtf);
jp1.add(jb1);
jp2 = new JPanel();
jb2 = new JButton("添加");
jb2.addActionListener(this);
jb3 = new JButton("修改");
jb3.addActionListener(this);
jb4 = new JButton("删除");
jb4.addActionListener(this);
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
StuModel stu = new StuModel();
String[] paras = {"1"};
stu.queryStu("select * from stu where 1=?", paras);
jt = new JTable(stu);
jsp = new JScrollPane(jt);
this.add(jsp);
this.add(jp1, "North");
this.add(jp2, "South");
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jb1){
String name = this.jtf.getText().trim();
String sql = "select * from stu where stuName=?";
String[] paras = {name};
stu = new StuModel();
stu.queryStu(sql, paras);
jt.setModel(stu);
}else if(e.getSource() == jb2){
StuAddDialog sa = new StuAddDialog(this, "添加", true);
stu = new StuModel();
String[] paras = {"1"};
stu.queryStu("select * from stu where 1=?", paras);
jt.setModel(stu);
}else if(e.getSource() == jb3){
int rowNum = this.jt.getSelectedRow();
if(rowNum == -1){
JOptionPane.showMessageDialog(this, "请选择一行");
return;
}
stu = new StuModel();
String[] paras = {"1"};
stu.queryStu("select * from stu where 1=?", paras);
new StuUpDialog(this, "修改", true, stu, rowNum);
String[] paras2 = {"1"};
stu.queryStu("select * from stu where 1=?", paras2);
jt.setModel(stu);
}else if(e.getSource() == jb4){
//返回用户点击的行,如果用户哪行都没有选,就返回-1
int rowNum = this.jt.getSelectedRow();
if(rowNum == -1){
JOptionPane.showMessageDialog(this, "请选择一行");
return;
}
stu = new StuModel();
String[] paras = {"1"};
stu.queryStu("select * from stu where 1=?", paras);
StuModel tmp = new StuModel();
String id = (String)stu.getValueAt(rowNum, 0);
System.out.println("id" + id);
String sql = "delete from stu where stuId=?";
String[] paras2 = {id};
tmp.upStu(sql, paras2);
stu = new StuModel();
String[] paras1 = {"1"};
stu.queryStu("select * from stu where 1=?", paras1);
jt.setModel(stu);
}
}
}
/*
* stu表的模型
*/
package model2;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
public class StuModel extends AbstractTableModel{
Vector rowData, columnNames;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return this.rowData.size();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return this.columnNames.size();
}
//得到某行某列的数据
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return ((Vector)this.rowData.get(rowIndex)).get(columnIndex);
}
@Override
public String getColumnName(int column) {
// TODO Auto-generated method stub
return (String)this.columnNames.get(column);
}
// //初始化数据模型
// public StuModel(){
//
// }
//查询的本质就是初始化
public void queryStu(String sql, String[] paras) {
SqlHelper sqlHelper = null;
columnNames = new Vector();
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("籍贯");
columnNames.add("所在系");
rowData = new Vector();
try {
sqlHelper = new SqlHelper();
rs = sqlHelper.queryExt(sql, paras);
while(rs.next()){
Vector hang = new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getInt(3));
hang.add(rs.getString(4));
hang.add(rs.getString(5));
hang.add(rs.getString(6));
rowData.add(hang);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sqlHelper.close();
}
}
//更新学生(增删改)
public boolean upStu(String sql, String[] paras){
SqlHelper sqlHelper = new SqlHelper();
return sqlHelper.upExecute(sql, paras);
}
}
添加学生界面:
package model2;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StuAddDialog extends JDialog implements ActionListener{
JPanel jp1, jp2, jp3;
JLabel jl1, jl2, jl3, jl4, jl5, jl6;
JButton jb1, jb2, jb3, jb4, jb5;
JTextField jf1, jf2, jf3, jf4, jf5, jf6;
//owner父窗口, model表示是不是模式窗口
public StuAddDialog(Frame owner, String title, boolean modal){
super(owner, title, modal);
jl1 = new JLabel("学号");
jl2 = new JLabel("姓名");
jl3 = new JLabel("年龄");
jl4 = new JLabel("性别");
jl5 = new JLabel("籍贯");
jl6 = new JLabel("系别");
jf1 = new JTextField();
jf2 = new JTextField();
jf3 = new JTextField();
jf4 = new JTextField();
jf5 = new JTextField();
jf6 = new JTextField();
jb1 = new JButton("添加");
jb2 = new JButton("取消");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
//设置布局
jp1.setLayout(new GridLayout(6, 1));
jp2.setLayout(new GridLayout(6, 1));
jp1.add(jl1);
jp1.add(jl2);
jp1.add(jl3);
jp1.add(jl4);
jp1.add(jl5);
jp1.add(jl6);
jp2.add(jf1);
jp2.add(jf2);
jp2.add(jf3);
jp2.add(jf4);
jp2.add(jf5);
jp2.add(jf6);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1, BorderLayout.WEST);
this.add(jp2, BorderLayout.CENTER);
this.add(jp3, BorderLayout.SOUTH);
jb1.addActionListener(this);
this.setSize(300, 250);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jb1){
StuModel tmp = new StuModel();
String sql = "insert into stu values(?, ?, ?, ?, ?, ?)";
String[] paras = {jf1.getText(),jf2.getText(),jf3.getText(),jf4.getText(),jf5.getText(),jf6.getText()};
if(!tmp.upStu(sql, paras)){
JOptionPane.showMessageDialog(this, "添加失败");
}
this.dispose();
}
}
}
package model2;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class StuUpDialog extends JDialog implements ActionListener{
JPanel jp1, jp2, jp3;
JLabel jl1, jl2, jl3, jl4, jl5, jl6;
JButton jb1, jb2, jb3, jb4, jb5;
JTextField jf1, jf2, jf3, jf4, jf5, jf6;
//owner父窗口, model表示是不是模式窗口
public StuUpDialog(Frame owner, String title, boolean modal, StuModel stu, int rowNum){
super(owner, title, modal);
jl1 = new JLabel("学号");
jl2 = new JLabel("姓名");
jl3 = new JLabel("年龄");
jl4 = new JLabel("性别");
jl5 = new JLabel("籍贯");
jl6 = new JLabel("系别");
jf1 = new JTextField();
System.out.println(jf1.toString());
jf1.setText((String)stu.getValueAt(rowNum, 0));
System.out.println(jf1.toString());
jf1.setEditable(false);
jf2 = new JTextField();
jf2.setText((String)stu.getValueAt(rowNum, 1));
jf3 = new JTextField();
jf3.setText(stu.getValueAt(rowNum, 2).toString());
jf4 = new JTextField();
jf4.setText((String)stu.getValueAt(rowNum, 3));
jf5 = new JTextField();
jf5.setText((String)stu.getValueAt(rowNum, 4));
jf6 = new JTextField();
jf6.setText((String)stu.getValueAt(rowNum, 5));
jb1 = new JButton("添加");
jb1.addActionListener(this);
jb2 = new JButton("取消");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
//设置布局
jp1.setLayout(new GridLayout(6, 1));
jp2.setLayout(new GridLayout(6, 1));
jp1.add(jl1);
jp1.add(jl2);
jp1.add(jl3);
jp1.add(jl4);
jp1.add(jl5);
jp1.add(jl6);
jp2.add(jf1);
jp2.add(jf2);
jp2.add(jf3);
jp2.add(jf4);
jp2.add(jf5);
jp2.add(jf6);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1, BorderLayout.WEST);
this.add(jp2, BorderLayout.CENTER);
this.add(jp3, BorderLayout.SOUTH);
this.setSize(300, 250);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jb1){
String sql = "update stu set stuName=?, stuAge=?, stuSex=?, stuJg=?, stuDept=? where stuId=?";
String[] paras = {jf2.getText(),jf3.getText(),jf4.getText(),jf5.getText(),jf6.getText(),jf1.getText()};
StuModel tmp = new StuModel();
tmp.upStu(sql, paras);
this.dispose();
}
}
}
连接数据库:
package model2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SqlHelper {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
String url = "jdbc:mysql://localhost:3306/stu";
String user = "root";
String passwd = "123456";
String driver = "com.mysql.jdbc.Driver";
public SqlHelper(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet queryExt(String sql, String[] paras){
try {
ps = conn.prepareStatement(sql);
for(int i=0; i