連接數(shù)據(jù)庫模塊_第1頁
連接數(shù)據(jù)庫模塊_第2頁
連接數(shù)據(jù)庫模塊_第3頁
連接數(shù)據(jù)庫模塊_第4頁
連接數(shù)據(jù)庫模塊_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、.連接數(shù)據(jù)庫模塊。主要是連接數(shù)據(jù)庫相關(guān)的端口和用戶名、密碼以及Jdbc連接 本地?cái)?shù)據(jù)庫等信息,其主要實(shí)現(xiàn)代碼如下:public class DatabaseBean public static Connection getConnection() Connection conn = null;String driver = oracle.jdbc.driver.OracleDriver”;String url = jdbc:oracle:thin:localhost:1521:xe;String user = hr;String password = oracle;try Class.forN

2、ame(driver);conn = DriverManager.getConnection(url, user, password); catch (ClassNotFoundException | SQLException ex) Logger.getLogger(DatabaseBean.class.getName().log(Level.SEVERE, null, ex);return conn;public static void close(ResultSet rs, Statement st, Connection conn) try if (rs != null) rs.clo

3、se();if(st != null) st.close();if(conn != null) conn.close(); catch (SQLException ex) Logger.getLogger(DatabaseBean.class.getName().log(Level.SEVERE, null, ex);2 .調(diào)用存儲(chǔ)過程(此處只舉例學(xué)生表存儲(chǔ)過程)調(diào)用學(xué)生表的查詢所有信息、根據(jù)學(xué)號(hào)查詢學(xué)生表的所有信息返 回游標(biāo)顯示出來,以及插入、更新等存儲(chǔ)過程,具體實(shí)現(xiàn)代碼如下: public class StudentDaoImpl implements IStudentDao public

4、 static ResultSet executQuery(String sqlString) throw new UnsupportedOperationException(Not supported yet.); /To change body of generated methods, choose Tools | Templates.Overridepublic List getAllStudent() List students = new ArrayList();Connection conn = null;CallableStatement cs = null;ResultSet

5、 rs = null;try conn = DatabaseBean.getConnection();cs = conn.prepareCall(call students.select_all(?);cs.registerOutParameter(1, OracleTypes.CURSOR);cs.executeQuery();rs = (ResultSet) cs.getObject(1);while (rs.next() Student stu = new Student();stu.setSno(rs.getString(sno);stu.setSname(rs.getString(s

6、name);stu.setSsex(rs.getString(ssex);stu.setSage(rs.getInt(sage);stu.setSdept(rs.getString(sdept);students.add(stu); catch (SQLException ex) Logger.getLogger(StudentDaoImpl.class.getName().log(Level.SEVERE, null, ex); finally DatabaseBean.close(rs, cs, conn);return students;Overridepublic Student ge

7、tStudent(String sno) Student stu = new Student();Connection conn = null;CallableStatement cs = null;ResultSet rs = null;try conn = DatabaseBean.getConnection();cs = conn.prepareCall(call students.select_sno(?,?)”);cs.setString(1, sno);cs.registerOutParameter(2, OracleTypes.CURSOR);cs.executeQuery();

8、rs = (ResultSet) cs.getObject(2);if (rs.next() stu.setSno(rs.getString(sno);stu.setSname(rs.getString(sname);stu.setSsex(rs.getString(ssex);stu.setSage(rs.getInt(sage);stu.setSdept(rs.getString(sdept); catch (SQLException ex) Logger.getLogger(StudentDaoImpl.class.getName().log(Level.SEVERE, null, ex

9、); finally DatabaseBean.close(rs, cs, conn);return stu;Overridepublic boolean insertStudent(Student stu) Connection conn = null;CallableStatement cs = null;try conn = DatabaseBean.getConnection();cs = conn.prepareCall(call students.insert_msg(?,?,?,?,?)”);cs.setString(1, stu.getSno();cs.setString(2,

10、 stu.getSname();cs.setString(3, stu.getSsex();cs.setInt(4, stu.getSage();cs.setString(5, stu.getSdept();cs.executeUpdate();return true; catch (SQLException ex) Logger.getLogger(StudentDaoImpl.class.getName().log(Level.SEVERE, null, ex); return false; finally DatabaseBean.close(null, cs, conn);Overri

11、depublic boolean updateStudent(Student stu) Connection conn = null;CallableStatement cs = null;try conn = DatabaseBean.getConnection();cs = conn.prepareCall(call students.update_msg(?,?,?,?,?)”);cs.setString(1, stu.getSno();cs.setString(2, stu.getSname();cs.setString(3, stu.getSsex();cs.setInt(4, st

12、u.getSage();cs.setString(5, stu.getSdept();cs.executeUpdate();return true; catch (SQLException ex) Logger.getLogger(StudentDaoImpl.class.getName().log(Level.SEVERE, null, ex); return false; finally DatabaseBean.close(null, cs, conn);Overridepublic boolean deleteStudent(String sno) Connection conn =

13、null;CallableStatement cs = null;try conn = DatabaseBean.getConnection();cs = conn.prepareCall(call students.delete_sno(?);cs.setString(1, sno);cs.executeUpdate();return true; catch (SQLException ex) Logger.getLogger(StudentDaoImpl.class.getName().log(Level.SEVERE, null, ex); return false; finally D

14、atabaseBean.close(null, cs, conn);3.表模型模塊:public class StudentTableModel extends AbstractTableModelList rowsData;List columnsData;public StudentTableModel() rowsData = DaoFactory.getStudentDao().getAllStudent(); setColumnNames();public StudentTableModel(String sno) rowsData = new ArrayList();rowsDat

15、a.add(DaoFactory.getStudentDao().getStudent(sno); setColumnNames();public int getRowCount() return rowsData.size();public int getColumnCount() return columnsData.size();public Object getValueAt(int rowIndex, int columnIndex) switch (columnIndex) case 0:return rowsData.get(rowIndex).getSno();case 1:r

16、eturn rowsData.get(rowIndex).getSname();case 2:return rowsData.get(rowIndex).getSsex();case 3:return rowsData.get(rowIndex).getSage();case 4:return rowsData.get(rowIndex).getSdept();default:return s;private void setColumnNames() columnsData = new ArrayList();columnsData.add(學(xué)號(hào));columnsData.add(姓名);c

17、olumnsData.add(性別);columnsData.add(年齡);columnsData.add(院系);public String getColumnName(int column) return columnsData.get(column);4.主界面模塊:主界面采用Form窗體,來實(shí)現(xiàn)操作主界面窗體,里面包含有三個(gè)按鈕,分 別為學(xué)生信息管理、課程信息管理、成績信息管理。用戶可通過點(diǎn)擊這三個(gè)按鈕 分別進(jìn)入相應(yīng)的功能模塊。其實(shí)現(xiàn)代碼主要有:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) j

18、ava.awt.EventQueue.invokeLater(new Runnable() public void run() new studentFrame().setVisible(true););private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) java.awt.EventQueue.invokeLater(new Runnable() public void run() new courseFrame().setVisible(true););private void jButton3Action

19、Performed(java.awt.event.ActionEvent evt) ( java.awt.EventQueue.invokeLater(new Runnable() (public void run() (new gradeFrame().setVisible(true););5.學(xué)生信息管理模塊:(1)此模塊實(shí)現(xiàn)學(xué)生信息管理,主要實(shí)現(xiàn)學(xué)生信息的添加、查詢、修改和刪除,運(yùn)行截圖如下:話輸入要查句的學(xué)號(hào):學(xué)話輸入要查句的學(xué)號(hào):學(xué)W眺名T |查詢浦加信息酬信自刪除信,性刖年檔學(xué)站扁9512101 李勇男19計(jì)算機(jī)條951210295121039512101 李勇男19計(jì)算機(jī)條951

20、2102951210395211019521102952110395311019531102/- Xfi工202022?1201819計(jì)翼機(jī)兼 計(jì)賓機(jī)崩1= m共 故學(xué)弩 數(shù)學(xué)窘學(xué)號(hào):9512101年都:19I算機(jī)蚤:其主要實(shí)現(xiàn)代碼有:private void queryButtonActionPerformed(java.awt.event.ActionEvent evt) (String sno = snoQueryText.getText().trim();JOptionPane.showMessageDialog(this, sno);if (”.equals(sno) (sm = n

21、ew StudentTableModel(); else (sm = new StudentTableModel(sno);studentTable.setModel(sm);snoQueryText.setText();private void addStudentButtonActionPerformed(java.awt.event.ActionEvent evt) ( AddStudentDialog dialog = new AddStudentDialog(this, true);dialog.setLocationRelativeTo(this);dialog.setVisibl

22、e(true);if(AddStudentDialog.RET_OK = dialog.getReturnStatus() (sm = new StudentTableModel();studentTable.setModel(sm);private void studentTableMouseClicked(java.awt.event.MouseEvent evt) (int rowNum = studentTable.getSelectedRow();if (rowNum = 0) (String sno = (String) studentTable.getVdueAt(rowNum,

23、 0);if (snoText.getText().trim().equals(sno) (return;Student stu = DaoFactory.getStudentDao().getStudent(sno);snoText.setText(stu.getSno();snameText.setText(stu.getSname();if (男”.equals(stu.getSsex() (maleRadioButton.setSelected(true); else (femaleRadioButton.setSelected(true);sageText.setText(Strin

24、g.valueOf(stu.getSage();sdeptText.setText(stu.getSdept();private void deleteStudentButtonActionPerformed(java.awt.event.ActionEvent evt) (int rowNum = studentTable.getSelectedRow();if (rowNum = 0) (String sno = snoText.getText().trim();int choise = JOptionPane.showConfirmDialog(this,是否要?jiǎng)h除學(xué)生的信息 內(nèi)容”,”

25、提示”,JOptionPane.YES_NO_OPTION);if(choise = JOptionPane.YES_OPTION) (if (!DaoFactory.getStudentDao().deleteStudent(sno) (JOptionPane.showMessageDialog(this,”學(xué)生信息刪除失敗,請與系統(tǒng)管理員聯(lián)系!”);sm = new StudentTableModel();studentTable.setModel(sm); else (JOptionPane.showMessageDialog(this,”請選擇要?jiǎng)h除的學(xué)生信息!”);private v

26、oid updateStudentButtonActionPerformed(java.awt.event.ActionEvent evt) (String sno = snoText.getText().trim();String sname = snameText.getText().trim();String ssex;if (maleRadioButton.isSelected() (ssex = maleRadioButton.getText(); else (ssex = femaleRadioButton.getText();String sage = sageText.getT

27、ext().trim();String sdept = sdeptText.getText().trim();if(!validateData(sno, sname, ssex, sage, sdept) ( return;if (!DaoFactory.getStudentDao().findStudent(sno) (JOptionPane.showMessageDialog(this,”指定學(xué)號(hào)的學(xué)生記錄不存在,請重 新輸入!);return;Student stu = new Student(sno, sname, ssex, Integer.parseInt(sage), sdept

28、);如果數(shù)據(jù)驗(yàn)證正確,則插入學(xué)生記錄內(nèi)容if (!DaoFactory.getStudentDao().updateStudent(stu) (JOptionPane.showMessageDialog(this,”學(xué)生記錄更新失敗,請檢查輸入的 數(shù)據(jù)內(nèi)容!);sm = new StudentTableModel();studentTable.setModel(sm);private void queryButtonMouseClicked(java.awt.event.MouseEvent evt) (/ TODO add your handling code here:String sno

29、 = snoQueryText.getText().trim();if (”.equals(sno) (sm = new StudentTableModel();sdept) (字!”)sdept) (字!”);間!);sm = new StudentTableModel(sno);studentTable.setModel(sm);snoQueryText.setText();private boolean validateData(String sno, String sname, String ssex, String sage, Stringif (”.equals(sno) | !Tools.isNumeric(sno) | sno.length() != 7) (JOptionPane.showMessageDialog(this,”請輸入正確的學(xué)號(hào),并且學(xué)號(hào)為數(shù)snoText.requestFocus(); return fa

溫馨提示

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

評論

0/150

提交評論