韓順平 循序漸進學(xué)java 從入門到精通筆記到_第1頁
韓順平 循序漸進學(xué)java 從入門到精通筆記到_第2頁
韓順平 循序漸進學(xué)java 從入門到精通筆記到_第3頁
韓順平 循序漸進學(xué)java 從入門到精通筆記到_第4頁
韓順平 循序漸進學(xué)java 從入門到精通筆記到_第5頁
已閱讀5頁,還剩41頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、由于對數(shù)據(jù)庫操作后有很多重復(fù)代碼.這樣可以把操作封裝成一個類,這個類可以完成對表的操作.第七十一講: 初步理解 模式的概念(mv):增加項目的可維護性,尤其在做一個大項目的時候,如果沒有一個好的方案的話,那么就會很悲劇了.軟件開發(fā)也是一個漸進的過程./* * 寫成一個 ming版的學(xué)生管理系統(tǒng) * 1,查詢?nèi)蝿?wù) * 2,添加學(xué)生 */package com.test1;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;public class StudentManagement ex

2、tends JFrame implements ActionListener /定義一些控件JPanel jp1,jp2;JLabel jl;JButton jb1,jb2,jb3,jb4;JTable jt ;JTextField jtf;JScrollPane jsp =null;Connection ct=null;PreparedStatement ps=null;StuModel sm=null;public static void main(String args) StudentManagement ta=new StudentManagement();public Studen

3、tManagement()jp1=new JPanel();jl=new JLabel("請輸入名字:");jtf=new JTextField(20);jb1=new JButton("查詢");/注冊監(jiān)聽jb1.addActionListener(this);/把各個控件加入到j(luò)p1;jp1.add(jl);jp1.add(jtf);jp1.add(jb1);jp2=new JPanel();jb2=new JButton("添加");jb3 =new JButton("修改");jb4 =new JButto

4、n("刪除");/注冊監(jiān)聽jb2.addActionListener(this);jb3.addActionListener(this);jb4.addActionListener(this);jp2.add(jb2);jp2.add(jb3);jp2.add(jb4);/創(chuàng)建一個數(shù)據(jù)模型對象 sm =new StuModel();/初始化JTablejt=new JTable(sm);/初始化jspjsp=new JScrollPane(jt);/吧jsp放進到JFramethis.add(jsp);this.add(jp1,BorderLayout.NORTH);thi

5、s.add(jp2,BorderLayout.SOUTH);this.setSize(500,400);this.setLocation(200, 200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stub/判斷是那個按鈕被點擊/如果相應(yīng)與監(jiān)聽在同一個類中 也可以用下面方法.if(e.getSource()=jb1)/System

6、.out.println("用戶希望查詢");測試用的/因為把表的數(shù)據(jù)封裝到StuModel中,我們就可以比較簡單地完成查詢?nèi)蝿?wù)String name=this.jtf.getText().trim();/寫一個sql語句String sql ="select * from student where stuName='"+name+"'"/構(gòu)建新的數(shù)據(jù)模型類并更新sm =new StuModel(sql);jt.setModel(sm);/當(dāng)用戶點擊添加else if(e.getSource()=jb2)/合理應(yīng)該為

7、模式的狀態(tài),否則,還沒有插完就會執(zhí)行下面的語句,導(dǎo)致無法更新.StuAddDialog sad =new StuAddDialog(this,"添加學(xué)生",true);/重新再獲得新的數(shù)據(jù)模型 sm =new StuModel();jt.setModel(sm);else if(e.getSource()=jb3)System.out.print("aaaa");int rownum =this.jt.getSelectedRow();if(rownum=-1)/提示JOptionPane.showMessageDialog(this, "請選

8、擇一行");return;/代表不要再往下面走了,誰調(diào)用就返回給誰/顯示修改對話框new StudentUpdateDialog(this,"修改對話框",true,sm,rownum);/當(dāng)前用戶點擊刪除else if(e.getSource()=jb4)/得到該學(xué)生的id/getSelectedRo會返回用戶點擊的行/如果一行都沒選,則會返回-1int rownum=this.jt.getSelectedRow();if(rownum=-1)/提示JOptionPane.showMessageDialog(this, "請選擇一行");re

9、turn;/代表不要再往下面走了,誰調(diào)用就返回給誰/得到學(xué)生的編號String stuId=(String)sm.getValueAt(rownum, 0);/System.out.print(stuId);/測試用的/連接數(shù)據(jù)庫,完成刪除任務(wù)try/加載驅(qū)動Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");/System.out.print("1");/測試用的/得到連接ct=DriverManager.getConnection("jdbc:sqlserver:/12

10、7.0.0.1:1433;databaseName=spdb1","sa","h123");/System.out.print("2");/測試用的ps=ct.prepareStatement("delete from student where stuid='"+stuId+"'");/System.out.print("3");/測試用的ps.executeUpdate();catch(Exception ex)ex.printStackTrac

11、e();finallytry if(ps!=null) ps.close();if(ct!=null) ps.close(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace(); sm =new StuModel();jt.setModel(sm);/* * 這是我的一個Student表的模型 * 可以把對student表的各種操作封裝到該模型中 */package com.test1;import java.sql.*;import java.util.Vector;import jav

12、ax.swing.JTable;import javax.swing.table.AbstractTableModel;public class StuModel extends AbstractTableModelVector rowData, columnNames; JTable jt=null;/定義操作數(shù)據(jù)庫需要的東西PreparedStatement ps=null;Connection ct=null;ResultSet rs =null;public void init(String sql)if(sql =null)sql ="select * from stude

13、nt"/中間處理jt =new JTable();columnNames=new Vector();/設(shè)置列名columnNames.add("學(xué)號");columnNames.add("名字");columnNames.add("性別");columnNames.add("年齡");columnNames.add("籍貫");columnNames.add("系別");/rowData可以存放多行rowData =new Vector();try/加載驅(qū)動Clas

14、s.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");/System.out.print("1");/測試用的/得到連接ct=DriverManager.getConnection("jdbc:sqlserver:/127.0.0.1:1433;databaseName=spdb1","sa","h123");/System.out.print("2");/測試用的ps=ct.prepareStatement(sq

15、l);/System.out.print("3");/測試用的rs=ps.executeQuery();/System.out.print("4");/測試用的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.getInt(4);hang.add(rs.getString(5);hang.add(rs.getString(6);/加入到rowDatar

16、owData.add(hang);/System.out.print("5");/測試用的catch(Exception e)e.printStackTrace();finallytry if(rs!=null) rs.close();if(ps!=null) ps.close();if(ct!=null) ct.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();/通過傳遞的sql語句來獲得數(shù)據(jù)模型public StuModel(String sql)in

17、it(sql);/構(gòu)造函數(shù),初始化我們的數(shù)據(jù)模型public StuModel()this.init(null);public void addStu(String sql)/根據(jù)用戶輸入的sql語句完成添加任務(wù).Overridepublic String getColumnName(int column) / TODO Auto-generated method stubreturn (String)this.columnNames.get(column);Override/得到共有多少列public int getColumnCount() / TODO Auto-generated me

18、thod stub/System.out.print("getColumnCount");/測試所用return this.columnNames.size();Override/得到共有多少行public int getRowCount() / TODO Auto-generated method stubreturn this.rowData.size();Override/得到某行某列的數(shù)據(jù)public Object getValueAt(int rowIndex, int columnIndex) / TODO Auto-generated method stubr

19、eturn (Vector)this.rowData.get(rowIndex).get(columnIndex);package com.test1;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;public class StuAddDialog extends JDialog implements ActionListener/定義我需要的swing組件JLabel jl1, jl2,jl3, jl4, jl5 ,jl6;JButton jb1,jb2;JTextField

20、jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;JPanel jp1,jp2,jp3;/構(gòu)造函數(shù) Frame代表父窗口口,title代表窗口的名字,model指定是模式窗口,還是非模式的窗口public StuAddDialog(Frame owner,String title,boolean model)super(owner,title, model); /調(diào)用父類構(gòu)造方法,達到模式對話框效果jl1=new JLabel("學(xué)號");jl2=new JLabel("姓名");jl3=new JLabel("性別");j

21、l4=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 JP

22、anel();jp2=new JPanel();jp3=new JPanel();/設(shè)置布局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.ad

23、d(jb2);this.add(jp1,BorderLayout.WEST);this.add(jp2,BorderLayout.CENTER);this.add(jp3,BorderLayout.SOUTH);/注冊監(jiān)聽jb1.addActionListener(this);/展現(xiàn)this.setSize(300,200);this.setLocation(200, 300);this.setVisible(true);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(

24、e.getSource()=jb1)/對用戶點擊添加按鈕后的響應(yīng)動作/連接數(shù)據(jù)庫Connection ct =null;PreparedStatement ps =null;try /加載驅(qū)動Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");/獲取連接ct=DriverManager.getConnection("jdbc:sqlserver:/127.0.0.1:1433;databaseName=spdb1","sa","h123");/

25、預(yù)編譯的都是通過添加參數(shù)的方式來賦值ps=ct.prepareStatement("insert into student values (?,?,?,?,?,?)");ps.setString(1, this.jtf1.getText();ps.setString(2, this.jtf2.getText();ps.setString(3, this.jtf3.getText();ps.setInt(4, Integer.parseInt(this.jtf4.getText();ps.setString(5, this.jtf5.getText();ps.setStrin

26、g(6, this.jtf6.getText();int i=ps.executeUpdate();if(i=1)System.out.print("添加成功ok");elseSystem.out.print("添加失敗"); catch (Exception e1) / TODO Auto-generated catch blocke1.printStackTrace();finallytry ps.close();ct.close(); catch (SQLException e1) / TODO Auto-generated catch block

27、e1.printStackTrace();/* * 修改學(xué)生界面 */package com.test1;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;public class StudentUpdateDialog extends JDialog implements ActionListener/定義我需要的swing組件JLabel jl1, jl2,jl3, jl4, jl5 ,jl6;JButton jb1,jb2;JTextField jtf1,jtf2,jtf3,j

28、tf4,jtf5,jtf6;JPanel jp1,jp2,jp3;/構(gòu)造函數(shù) Frame代表父窗口口,title代表窗口的名字,model指定是模式窗口,還是非模式的窗口public StudentUpdateDialog(Frame owner,String title,boolean model,StuModel sm,int rownum)super(owner,title, model); /調(diào)用父類構(gòu)造方法,達到模式對話框效果jl1=new JLabel("學(xué)號");jl2=new JLabel("姓名");jl3=new JLabel(&qu

29、ot;性別");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();/初始化數(shù)據(jù)jtf1.setText(String)sm.getValueAt(rownum, 0);jtf1.setEdi

30、table(false);jtf2.setText(String)sm.getValueAt(rownum, 1);jtf3.setText(String)sm.getValueAt(rownum, 2);jtf4.setText(sm.getValueAt(rownum, 3)+"");jtf5.setText(String)sm.getValueAt(rownum, 4);jtf6.setText(String)sm.getValueAt(rownum, 5);jb1=new JButton ("修改");jb2=new JButton ("

31、;取消");jp1=new JPanel();jp2=new JPanel();jp3=new JPanel();/設(shè)置布局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

32、);jp3.add(jb1);jp3.add(jb2);this.add(jp1,BorderLayout.WEST);this.add(jp2,BorderLayout.CENTER);this.add(jp3,BorderLayout.SOUTH);/注冊監(jiān)聽jb1.addActionListener(this);/展現(xiàn)this.setSize(300,200);this.setLocation(200, 300);this.setVisible(true);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-gen

33、erated method stubif(e.getSource()=jb1)/對用戶點擊添加按鈕后的響應(yīng)動作/連接數(shù)據(jù)庫Connection ct =null;PreparedStatement ps =null;try /加載驅(qū)動Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");/獲取連接ct=DriverManager.getConnection("jdbc:sqlserver:/127.0.0.1:1433;databaseName=spdb1","sa"

34、;,"h123");/預(yù)編譯的都是通過添加參數(shù)的方式來賦值ps=ct.prepareStatement("update student set stuName=?,stuSex=?,stuAge=?,stuJg=?,stuDept=? where stuId=?");ps.setString(1, this.jtf2.getText();ps.setString(2, this.jtf3.getText();ps.setInt(3, Integer.parseInt(this.jtf4.getText();ps.setString(4, this.jtf

35、5.getText();ps.setString(5, this.jtf6.getText();ps.setString(6, this.jtf1.getText();int i=ps.executeUpdate();if(i=1)System.out.print("修改成功ok");elseSystem.out.print("修改失敗"); catch (Exception e1) / TODO Auto-generated catch blocke1.printStackTrace();finallytry ps.close();ct.close()

36、; catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();第七十二講:下面將前面的model1模式改成model2模式Model2模式的最大特點是: 界面和后臺操作是分離的,代碼復(fù)用性高,可讀性好,可維護性高;缺點是:復(fù)雜性高.現(xiàn)在大部分公司采用的是model2模式./* * Model2 模式 * 寫成一個 ming版的學(xué)生管理系統(tǒng) * 1,查詢?nèi)蝿?wù) * 2,添加學(xué)生 */package com.test2;import java.awt.*;import java.awt.event.*;

37、import java.sql.*;import javax.swing.*;public class StudentManagement extends JFrame implements ActionListener /定義一些控件JPanel jp1,jp2;JLabel jl;JButton jb1,jb2,jb3,jb4;JTable jt ;JTextField jtf;JScrollPane jsp =null;StuModel sm=null;public static void main(String args) StudentManagement ta=new Studen

38、tManagement();public StudentManagement()jp1=new JPanel();jl=new JLabel("請輸入名字:");jtf=new JTextField(20);jb1=new JButton("查詢");/注冊監(jiān)聽jb1.addActionListener(this);/把各個控件加入到j(luò)p1;jp1.add(jl);jp1.add(jtf);jp1.add(jb1);jp2=new JPanel();jb2=new JButton("添加");jb3 =new JButton(&quo

39、t;修改");jb4 =new JButton("刪除");/注冊監(jiān)聽jb2.addActionListener(this);jb3.addActionListener(this);jb4.addActionListener(this);jp2.add(jb2);jp2.add(jb3);jp2.add(jb4);/創(chuàng)建一個數(shù)據(jù)模型對象 sm =new StuModel();/初始化JTablejt=new JTable(sm);/初始化jspjsp=new JScrollPane(jt);/吧jsp放進到JFramethis.add(jsp);this.add(

40、jp1,BorderLayout.NORTH);this.add(jp2,BorderLayout.SOUTH);this.setSize(500,400);this.setLocation(200, 200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stub/判斷是那個按鈕被點擊/如果相應(yīng)與監(jiān)聽在同一個類中 也可以用下面方法.i

41、f(e.getSource()=jb1)/因為把表的數(shù)據(jù)封裝到StuModel中,我們就可以比較簡單地完成查詢?nèi)蝿?wù)String name=this.jtf.getText().trim();/寫一個sql語句String sql ="select * from student where stuName='"+name+"'"/構(gòu)建新的數(shù)據(jù)模型類并更新sm =new StuModel(sql);jt.setModel(sm);/當(dāng)用戶點擊添加else if(e.getSource()=jb2)/合理應(yīng)該為模式的狀態(tài),否則,還沒有插完就會執(zhí)

42、行下面的語句,導(dǎo)致無法更新.StuAddDialog sad =new StuAddDialog(this,"添加學(xué)生",true);/重新再獲得新的數(shù)據(jù)模型 sm =new StuModel();jt.setModel(sm);else if(e.getSource()=jb3)System.out.print("aaaa");int rownum =this.jt.getSelectedRow();if(rownum=-1)/提示JOptionPane.showMessageDialog(this, "請選擇一行");return

43、;/代表不要再往下面走了,誰調(diào)用就返回給誰/顯示修改對話框new StudentUpdateDialog(this,"修改對話框",true,sm,rownum);/當(dāng)前用戶點擊刪除else if(e.getSource()=jb4)/得到該學(xué)生的id/getSelectedRo會返回用戶點擊的行/如果一行都沒選,則會返回-1int rownum=this.jt.getSelectedRow();if(rownum=-1)/提示JOptionPane.showMessageDialog(this, "請選擇一行");return;/代表不要再往下面走了,

44、誰調(diào)用就返回給誰/得到學(xué)生的編號String stuId=(String)sm.getValueAt(rownum, 0);/System.out.print(stuId);/測試用的StuModel temp=new StuModel();/創(chuàng)建一個sql 語句String sql ="delete from student where stuId=?"String paras =stuId;if(!temp.updateStudent(sql, paras)/提示JOptionPane.showMessageDialog(this, "刪除失敗");

45、 sm =new StuModel();/解決一次無用的查詢jt.setModel(sm);/* * 這是我的一個Student表的模型 * 可以把對student表的各種操作封裝到該模型中 */package com.test2;import java.sql.*;import java.util.Vector;import javax.swing.JTable;import javax.swing.table.*;public class StuModel extends AbstractTableModelVector rowData, columnNames; JTable jt=nu

46、ll;/定義操作數(shù)據(jù)庫需要的東西PreparedStatement ps=null;Connection ct=null;ResultSet rs =null;String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"String url="jdbc:sqlserver:/127.0.0.1:1433;databaseName=spdb1"String user="sa"String password ="h123"/添加,刪除,修改學(xué)生 由于添加的參數(shù)

47、不確定,因此用數(shù)組來傳遞參數(shù)public Boolean updateStudent( String sql,String paras )boolean b=true;try/1加載驅(qū)動Class.forName(driver);/2得到連接ct=DriverManager.getConnection(url,user,password);/3創(chuàng)建ps對象ps=ct.prepareStatement(sql);/給ps的?賦值for(int i=0;i<paras.length;i+)/sql 中給int 傳入String類型,dbms會自動轉(zhuǎn)的.ps.setString(i+1, pa

48、rasi);/4執(zhí)行操作if(ps.executeUpdate()!=1)b=false;catch (Exception e)b=false;e.printStackTrace();finallytry if(ps!=null)ps.close();if(ct!=null)ct.close(); catch (SQLException e) e.printStackTrace();return b;public void init(String sql)if(sql =null)sql ="select * from student"/中間處理jt =new JTable

49、();columnNames=new Vector();/設(shè)置列名columnNames.add("學(xué)號");columnNames.add("名字");columnNames.add("性別");columnNames.add("年齡");columnNames.add("籍貫");columnNames.add("系別");/rowData可以存放多行rowData =new Vector();try/加載驅(qū)動Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");/System.out.print("1");/測試用的/得到連接ct=DriverManager.getConnection("jdbc:sqlserver:/127.0.0.1:1

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論