1.准备环境
win7 eclipse sql2008sever
连接数据库需要jar包,上篇文章已写
2.定义模块
(1)StuManage:主界面
(2)StuModel:数据模型
(3)StuAddDialog:添加学生信息模块
(4)StuUpDialog:修改学生信息模块
3. 模块源码
StuManage源码:
package cn.test.StuManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
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;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.mysql.jdbc.PreparedStatement;
/**
*
* @author lihua
*学生管理系统 mini1
*1.查询
*2.添加
*/
public class StuMange extends JFrame implements ActionListener{
//定义组件
JPanel jp1,jp2;
JLabel jl1;
JButton jb1,jb2,jb3,jb4;
JTable jt;
JScrollPane jsp;
JTextField jtf;
StuModel sm;
//定义操作数据需要的组件
java.sql.PreparedStatement ps=null;
Connection ct;
ResultSet rs=null;
public static void main(String[] args) {
try {
//将当前窗体的外观设置为所在操作系统的外观
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO: handle exception
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new StuMange();
}
//构造函数
public StuMange() {
// TODO Auto-generated constructor stub
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中
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
//创建一个数据对象模型
sm=new StuModel();
//初始化JTabel
jt=new JTable(sm);
//初始化jsp
jsp=new JScrollPane(jt);
//将jsp放入jframe
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) {
// TODO Auto-generated method stub
if (e.getSource()==jb1) {
//因为把对表的数据封装到StuModel中,我们就可以比较简单的完成查询
String name =this.jtf.getText();
//写一个SQL语句
String sql ="select * from stu where stuName='"+name+"'";
//构建新的数据模型类,并更新
sm=new StuModel(sql);
//更新JTabel
jt.setModel(sm);
}
//用户点击添加时
else if (e.getSource()==jb2) {
StuAddDialog sa=new StuAddDialog(this, "添加学生信息", true);
//重新再获得数据模型
//构建新的数据类型,并更新
sm =new StuModel();
//更新JTable
jt.setModel(sm);
}
//用户修改数据
else if (e.getSource()==jb3) {
int rowNum=this.jt.getSelectedRow();
if (rowNum==-1) {
//提示
JOptionPane.showMessageDialog(this, "请选择一行", "提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
//显示修改对话框
new StuUpDialog(this, "修改用户信息",true , sm, rowNum);
//更新数据模型
sm=new StuModel();
//更新JTable
jt.setModel(sm);
//用户点击删除时,删除一条选中的数据
}
else if (e.getSource()==jb4) {
//1.得到学生的ID号
//getSelectedRow会返回用户点中得行
//如果该用户一行都没有选择,就会返回-1
int rowNum=this.jt.getSelectedRow();
if (rowNum==-1) {
//提示
JOptionPane.showMessageDialog(this, "请选择一行", "提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
//得到学生编号
String stuId=(String)sm.getValueAt(rowNum, 0);
//连接数据库,完成删除任务
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.得到连接
ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433", "sa", "123456");
ps=ct.prepareStatement("delete from stu where stuid=?");
ps.setString(1, stuId);
ps.executeUpdate();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}finally {
try {
if (rs!=null) {
rs.close();
}
if (ps!=null) {
ps.close();
}
if (ct!=null) {
ct.close();
}
} catch (SQLException e1) {
// TODO: handle exception
e1.printStackTrace();
}
}
//更新数据模型
sm =new StuModel();
//更新JTable
jt.setModel(sm);
}
}
}
StuModel源码:
package cn.test.StuManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
import com.mysql.jdbc.RowData;
public class StuModel extends AbstractTableModel{
//rowData用来存放行数据,columNames存放列名
Vector rowData,columnNames;
//定义操作数据库需要的组件
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
public void init(String sql ) {
if (sql==""||sql.equals(null)) {
sql="select * from stu";
}
//中间
columnNames=new Vector<>();
//设置列名
columnNames.add("学号");
columnNames.add("名字");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("籍贯");
columnNames.add("系别");
rowData=new Vector<>();
//rowData可以存放多行
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.得到连接
ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433", "sa", "123456");
ps=ct.prepareStatement(sql);
rs=ps.executeQuery();
while (rs.next()) {
Vector hang=new Vector<>();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
hang.add(rs.getString(5));
hang.add(rs.getString(6));
//加入rowData
rowData.add(hang);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if (rs!=null) {
rs.close();
}
if (ps!=null) {
ps.close();
}
if (ct!=null) {
ct.close();
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
//构造函数,用于初始化我们的数据模型
public StuModel(String sql) {
// TODO Auto-generated constructor stub
this.init(sql);
}
//构造函数
public StuModel() {
// TODO Auto-generated constructor stub
this.init("");
}
//得到共有多少列
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return this.columnNames.size();
}
@Override
public String getColumnName(int column) {
// TODO Auto-generated method stub
return (String)this.columnNames.get(column);
}
//得到共有多少行
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return this.rowData.size();
}
//得到某行某列的数据
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return ((Vector)this.rowData.get(rowIndex)).get(columnIndex);
}
}
StuAddDialog源码;
package cn.test.StuManager;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class StuAddDialog extends JDialog implements ActionListener{
//定义需要的Swing组件
JLabel jl1,jl2,jl3,jl4,jl5,jl6;
JButton jb1,jb2;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;
JPanel jp1,jp2,jp3;
//owener它的父窗口;title窗口名;model指定是模态窗口,还是非模态
public StuAddDialog(Frame owner,String title,boolean modal) {
// TODO Auto-generated constructor stub
super(owner,title,modal);
jl1=new JLabel("学号");
jl2=new JLabel("名字");
jl3=new JLabel("性别");
jl4=new JLabel("年龄");
jl5=new JLabel("籍贯");
jl6=new JLabel("系别");
jtf1=new JTextField();
jtf2=new JTextField();
jtf3=new JTextField();
jtf4=new JTextField();
jtf5=new JTextField();
jtf6=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(jtf1);
jp2.add(jtf2);
jp2.add(jtf3);
jp2.add(jtf4);
jp2.add(jtf5);
jp2.add(jtf6);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1,BorderLayout.WEST);
this.add(jp2,BorderLayout.CENTER);
this.add(jp3,BorderLayout.SOUTH);
jb1.addActionListener(this);
jb2.addActionListener(this);
//展现
this.setSize(300, 250);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//用户点击添加按钮后的响应动作
if (e.getSource()==jb1) {
//连接数据库
Connection ct=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement ps=null;
//连接数据库
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433", "sa","123456");
String strsql="insert into stu values(?,?,?,?,?,?)";
ps=ct.prepareStatement(strsql);
ps.setString(1, jtf1.getText());
ps.setString(2, jtf2.getText());
ps.setString(4, jtf3.getText());
ps.setInt(3, Integer.parseInt(jtf4.getText()));
ps.setString(5, jtf5.getText());
ps.setString(6, jtf6.getText());
ps.executeUpdate();
this.dispose();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}finally {
try {
if (ps!=null) {
ps.close();
}
if (ct!=null) {
ct.close();
}
} catch (SQLException e1) {
// TODO: handle exception'
e1.printStackTrace();
}
}
}
else if (e.getSource()==jb2) {
this.dispose();
}
}
}
StuUpDialog源码:
package cn.test.StuManager;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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{
//定义需要的swing组件
JLabel jl1,jl2,jl3,jl4,jl5,jl6;
JButton jb1,jb2;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;
JPanel jp1,jp2,jp3;
//owner它的父窗口;title窗口名,modal指定是模态窗口,还是非模态窗口
public StuUpDialog(Frame owner,String title,boolean modal,StuModel sm,int rowNum) {
// TODO Auto-generated constructor stub
super(owner, title, modal);//调用父类构造方法,达到模式对话框的效果
jl1=new JLabel("学号");
jl2=new JLabel("名字");
jl3=new JLabel("性别");
jl4=new JLabel("年龄");
jl5=new JLabel("籍贯");
jl6=new JLabel("系列");
jtf1=new JTextField();
//初始化数据
jtf1.setText((String)sm.getValueAt(rowNum, 0));
//让jtf1不能修改
jtf1.setEditable(false);
jtf2=new JTextField();
jtf2.setText((String)sm.getValueAt(rowNum, 1));
jtf3=new JTextField();
jtf3.setText((String)sm.getValueAt(rowNum, 2));
jtf4=new JTextField();
jtf4.setText(sm.getValueAt(rowNum, 3).toString());
jtf5=new JTextField();
jtf5.setText((String)sm.getValueAt(rowNum, 4));
jtf6=new JTextField();
jtf6.setText((String)sm.getValueAt(rowNum, 5));
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(jtf1);
jp2.add(jtf2);
jp2.add(jtf3);
jp2.add(jtf4);
jp2.add(jtf5);
jp2.add(jtf6);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1,BorderLayout.WEST);
this.add(jp2,BorderLayout.CENTER);
this.add(jp3,BorderLayout.SOUTH);
jb1.addActionListener(this);
jb2.addActionListener(this);
//展现
this.setSize(300, 250);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//用户点击添加按钮后的响应动作
if (e.getSource()==jb1) {
//连接数据库
Connection ct=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement ps=null;
//连接数据库
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433", "sa","123456");
String strsql="update stu set stuName=?,stuSex=?,stuAge=?,stuJg=?,stuDept=? where stuId=?";
ps=ct.prepareStatement(strsql);
ps.setString(1, jtf2.getText());
ps.setString(2, jtf3.getText());
ps.setInt(3, Integer.parseInt(jtf4.getText()));
ps.setString(4, jtf5.getText());
ps.setString(5, jtf6.getText());
ps.setString(6, jtf1.getText());
ps.executeUpdate();
this.dispose();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}finally {
try {
if (ps!=null) {
ps.close();
}
if (ct!=null) {
ct.close();
}
} catch (SQLException e1) {
// TODO: handle exception'
e1.printStackTrace();
}
}
}
else if (e.getSource()==jb2) {
this.dispose();
}
}
}