CREATE TABLE st
( `stId` integer (255) NOT NULL AUTO_INCREMENT , `stNname` varchar (20), `st s e x` varchar (100), // s e x 被和諧了 `stAge` varchar (100), `stBorn` varchar (100), `stDept` varchar (100), PRIMARY KEY (`stId`) )TYPE=MyISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci; ================================================================================ #方法1
================================================================================ package model_1;
//InsertStudent文件源代碼: import java.sql.*;
import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.table.*; import javax.swing.event.*; //添加學(xué)生的對話框類
public class InsertStudent extends JDialog implements ActionListener{ private JLabel id, name, ***, age, address, dep; //分別代表學(xué)生的學(xué)號,,名字,,性別,年齡,,籍貫,,系別 private JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6; //用戶輸入的文本框,分別為學(xué)生的學(xué)號,,名字,,性別,年齡,,籍貫,,系別 private JButton add, canel; //分別為添加按鈕和取消按鈕 private Panel panel1, panel2, panel3; public InsertStudent(Frame owner, String title, boolean model) { super(owner, title, model); //實(shí)現(xiàn)與父類窗口對話 System.out.println("我執(zhí)行了"); //窗體的西部的組件 id = new JLabel("學(xué)號:"); name = new JLabel("姓名:"); *** = new JLabel("性別"); age = new JLabel("年齡"); address = new JLabel("籍貫"); dep = new JLabel("系別:"); //添加到panel1面板中 panel1 = new Panel(new GridLayout(6, 1)); panel1.add(id); panel1.add(name); panel1.add(***); panel1.add(age); panel1.add(address); panel1.add(dep); //窗體的東部組件 jtf1 = new JTextField(10); jtf2 = new JTextField(10); jtf3 = new JTextField(10); jtf4 = new JTextField(10); jtf5 = new JTextField(10); jtf6 = new JTextField(10); //添加到第二個面板中 panel2 = new Panel(new GridLayout(6, 1)); panel2.add(jtf1); panel2.add(jtf2); panel2.add(jtf3); panel2.add(jtf4); panel2.add(jtf5); panel2.add(jtf6); //窗體北部的控件 add = new JButton("添加"); canel = new JButton("取消"); //注冊事件監(jiān)聽 add.addActionListener(this); canel.addActionListener(this); //添加到panel3面板中 panel3 = new Panel(); panel3.add(add); panel3.add(canel); //將各個組件添加到窗體中 this.add(panel1, BorderLayout.WEST); this.add(panel2, BorderLayout.CENTER); this.add(panel3, BorderLayout.SOUTH); //設(shè)置對話框?qū)傩?br> this.setSize(300, 200); //設(shè)置對話框?yàn)榭梢?br> this.setVisible(true); } public void actionPerformed(ActionEvent e) { //數(shù)據(jù)庫 Connection con = null; PreparedStatement ps = null; boolean result = true; //記錄添加學(xué)生是否成功 if(e.getSource() == add) { try { //加載jdbc驅(qū)動 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //連接數(shù)據(jù)源 con = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:3306;databaseName=st", "feng", "1001001"); //執(zhí)行sql String sql = "insert into st values(?,?,?,?,?,?)"; ps = con.prepareStatement(sql); ps.setString(1, jtf1.getText()); ps.setString(2, jtf2.getText()); ps.setString(3, jtf3.getText()); ps.setString(4, jtf4.getText()); ps.setString(5, jtf5.getText()); ps.setString(6, jtf6.getText()); ps.executeUpdate(); //關(guān)閉對話框 this.dispose(); } catch(ClassNotFoundException e1) { result = false; //添加學(xué)生記錄失敗 e1.printStackTrace(); } catch(SQLException e1) { result = false; //添加學(xué)生記錄失敗 e1.printStackTrace(); } finally { //關(guān)閉資源 try { if(null != ps) { ps.close(); } if(null != con) { con.close(); } } catch(SQLException e1) { e1.printStackTrace(); } } if(false == result) { JOptionPane.showMessageDialog(this, "失敗信息:學(xué)號不能重復(fù),性別只能為'男'或者'女',年齡大于0"); } } else if(e.getSource() == canel) { //關(guān)閉對話框 this.dispose(); } } }
================================================================================ package model_1;
//StModel文件源代碼: import java.util.*; import java.sql.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.table.*; import javax.swing.event.*; public class StModel extends AbstractTableModel{ Vector Vector //數(shù)據(jù)庫 private Connection con = null; private PreparedStatement ps = null; private ResultSet rs = null; /**********
//driver="org.gjt.mm.mysql.Driver";
//url="jdbc:mysql://localhost:3306/st?characterEncoding=utf8";
//user="feng";
//passWord="1001001";
//以上四行在對mysql操作時,,支持linux環(huán)境。 ************/
private static final String driver="org.gjt.mm.mysql.Driver"; private static final String url="jdbc:mysql://localhost:3306/st?characterEncoding=utf8"; private static final String user="feng"; private static final String passWord="1001001"; //對數(shù)據(jù)庫進(jìn)行查找 public void insertDatabase(String sql) { columNames = new Vector rowDate = new Vector columNames.add("學(xué)號"); columNames.add("名字"); columNames.add("性別"); columNames.add("年齡"); columNames.add("籍貫"); columNames.add("系別"); try { //加載jdbc驅(qū)動 Class.forName(driver); //連接數(shù)據(jù)源 con = DriverManager.getConnection(url,user,passWord); //執(zhí)行sql ps = con.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()) { Vector row.add(rs.getString(1)); row.add(rs.getString(2)); row.add(rs.getString(3)); row.add(rs.getString(4)); row.add(rs.getString(5)); row.add(rs.getString(6)); rowDate.add(row); //將學(xué)生的一條記錄(row)添加到rowDate中 } } catch(ClassNotFoundException e) { e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } finally { //關(guān)閉數(shù)據(jù)庫資源 try { if(null != rs) { rs.close(); } if(null != ps) { ps.close(); } if(null != con) { con.close(); } } catch(SQLException e) { e.printStackTrace(); } } }
@Override //返回列數(shù) public int getColumnCount() { // TODO Auto-generated method stub return this.columNames.size(); } @Override
//返回表的行數(shù) public int getRowCount() { // TODO Auto-generated method stub return this.rowDate.size(); } @Override
//得到某行某列的數(shù)據(jù) public Object getValueAt(int rowIndex, int columnIndex) { // TODO Auto-generated method stub return ((Vector)this.rowDate.get(rowIndex)).get(columnIndex); } //設(shè)置表的字段名 public String getColumnName(int column) { return (String)this.columNames.get(column); } } ================================================================================ package model_1;
//model1模式開發(fā):
//StuMange文件源代碼: import java.util.*;
import java.sql.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; public class StuMange extends JFrame implements ActionListener {
//定義用戶界面用到的組件 JLabel userName = null; //要查詢的學(xué)生的名字 JTextField textField = null; //用戶輸入的文本框 JButton select = null; //查詢按鈕 JButton insert = null; //添加按鈕 JButton update = null; //修改按鈕 JButton delete = null; //刪除按鈕 JTable table = null; //用于顯示數(shù)據(jù)庫調(diào)出的數(shù)據(jù)的表 Panel panel1 = null; Panel panel2 = null; Panel panel3 = null; JScrollPane jscrollPane = null; StModel stModel = null; //st表的模型 private Object PreparedStatement; public static void main(String[] args) { StuMange window = new StuMange(); } public StuMange()
{ //窗口顯示的上部的組件 userName = new JLabel("學(xué)生名:"); textField = new JTextField(10); //用戶輸入文本框限定為最多輸入10個字符 select = new JButton("查詢"); //查詢按鈕 //添加到第一個面板中 panel1 = new Panel(); panel1.add(userName); panel1.add(textField); panel1.add(select); //窗口下部的組件 insert = new JButton("添加"); delete = new JButton("刪除"); update = new JButton("修改"); //添加到第三個個面板中 panel3 = new Panel(); panel3.add(insert); panel3.add(delete); panel3.add(update); //窗口中部的組件 stModel = new StModel(); stModel.insertDatabase("select * from st"); table = new JTable(stModel); jscrollPane = new JScrollPane(table); //注冊事件監(jiān)聽 select.addActionListener(this); insert.addActionListener(this); update.addActionListener(this); delete.addActionListener(this); //將各個組件添加到窗體中 this.add(panel1, BorderLayout.NORTH); this.add(jscrollPane, BorderLayout.CENTER); this.add(panel3, BorderLayout.SOUTH); //設(shè)置窗體屬性 this.setSize(400, 300); //設(shè)置窗口大小 this.setLocation(200, 200); //設(shè)置窗口顯示的位置 this.setTitle("mini學(xué)生信息管理系統(tǒng)"); //設(shè)置窗口標(biāo)題 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置窗口關(guān)閉之后其后臺程序同時關(guān)閉 //this.setResizable(false); //設(shè)置窗口不可拉伸 //設(shè)置窗口為可見 this.setVisible(true); } public void actionPerformed(ActionEvent e) { //用戶點(diǎn)擊了查找按鈕 if(e.getSource() == select) { String paras = textField.getText().trim(); stModel = new StModel(); stModel.insertDatabase("select * from st where stNname='" paras "'"); table.setModel(stModel); } //用戶點(diǎn)擊了添加按鈕 else if(e.getSource() == insert) { InsertStudent insertStudent = new InsertStudent(this, "添加學(xué)生", true); //刷新窗口表的數(shù)據(jù) stModel = new StModel(); stModel.insertDatabase("select * from st"); table.setModel(stModel); } //用戶點(diǎn)擊了修改按鈕 else if(e.getSource() == update) { int row = this.table.getSelectedRow(); //用戶選擇的行數(shù) if(-1 == row) //如果用戶沒有選中行 { JOptionPane.showMessageDialog(this, "請選擇一行"); } else { UpdateDatabase updateDatabase = new UpdateDatabase(this, "修改學(xué)生", true, stModel, row); //刷新窗口表的數(shù)據(jù) stModel =new StModel(); stModel.insertDatabase("select * from st"); table.setModel(stModel); } } //用戶點(diǎn)擊了刪除按鈕 else if(e.getSource() == delete) { //數(shù)據(jù)庫 Connection con = null; PreparedStatement ps = null; private static final String driver="org.gjt.mm.mysql.Driver"; private static final String url="jdbc:mysql://localhost:3306/st?characterEncoding=utf8"; private static final String user="feng"; private static final String passWord="1001001"; int row = this.table.getSelectedRow(); //用戶選擇的行 if(-1 == row) //用戶沒有選中行 { JOptionPane.showMessageDialog(this, "請選擇一行"); } else { try { //加載jdbc驅(qū)動 Class.forName(driver); //連接數(shù)據(jù)源 con = DriverManager.getConnection(url, user, passWord); //執(zhí)行sql String sql = "delete from st where stId=?"; String id = (String)(stModel.getValueAt(row, 0)); //獲得用戶選擇的行的學(xué)生的學(xué)號 ps = con.prepareStatement(sql); ps.setString(1, id); ps.executeUpdate(); //刷新窗口 stModel = new StModel(); stModel.insertDatabase("select * from st"); table.setModel(stModel); } catch(ClassNotFoundException e1) { e1.printStackTrace(); } catch(SQLException e1) { e1.printStackTrace(); } finally { //關(guān)閉資源 try { if(null != ps) { ps.close(); } if(null != con) { con.close(); } } catch(SQLException e1) { e1.printStackTrace(); } } } } } } ================================================================================
package model_1;
//UpdateDatabase文件源代碼 import java.sql.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.table.*;
import javax.swing.event.*; public class UpdateDatabase extends JDialog implements ActionListener{
JLabel id,name,***,age,address,dep; //分別為學(xué)生的學(xué)號,,學(xué)生的姓名,,學(xué)生的性別,學(xué)生的年齡,,學(xué)生的籍貫,,學(xué)生的系別的標(biāo)簽 JTextField jtf1, jtf2, jtf3, jtf4,jtf5, jtf6; //分別為學(xué)生的學(xué)號,姓名,,性別,,年齡,籍貫,,系列的輸入文本框 JButton update,canel; //分別為修改和取消按鈕 Panel panel1 ,panel2, panel3; public UpdateDatabase(Frame own, String title, boolean model, StModel stModel, int row) { super(own, title, model); //實(shí)現(xiàn)與父類窗口對話 //窗體西部的組件 id = new JLabel("學(xué)號"); name = new JLabel("名字"); *** = new JLabel("性別"); age = new JLabel("年齡"); address = new JLabel("地址"); dep = new JLabel("系別"); //添加到panel1中 panel1 = new Panel(new GridLayout(6, 1)); panel1.add(id); panel1.add(name); panel1.add(***); panel1.add(age); panel1.add(address); panel1.add(dep); //添加到窗體的東部的組件 jtf1 = new JTextField(10); jtf1.setText((String)(stModel.getValueAt(row, 0))); jtf1.setEditable(false);//設(shè)置學(xué)號不可修改 jtf2 = new JTextField(10); jtf2.setText((String)(stModel.getValueAt(row, 1))); jtf3 = new JTextField(10); jtf3.setText((String)(stModel.getValueAt(row, 2))); jtf4 = new JTextField(10); jtf4.setText((String)(stModel.getValueAt(row, 3))); jtf5 = new JTextField(10); jtf5.setText((String)(stModel.getValueAt(row, 4))); jtf6 = new JTextField(10); jtf6.setText((String)(stModel.getValueAt(row, 5))); //添加到第二個面板中 panel2 = new Panel(new GridLayout(6, 1)); panel2.add(jtf1); panel2.add(jtf2); panel2.add(jtf3); panel2.add(jtf4); panel2.add(jtf5); panel2.add(jtf6); //窗體南部的組件 update = new JButton("修改"); canel = new JButton("取消"); panel3 = new Panel(); panel3.add(update); panel3.add(canel); //注冊事件監(jiān)聽 update.addActionListener(this); canel.addActionListener(this); //添加組件到窗體中 this.add(panel1, BorderLayout.WEST); this.add(panel2, BorderLayout.CENTER); this.add(panel3, BorderLayout.SOUTH); //設(shè)置窗體屬性
this.setSize(300, 200); //設(shè)置窗體為可見 this.setVisible(true); } //響應(yīng)用戶操作 public void actionPerformed(ActionEvent e) { //數(shù)據(jù)庫 Connection con = null; PreparedStatement ps = null; String driver="org.gjt.mm.mysql.Driver"; String url="jdbc:mysql://localhost:3306/my_ddb?characterEncoding=utf8"; String user="feng"; String passWord="1001001"; boolean result = true; //記錄修改學(xué)生是否成功 if(e.getSource() == update) { try { //加載jdbc驅(qū)動 Class.forName(driver); //連接數(shù)據(jù)源 con = DriverManager.getConnection(url,user,passWord); //執(zhí)行sql String sql = "update st set stNname=?, st***=?,stAge=?,stBorn=?,stDept=? where stId=? "; ps = con.prepareStatement(sql); //修改學(xué)生數(shù)據(jù) ps.setString(1, jtf2.getText()); ps.setString(2, jtf3.getText()); ps.setString(3, jtf4.getText()); ps.setString(4, jtf5.getText()); ps.setString(5, jtf6.getText()); ps.setString(6, jtf1.getText()); ps.executeUpdate(); //關(guān)閉對話框 this.dispose(); } catch(ClassNotFoundException e1) { result = false; e1.printStackTrace(); } catch(SQLException e1) { result = false; e1.printStackTrace(); } finally { //關(guān)閉資源 try { if(null != ps) { ps.close(); } if(null != con) { con.close(); } } catch(SQLException e1) { e1.printStackTrace(); } if(false == result) { JOptionPane.showMessageDialog(this, "失敗信息:學(xué)號不能重復(fù),,性別只能為'男'或者'女',年齡大于0"); } } } else if(e.getSource() == canel) { this.dispose();//關(guān)閉對話框 } } }
================================================================================ #方法2
================================================================================
package model_2;
//AddStu文件源代碼:
import java.awt.*; import javax.swing.*; import javax.swing.table.*; import java.awt.event.*; import javax.swing.event.*; public class AddStu extends JDialog implements ActionListener {
private JLabel id, name, ***, age, address, dep; //分別代表學(xué)生的學(xué)號,名字,,性別,,年齡,籍貫,,系別 private JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6; //用戶輸入的文本框,,分別為學(xué)生的學(xué)號,名字,,性別,,年齡,籍貫,,系別 private JButton add, canel; //分別為添加按鈕和取消按鈕 private Panel panel1, panel2, panel3; public AddStu(Frame owner, String title, boolean model) { super(owner, title, model); //實(shí)現(xiàn)與父類窗口對話 System.out.println("我執(zhí)行了"); //窗體的西部的組件 id = new JLabel("學(xué)號:"); name = new JLabel("姓名:"); *** = new JLabel("性別"); age = new JLabel("年齡"); address = new JLabel("籍貫"); dep = new JLabel("系別:"); //添加到panel1面板中 panel1 = new Panel(new GridLayout(6, 1)); panel1.add(id); panel1.add(name); panel1.add(***); panel1.add(age); panel1.add(address); panel1.add(dep); //窗體的東部組件 jtf1 = new JTextField(10); jtf2 = new JTextField(10); jtf3 = new JTextField(10); jtf4 = new JTextField(10); jtf5 = new JTextField(10); jtf6 = new JTextField(10); //添加到第二個面板中 panel2 = new Panel(new GridLayout(6, 1)); panel2.add(jtf1); panel2.add(jtf2); panel2.add(jtf3); panel2.add(jtf4); panel2.add(jtf5); panel2.add(jtf6); //窗體北部的控件 add = new JButton("添加"); canel = new JButton("取消"); //注冊事件監(jiān)聽 add.addActionListener(this); canel.addActionListener(this); //添加到panel3面板中 panel3 = new Panel(); panel3.add(add); panel3.add(canel); //將各個組件添加到窗體中 this.add(panel1, BorderLayout.WEST); this.add(panel2, BorderLayout.CENTER); this.add(panel3, BorderLayout.SOUTH); //設(shè)置對話框?qū)傩?br> this.setSize(300, 200); //設(shè)置對話框?yàn)榭梢?br> this.setVisible(true); } //響應(yīng)用戶的操作 public void actionPerformed(ActionEvent e) { //用戶點(diǎn)擊了添加按鈕 if(e.getSource() == add) { String sql = "insert into st values(?,?,?,?,?,?)"; String[]paras = {jtf1.getText(),jtf2.getText(),jtf3.getText(),jtf4.getText(),jtf5.getText(),jtf6.getText()}; //建立st表的數(shù)據(jù)模型 StuModel stuModel = new StuModel(); if(!stuModel.updateDatebase(sql, paras)) { JOptionPane.showMessageDialog(this, "失敗信息:學(xué)號不能重復(fù),,性別只能為'男'或者'女',年齡大于0"); } else { //關(guān)閉對話框 this.dispose(); } } else if(e.getSource() == canel) { //關(guān)閉對話框 this.dispose(); } } } ================================================================================ package model_2;
//SqlHelper 文件源代碼:
import java.sql.*;
//后臺數(shù)據(jù)庫的類
public final class SqlHelper { //數(shù)據(jù)庫 Connection con = null; PreparedStatement ps = null; ResultSet rs = null; private static final String driver="org.gjt.mm.mysql.Driver"; private static final String url="jdbc:mysql://localhost:3306/my_ddb?characterEncoding=utf8"; private static final String user="feng"; private static final String password="1001001"; //用戶對數(shù)據(jù)庫進(jìn)行數(shù)據(jù)查詢 public ResultSet queryDatabase(String sql, String paras) { try { //加載jdbc驅(qū)動 Class.forName(driver); //連接數(shù)據(jù)源 con = DriverManager.getConnection(url, user, password); //執(zhí)行sql ps = con.prepareStatement(sql); if(null != paras) { ps.setString(1, paras); } rs = ps.executeQuery(); } catch(ClassNotFoundException e) { e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } return rs; } //用戶對數(shù)據(jù)庫進(jìn)行增刪改的操作 public boolean updateDatabase(String sql, String[] paras) { boolean result = true; //記錄對數(shù)據(jù)庫的操作是否成功 try { //加載jdbc驅(qū)動 Class.forName(driver); //連接數(shù)據(jù)源 con = DriverManager.getConnection(url, user, password); //執(zhí)行sql ps = con.prepareStatement(sql); for(int i = 0; i < paras.length; i ) { ps.setString(i 1, paras[i]); } ps.executeUpdate(); } catch(ClassNotFoundException e) { result = false; e.printStackTrace(); } catch(SQLException e) { result = false; e.printStackTrace(); } finally { //關(guān)閉資源 this.close(); } return result; } public void close() { //關(guān)閉資源 try { if(null != ps) { ps.close(); } if(null != con) { con.close(); } } catch(SQLException e) { e.printStackTrace(); } } } ================================================================================ package model_2;
//model2模式開發(fā):
//StuMange文件源代碼: import java.awt.*; import javax.swing.*; import javax.swing.table.*; import java.awt.event.*; import javax.swing.event.*; public class StuMange extends JFrame implements ActionListener {
JLabel userName; //學(xué)生名 JTextField textField ; //用戶輸入學(xué)生名字的文本框 JButton select, insert, update, delete; //分別代表查詢,,添加修改,刪除的按鈕 JTable table; //用于顯示數(shù)據(jù)的表 Panel panel1, panel2; JScrollPane jscrollPane; StuModel stuModel = null; //st表的模型 public static void main(String[] args) { StuMange stuMange = new StuMange(); } //構(gòu)造方法 public StuMange() { //窗體上部的組件 userName = new JLabel("學(xué)生名:"); textField = new JTextField(10); select = new JButton("查詢"); panel1 = new Panel(); //將組件添加到panel1面板中 panel1.add(userName); panel1.add(textField); panel1.add(select); //窗體的中部組件 stuModel = new StuModel(); stuModel.queryDate("select * from st", null); table = new JTable(stuModel); jscrollPane = new JScrollPane(table); //船體的下部組件 insert = new JButton("添加"); update = new JButton("修改"); delete = new JButton("刪除"); panel2 = new Panel(); //將組件添加到panel2面板中 panel2.add(insert); panel2.add(update); panel2.add(delete); //設(shè)置事件監(jiān)聽 select.addActionListener(this); insert.addActionListener(this); update.addActionListener(this); delete.addActionListener(this); //將各個組件添加到窗體中 this.add(panel1, BorderLayout.NORTH); this.add(jscrollPane, BorderLayout.CENTER); this.add(panel2, BorderLayout.SOUTH); //設(shè)置窗體屬性 this.setSize(400, 300); this.setLocation(200, 200); this.setTitle("mini學(xué)生信息管理系統(tǒng)"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置窗體為可見 this.setVisible(true); } //響應(yīng)用戶的按鍵操作 public void actionPerformed(ActionEvent e) { //用戶點(diǎn)擊了查詢按鈕 if(e.getSource() == select) { String name = textField.getText().trim(); String sql = "select * from st where stNname=?"; stuModel = new StuModel(); stuModel.queryDate(sql, name); //刷新窗口 table.setModel(stuModel); } //用戶點(diǎn)擊了添加按鈕 else if(e.getSource() == insert) { AddStu addStu = new AddStu (this, "添加學(xué)生", true); //刷新窗口 stuModel = new StuModel(); stuModel.queryDate("select * from st", null); table.setModel(stuModel); } //用戶點(diǎn)擊了修改的按鈕 else if(e.getSource() == update) { int row = this.table.getSelectedRow();//獲得用戶選中的行 if(-1 == row) { JOptionPane.showMessageDialog(this, "請選擇行"); } else { UpdateStu updateStu = new UpdateStu(this, "修改學(xué)生", true, stuModel, row); //刷新窗口 StuModel stModel = new StuModel(); stModel.queryDate("select * from st", null); table.setModel(stModel); } } //用戶點(diǎn)擊了刪除按鈕 else if(e.getSource() == delete) { int row = this.table.getSelectedRow(); //獲得用戶選中的行 String id = (String)stuModel.getValueAt(row, 0); String sql = "delete from st where stId=?"; String[]paras = {id}; stuModel.updateDatebase(sql, paras); //刷新窗口 StuModel stuModel = new StuModel(); stuModel.queryDate("select * from st", null); table.setModel(stuModel); } } }
================================================================================ package model_2;
//StuModel文件源代碼:
import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; import javax.swing.table.*; import java.sql.*; public class StuModel extends AbstractTableModel {
Vector Vector SqlHelper sqlHelper = null; ResultSet rs = null; //用戶對數(shù)據(jù)庫進(jìn)行查詢的操作 public void queryDate(String sql, String paras) { columnNames = new Vector columnNames.add("學(xué)號"); columnNames.add("名字"); columnNames.add("性別"); columnNames.add("年齡"); columnNames.add("籍貫"); columnNames.add("系別"); rowDate = new Vector sqlHelper = new SqlHelper(); rs = sqlHelper.queryDatabase(sql, paras); try { while(rs.next()) { Vector row.add(rs.getString(1)); row.add(rs.getString(2)); row.add(rs.getString(3)); row.add(rs.getString(4)); row.add(rs.getString(5)); row.add(rs.getString(6)); rowDate.add(row); //將row添加到rowDate中,。實(shí)現(xiàn)了讀取數(shù)據(jù)庫的記錄 } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { //關(guān)閉資源 if(null != rs) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } sqlHelper.close(); } } //用戶對數(shù)據(jù)庫進(jìn)行增刪查的操作 public boolean updateDatebase(String sql, String[] paras) { SqlHelper sqlHelper = new SqlHelper(); return sqlHelper.updateDatabase(sql, paras); } @Override //返回表的列數(shù) public int getColumnCount() { // TODO Auto-generated method stub return this.columnNames.size(); } @Override
//返回表的行數(shù) public int getRowCount() { // TODO Auto-generated method stub return this.rowDate.size(); } @Override
//返回某行某列 public Object getValueAt(int rowIndex, int columnIndex) { // TODO Auto-generated method stub return ((Vector)this.rowDate.get(rowIndex)).get(columnIndex); } //設(shè)置表的字段名 public String getColumnName(int column) { return (String)this.columnNames.get(column); } }
================================================================================ package model_2;
//UpdateStu文件源代碼: import java.awt.*;
import javax.swing.*;
import javax.swing.table.*; import java.awt.event.*; import javax.swing.event.*;
public class UpdateStu extends JDialog implements ActionListener{
JLabel id,name,***,age,address,dep; //分別為學(xué)生的學(xué)號,,學(xué)生的姓名,學(xué)生的性別,,學(xué)生的年齡,,學(xué)生的籍貫,學(xué)生的系別的標(biāo)簽 JTextField jtf1, jtf2, jtf3, jtf4,jtf5, jtf6; //分別為學(xué)生的學(xué)號,,姓名,,性別,年齡,,籍貫,,系列的輸入文本框 JButton update,canel; //分別為修改和取消按鈕 Panel panel1 ,panel2, panel3; public UpdateStu(Frame owner, String title, boolean model, StuModel stModel, int row) { super(owner, title, model); //窗體西部的組件 id = new JLabel("學(xué)號"); name = new JLabel("名字"); *** = new JLabel("性別"); age = new JLabel("年齡"); address = new JLabel("地址"); dep = new JLabel("系別"); //添加到panel1中 panel1 = new Panel(new GridLayout(6, 1)); panel1.add(id); panel1.add(name); panel1.add(***); panel1.add(age); panel1.add(address); panel1.add(dep); //添加到窗體的東部的組件 jtf1 = new JTextField(10); jtf1.setText((String)(stModel.getValueAt(row, 0))); jtf1.setEditable(false);//設(shè)置學(xué)號不可修改 jtf2 = new JTextField(10); jtf2.setText((String)(stModel.getValueAt(row, 1))); jtf3 = new JTextField(10); jtf3.setText((String)(stModel.getValueAt(row, 2))); jtf4 = new JTextField(10); jtf4.setText((String)(stModel.getValueAt(row, 3))); jtf5 = new JTextField(10); jtf5.setText((String)(stModel.getValueAt(row, 4))); jtf6 = new JTextField(10); jtf6.setText((String)(stModel.getValueAt(row, 5))); //添加到第二個面板中 panel2 = new Panel(new GridLayout(6, 1)); panel2.add(jtf1); panel2.add(jtf2); panel2.add(jtf3); panel2.add(jtf4); panel2.add(jtf5); panel2.add(jtf6); //窗體南部的組件 update = new JButton("修改"); canel = new JButton("取消"); panel3 = new Panel(); panel3.add(update); panel3.add(canel); //注冊事件監(jiān)聽 update.addActionListener(this); canel.addActionListener(this); //添加組件到窗體中 this.add(panel1, BorderLayout.WEST); this.add(panel2, BorderLayout.CENTER); this.add(panel3, BorderLayout.SOUTH); //設(shè)置窗體屬性
this.setSize(300, 200); //設(shè)置窗體為可見 this.setVisible(true); } //響應(yīng)用戶操作 public void actionPerformed(ActionEvent e) { if(e.getSource() == update) { String sql = "update st set stNname=?, st***=?,stAge=?,stBorn=?,stDept=? where stId=?"; String[] paras = {jtf2.getText(),jtf3.getText(),jtf4.getText(),jtf5.getText(),jtf6.getText(),jtf1.getText()}; //建立st表的模型 StuModel stuModel = new StuModel(); if(!stuModel.updateDatebase(sql, paras)) { JOptionPane.showMessageDialog(this, "失敗信息:學(xué)號不能重復(fù),性別只能為'男'或者'女',年齡大于0"); } else { //關(guān)閉對話框 this.dispose(); } } else if(e.getSource() == canel) { //關(guān)閉對話框 this.dispose(); } } } ================================================================================ 以上由本人轉(zhuǎn)載http://blog.csdn.net/dengxina/article/details/5941810
并修改,。
|
|
來自: 紫火神兵 > 《計算機(jī)資料》