Java學生管理系統
阿新 • • 發佈:2018-11-21
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();
}
}
}
4.執行