java可視化界面操作數(shù)據(jù)庫編程_第1頁
java可視化界面操作數(shù)據(jù)庫編程_第2頁
java可視化界面操作數(shù)據(jù)庫編程_第3頁
java可視化界面操作數(shù)據(jù)庫編程_第4頁
java可視化界面操作數(shù)據(jù)庫編程_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、天津理工大學(xué)實驗報ZE告學(xué)院(系)名稱: 計算機與通信工程學(xué)院姓名晴小天學(xué)號201356XXXX專業(yè)計算機科學(xué)與技術(shù)班級2013級2班實驗項目實驗五:小型信息管理系統(tǒng)設(shè)計開發(fā)批改意見課程名稱實驗時間Java語言程序設(shè)計2015年11月12日、20日、25日第3、4節(jié)課程代碼實驗地點成績06670567-219教師簽字:實驗內(nèi)容:1.參考實驗資料,自擬題目,設(shè)計制作一個小型信息管理系統(tǒng)。實驗?zāi)康?1 .理解并掌握J(rèn)ava數(shù)據(jù)庫訪問技術(shù)。2 .掌握對數(shù)據(jù)進行增加、刪除、修改、查詢。實驗要求:1 .按照要求編制程序;3 .為增加程序可讀性,請在程序中對類成員進行適當(dāng)注釋說明;4 .整理上機步驟,總結(jié)

2、經(jīng)驗和體會;5 .認(rèn)真完成并按時提交實驗報告?!緦嶒炦^程記錄(源程序、測試用例、測試結(jié)果及心得體會等)】MIS系統(tǒng)系統(tǒng)名稱:學(xué)生信息管理系統(tǒng)操作系統(tǒng):Window8.1Java 環(huán)境;、jre 1.7.0_21集成開發(fā)環(huán)境:MyEcI ip se P rofessio nal 2013數(shù)據(jù)庫軟件版本: SQL Server 2010連接驅(qū)動:jdbc-odbc系統(tǒng)主要功能本項目主要實現(xiàn)了java界面的登錄功能,以及登陸后的增刪改查功能,主要是對student表實現(xiàn)增加一條學(xué)生信息、刪除一條學(xué)生信息記錄、更新學(xué)生信息表,以及查詢學(xué)生信息表的數(shù)據(jù)添加刪除更新查找1.2.數(shù)據(jù)庫結(jié)構(gòu)四、關(guān)鍵功能界面截

3、圖數(shù)據(jù)庫名稱:SchoolDB數(shù)據(jù)表:student表具體字段信息如下表:字段名類型長度小數(shù)點不是nullsn ame主鍵)varchar200Vsn amevarchar200Vsde ptvarchar200Vsgradevarchar200Vsaddressvarchar200Vsname, sname, sdept, sgrade 、saddress 分別保存學(xué)生的姓名、專業(yè)、學(xué)院、成績、家庭住址等 學(xué)生基本信息,其中sname為主鍵且不為空。1.登陸界面用戶輸入數(shù)據(jù)庫中存在的用戶名和密碼后,便可登陸成功,否則提示密碼錯誤正確的用戶名,錯誤的密碼2.正確的用戶名正確的密碼后進入學(xué)生信息

4、管理系統(tǒng):添加功能:輸入界面中的所有數(shù)據(jù)之后,點擊添加,即可向數(shù)據(jù)庫中加入一條學(xué)生記錄3. 刪除功能:輸入姓名,即可把數(shù)據(jù)庫中對應(yīng)姓名的一條學(xué)生信息刪除4. 更新功能:輸入要修改學(xué)生的信息,即可修改該學(xué)生在數(shù)據(jù)庫中的記錄5. 查找功能:點擊查找可以查詢數(shù)據(jù)庫中的所有學(xué)生信息,每頁9條記錄,其余的學(xué)生信息的點擊下一頁,可以查看,還可以點擊上一頁,來切換數(shù)據(jù)點擊下一頁:五、關(guān)鍵代碼分析DBConnetion用于連接數(shù)據(jù)庫:P ackage edu;p ublic class DBConn ecti on p ublic static Conn ecti on getC onn ecti on()C

5、onn ecti on conn = n ull;Stri ng url = "jdbc:odbc:schoolData"Stri ng user name = "sa"Stri ng p assword = "123456"try Class.forName(driver);conn = DriverMa nager.getC onn ectio n(url,username, password); catch (ClassNotFou ndExce pti on e) e.prin tStackTrace(); catch (SQ

6、LExce ption e) e.prin tStackTrace();retur n conn; 登陸的窗體類 LoginFrame 、 JFrame: package edu;LoginFrame frame;JLabel lname;JLabel lpassword; JTextField tname; JPasswordField tpassword; JButton byes;JButton bno;JPanel p1;JPanel p2;JPanel p3;public class Jswing implements ActionListener private private p

7、rivate private private private private private private privateprivate String userName,password;/* 初始化窗口數(shù)據(jù)*/public void initData() frame=new LoginFrame(" 登陸 "); frame.setResizable(false); frame.setLayout(new GridLayout(3, 2); Container c=frame.getContentPane(); lname=new JLabel(" 用戶名:

8、"); lpassword=new JLabel(" 密 碼 : "); tname=new JTextField(8); tpassword=new JPasswordField(8); byes=new JButton(" 確定 "); bno=new JButton(" 取消 "); p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); p1.add(lname); p1.add(tname); c.add(p1);p2.add(lpassword); p2.add(tp

9、assword); c.add(p2);p3.add(byes); p3.add(bno); c.add(p3); frame.setVisible(true); byes.addActionListener(this); bno.addActionListener(this);userName=null;password=null;Overridepublic void actionPerformed(ActionEvent e) if(e.getSource()=bno)System.exit(1);elseif(e.getSource()=byes)userName=tname.getT

10、ext().trim();password=new String(tpassword.getPassword(); boolean result=false;result=SchoolOperate.login(userName, password); if(result)frame.setVisible(false);SwingUtilities.invokeLater(new Runnable() public void run() SchoolInfoGui application = new SchoolInfoGui(); application.getJFrame().setVis

11、ible(true););elseString error=null;if(result=false)H.error=" 用戶名或密碼錯誤,請重試! JOptionPane.showMessageDialog(null, error);public static void main(String args) new Jswing().initData();package edu;public class LoginFrame extends JFramepublic LoginFrame(String title) throws HeadlessException super();s

12、etTitle(title);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(540,200, 280, 200);學(xué)生信息管理系統(tǒng)主界面的 SchoolInfoGui package edu;public class SchoolInfoGui implements ActionListener private JFrame jFrame;private JPanel jContentPane;private JLabel lblName = null;private JTextField tfName = null;priv

13、ate JLabel lblRegion = null;private JComboBox cbRegion = null;private JLabel lblArea = null;private JTextField tfArea = null;private JLabel lblPopu = null;private JTextField tfPopu = null;private JLabel lblGdp = null;private JTextField tfGDP = null;private JButton btnAdd = null;private JButton btnDe

14、l = null;private JButton btnModify = null;private JButton btnQuery = null;private JButton btnUpPage = null;private JButton btnDownPage = null; private JTable table = null;TableModel tm = null;JScrollPane jsp = null;private JTextField getTfName() if (tfName = null) tfName = new JTextField();tfName.se

15、tBounds(new Rectangle(89, 17, 173, 27);return tfName;private JComboBox getCbRegion() if (cbRegion = null) String str = " 請選擇 ", " 計算機科學(xué)與技術(shù) ", " 信息安全 ", " 網(wǎng)絡(luò)工程 ", " 信息與計算科學(xué) ", " 物流工程 ", " 軟件工程 "cbRegion = new JComboBox(str);cbRegio

16、n.setBounds(new Rectangle(360, 17, 229, 28);return cbRegion;private JTextField getTfArea() if (tfArea = null) tfArea = new JTextField();tfArea.setBounds(new Rectangle(89, 61, 173, 27);return tfArea; private JTextField getTfPopu() if (tfPopu = null) tfPopu = new JTextField(); tfPopu.setBounds(new Rec

17、tangle(360, 61, 229, 28); return tfPopu; private JTextField getTfGDP() if (tfGDP = null) tfGDP = new JTextField(); tfGDP.setBounds(new Rectangle(89, 105, 173, 27); return tfGDP;private JButton getBtnAdd() if (btnAdd = null) btnAdd = new JButton();btnAdd.setBounds(new Rectangle(46, 152, 71, 30); btnA

18、dd.addActionListener(this);btnAdd.setText(" 添加 ");return btnAdd;private JButton getBtnUpPage() if (btnUpPage = null) btnUpPage = new JButton(" 上一頁 "); btnUpPage.addActionListener(this); btnUpPage.setBounds(200, 390, 80, 30);return btnUpPage;private JButton getBtnDownPage() if (bt

19、nDownPage = null) btnDownPage = new JButton("下一頁 ");btnDownPage.addActionListener(this); btnDownPage.setBounds(300, 390, 80, 30);return btnDownPage;private JButton getBtnDel() if (btnDel = null) btnDel = new JButton();btnDel.setBounds(new Rectangle(189, 152, 71, 30); btnDel.addActionListen

20、er(this);刪除 ");btnDel.setText(" return btnDel;private JButton getBtnModify() if (btnModify = null) btnModify = new JButton(); btnModify.setBounds(new Rectangle(342, 152, 71, 30); btnModify.addActionListener(this);btnModify.setText("更新 ");return btnModify;private JButton getBtnQue

21、ry() if (btnQuery = null) btnQuery = new JButton();btnQuery.setBounds(new Rectangle(484, 152, 71, 30); btnQuery.addActionListener(this);btnQuery.setText(" 查找 ");return btnQuery;JFrame getJFrame() if (jFrame = null) jFrame = new JFrame(); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE

22、); jFrame.setBounds(new Rectangle(450, 250, 610, 454); jFrame.setContentPane(getJContentPane();jFrame.setTitle(" 學(xué)生信息管理系統(tǒng) ");return jFrame;private JPanel getJContentPane() if (jContentPane = null) lblGdp = new JLabel();lblGdp.setBounds(new Rectangle(15, 106, 63, 27); lblGdp.setText("

23、家庭住址 ");lblPopu = new JLabel(); lblPopu.setBounds(new Rectangle(285, 61, 63, 27); lblPopu.setText("成績 ");lblArea = new JLabel();lblArea.setBounds(new Rectangle(15, 61, 63, 27); lblArea.setText("學(xué)院 ");lblRegion = new JLabel();lblRegion.setBounds(new Rectangle(285, 17, 63, 27)

24、;lblRegion.setText("專業(yè) ");lblName = new JLabel(); lblName.setBounds(new Rectangle(15, 17, 63, 27); lblName.setText("姓名 ");jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(lblName, null); jContentPane.add(getTfName(), null); jContentPane.add(lblRegion, n

25、ull); jContentPane.add(getCbRegion(), null); jContentPane.add(lblArea, null); jContentPane.add(getTfArea(), null); jContentPane.add(lblPopu, null); jContentPane.add(getTfPopu(), null); jContentPane.add(lblGdp, null); jContentPane.add(getTfGDP(), null); jContentPane.add(getBtnAdd(), null); jContentPa

26、ne.add(getBtnDel(), null); jContentPane.add(getBtnModify(), null); jContentPane.add(getBtnQuery(), null); jContentPane.add(getBtnUpPage(), null); jContentPane.add(getBtnDownPage(), null); return jContentPane;public void actionPerformed(ActionEvent e) if (e.getActionCommand().equals(" selectAll(

27、); else if (e.getActionCommand().equals(" selectDownPage(); else if (e.getActionCommand().equals(" selectUpPage(); else if (e.getActionCommand().equals(" if (tfName.getText().equals("")| cbRegion.getSelectedItem().equals("| tfArea.getText().equals("") | tfPopu

28、.getText().equals("") | tfGDP.getText().equals("") JOptionPane.showMessageDialog(jFrame, " else Student co = new Student(); co.setSname(tfName.getText(); co.setSpro(String) cbRegion.getSelectedItem(); co.setSdept(tfArea.getText(); co.setSgrade(tfPopu.getText();查找 ") 下一頁

29、 ") 上一頁 ") 添加") 請選擇 ")請?zhí)顚懴嚓P(guān)信息 ");co.setSaddress(tfGDP.getText();boolean flag = SchoolOperate.add(co);if (flag) JOptionPane.showMessageDialog(jFrame, " else JOptionPane.showMessageDialog(jFrame, " else if (e.getActionCommand().equals("if(!tfName.getText().equal

30、s("")String key = tfName.getText();boolean flag = SchoolOperate.del(key);if (flag) JOptionPane.showMessageDialog(jFrame, " else JOptionPane.showMessageDialog(jFrame, "else JOptionPane.showMessageDialog(jFrame, " else if (e.getActionCommand().equals("if (!tfName.getText(

31、).equals("")&& !cbRegion.getSelectedItem().equals("&& !tfArea.getText().equals("")&& !tfPopu.getText().equals("")&& !tfGDP.getText().equals("") Student cou = new Student(); cou.setSname(tfName.getText();cou.setSpro(String)c

32、bRegion.getSelectedItem(); cou.setSdept(tfPopu.getText();cou.setSgrade(tfArea.getText();cou.setSaddress(tfGDP.getText();boolean flag = SchoolOperate.modify(cou); if(flag)JOptionPane.showMessageDialog(jFrame, "else JOptionPane.showMessageDialog(jFrame, " else JOptionPane.showMessageDialog(j

33、Frame, "public void setValues(Object obj) 添加成功添加失敗刪除") 刪除成功刪除失敗");");");");請?zhí)顚懸粋€國家名字 ");更新") 請選擇 ")更新成功 ");更新失敗 ");請?zhí)顚懴鄳?yīng)的信息 ");public void selectAll() String spro=cbRegion.getSelectedItem().toString(); List<Student> list = SchoolOpera

34、te.queryFirst(); Object datavalues = new Objectlist.size()5;for (int i = 0; i < list.size(); i+) Student co = list.get(i); datavaluesi0 = co.getSname(); datavaluesi1 = co.getSpro(); datavaluesi2 = co.getSdept(); datavaluesi3 = co.getSgrade(); datavaluesi4 = co.getSaddress(); tm = new TableModel()

35、; this.setValues(datavalues); table = new JTable(tm); table.updateUI();JScrollPane jsp = new JScrollPane(table); jsp.setBounds(0, 200, 600, 182); jContentPane.add(jsp); public void selectDownPage() List<Student> list = SchoolOperate.downPage(); if (list != null) Object datavalues = new Objectl

36、ist.size()5; for (int i = 0; i < list.size(); i+) Student co = list.get(i);datavaluesi0 = co.getSname(); datavaluesi1 = co.getSpro();datavaluesi2 = co.getSdept(); datavaluesi3 = co.getSgrade();datavaluesi4 = co.getSaddress(); this.setValues(datavalues); table.updateUI();已經(jīng)是最后一頁 "); else JOpt

37、ionPane.showMessageDialog(jFrame, " public void selectUpPage() List<Student> list = SchoolOperate.upPage();if (list != null) Object datavalues = new Objectlist.size()5; for (int i = 0; i < list.size(); i+) Student co = list.get(i);datavaluesi0 = co.getSname();datavaluesi1 = co.getSpro(

38、);datavaluesi2 = co.getSdept();datavaluesi3 = co.getSgrade();datavaluesi4 = co.getSaddress();this.setValues(datavalues); table.updateUI(); else 已經(jīng)是第一頁 ");JOptionPane.showMessageDialog(jFrame, "針對數(shù)據(jù)庫的增刪改查操作方法類 ;package edu;public class SchoolOperate static int i = 1;static int x = 0; public

39、 static List<Student> queryFirst() String sql = "select top 9 * from student order by sname" List<Student> list = query(sql);return list;public static List<Student> downPage() int y = queryCount(); List<Student> list = null;int count = 0; /總頁數(shù)if (y % 5 = 0) count =

40、y / 5; else count = y / 5 + 1;if (i < count) x = (+i) - 1) * 5;String sql = "select top "+ " * from (select * from student where sname not in(select top "+ x + " sname from student order by sname) as A " list = query(sql); else if (i >= count) list = null;return l

41、ist;public static boolean login(String userName,String password) boolean result=false;Connection con = DBConnection.getConnection();String sql="select passWord from login where userName="PreparedStatement ps = null;ResultSet rs = null;tryps=con.prepareStatement(sql);ps.setObject(1, userNam

42、e); rs=ps.executeQuery();while(rs.next() if(password.equals(rs.getString("passWord").trim() result=true;catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace();finallytryif(rs!=null)rs.close();catch(SQLException e)e.printStackTrace();try if(ps!=null) ps.close(); catch

43、(SQLException e) e.printStackTrace(); tryif(con!=null)con.close();catch(SQLException e)e.printStackTrace(); return result;public static int queryCount() int count = 0;Connection conn = DBConnection.getConnection(); String sql = "select count(*) from student"Statement stmt = null;ResultSet

44、rs = null;try stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next() count = rs.getInt(1); catch (SQLException e) e.printStackTrace(); finally try if (rs != null)rs.close();if (stmt != null)stmt.close();if (conn != null)conn.close(); catch (SQLException e) e.printStackTrace();r

45、eturn count;public static List<Student> query(String sql) Connection conn = DBConnection.getConnection(); List<Student> list = new ArrayList<Student>(); Statement stmt = null;ResultSet rs = null;try stmt = conn.createStatement();rs = stmt.executeQuery(sql);while (rs.next() Student

46、cou = new Student(); cou.setSname(rs.getString(1);cou.setSpro(rs.getString(2); cou.setSdept(rs.getString(3);cou.setSgrade(rs.getString(4); cou.setSaddress(rs.getString(5);list.add(cou); catch (SQLException e) e.printStackTrace(); finally try if (rs != null)rs.close();if (stmt != null) stmt.close();i

47、f (conn != null) conn.close(); catch (SQLException e) e.printStackTrace(); return list;public static List<Student> upPage() List<Student> list = null;if (i > 2) x = (-i) - 1) * 5;String sql = "select top "+ " * from (select * from student where sname not in(select top &

48、quot;+ x + " sname from student order by sname) as A " list = query(sql); else if (i = 2) list = queryFirst();i-; else if (i < 2) list = null;return list;public static boolean add(Student cou) Connection conn = DBConnection.getConnection(); PreparedStatement pstmt = null;String sql = &q

49、uot;insert into student values(,)"int x = 0;try pstmt = conn.prepareStatement(sql);pstmt.setString(1, cou.getSname();pstmt.setString(2, cou.getSpro(); pstmt.setString(3, cou.getSdept();pstmt.setString(4, cou.getSgrade();pstmt.setString(5, cou.getSaddress();x = pstmt.executeUpdate(); catch (SQLE

50、xception e) e.printStackTrace(); finally try if (pstmt != null) pstmt.close();if (conn != null) conn.close(); catch (SQLException e) e.printStackTrace();if (x = 1) return true; else return false;public static boolean del(String key) Connection conn = DBConnection.getConnection(); PreparedStatement s

51、tmt = null;String sql = "delete from student where sname =" int x = 0;try stmt = conn.prepareStatement(sql); stmt.setString(1, key);x = stmt.executeUpdate(); catch (SQLException e) e.printStackTrace(); finally try if (stmt != null) stmt.close();if (conn != null) conn.close(); catch (SQLException e) e.printStackTrace();if (x = 1) return true; else return false;public static boolean modify(Student cou) Connection conn = DBConnection.getConnection();PreparedStatement pstmt = null;String sql = "update student set spro=,sdept=,sgrade=,saddress= where sname =" int x = 0;try ps

溫馨提示

  • 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)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論