版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
应用JavaSwingGUI界面技术实现的可演示JavaJDBC数据库操作技术的应用实例本例如使用MySql数据库系统本例如可演示JavaJDBC数据库操作技术的应用实例,实现了对数据库中数据库表EBook的增、删、改和查的功能。用于获取数据库连接的DataBaseConnection程序类代码例如publicclassDatabaseConnection{publicStringclassDriver="com.mysql.jdbc.Driver";publicStringdbUrl="jdbc:mysql://localhost:3306/database";publicStringuser="root";publicStringpassword="admin";publicDatabaseConnection(){}publicConnectiongetConnection()throwsSQLException,ClassNotFoundException{Class.forName(classDriver);Connectionconnection=DriverManager.getConnection(dbUrl,user,password);returnconnection;}}主程序类MainFrame程序代码例如importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.ResultSetMetaData;importjava.sql.Statement;importjavax.swing.table.DefaultTableModel;ng.JFrame{privateDatabaseConnectiondc=newDatabaseConnection();privateConnectioncon;privateStatementstmt;privatePreparedStatementpstmt;privateResultSetrst;privateResultSetMetaDatametaData;privateStringsqlString;privateString[]columnNames;privateObject[][]dataRows;privateintselectIndex;publicMainFrame(){initComponents();}privatevoidinitComponents(){jPanel1=newjavax.swing.JPanel();jLabel2=newjavax.swing.JLabel();jLabel3=newjavax.swing.JLabel();jLabel4=newjavax.swing.JLabel();addName=newjavax.swing.JTextField();addKind=newjavax.swing.JTextField();addPrice=newjavax.swing.JTextField();add=newjavax.swing.JButton();jPanel2=newjavax.swing.JPanel();statusBar=newjavax.swing.JLabel();jScrollPane1=newjavax.swing.JScrollPane();bookTable=newjavax.swing.JTable();jPanel3=newjavax.swing.JPanel();jLabel5=newjavax.swing.JLabel();jLabel6=newjavax.swing.JLabel();jLabel7=newjavax.swing.JLabel();modifyName=newjavax.swing.JTextField();modifyKind=newjavax.swing.JTextField();modifyPrice=newjavax.swing.JTextField();jLabel8=newjavax.swing.JLabel();jLabel9=newjavax.swing.JLabel();modify=newjavax.swing.JButton();modifyId=newjavax.swing.JLabel();delete=newjavax.swing.JButton();queryBook=newjavax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle("\u6570\u636e\u5e93\u5b9e\u9a8c\u7a0b\u5e8f");jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(newjava.awt.Color(0,0,0)),"\u589e\u52a0"));jLabel2.setText("ebookName");jLabel3.setText("ebookKind");jLabel4.setText("ebookPrice");add.setText("\u6dfb\u52a0");add.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){addActionPerformed(evt);}});org.jdesktop.layout.GroupLayoutjPanel1Layout=neworg.jdesktop.layout.GroupLayout(jPanel1);jPanel1.setLayout(jPanel1Layout);jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(jPanel1Layout.createSequentialGroup().add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(jPanel1Layout.createSequentialGroup().addContainerGap().add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(jLabel2).add(jLabel4,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,62,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(jLabel3)).add(19,19,19).add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(addPrice,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,78,Short.MAX_VALUE).add(org.jdesktop.layout.GroupLayout.TRAILING,addName,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,78,Short.MAX_VALUE).add(org.jdesktop.layout.GroupLayout.TRAILING,addKind,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,78,Short.MAX_VALUE))).add(jPanel1Layout.createSequentialGroup().add(53,53,53).add(add))).add(43,43,43)));jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(jPanel1Layout.createSequentialGroup().addContainerGap().add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel2).add(addName,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).add(29,29,29).add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel3).add(addKind,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).add(29,29,29).add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel4).add(addPrice,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED,37,Short.MAX_VALUE).add(add).addContainerGap()));jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(newjava.awt.Color(0,0,0)),"\u72b6\u6001"));statusBar.setText("\u4ea7\u751f\u72b6\u6001\u4fe1\u606f");org.jdesktop.layout.GroupLayoutjPanel2Layout=neworg.jdesktop.layout.GroupLayout(jPanel2);jPanel2.setLayout(jPanel2Layout);jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(jPanel2Layout.createSequentialGroup().add(42,42,42).add(statusBar,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,475,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addContainerGap(157,Short.MAX_VALUE)));jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(statusBar,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,11,Short.MAX_VALUE));jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(newjava.awt.Color(0,0,0)),"\u6570\u636e"));jScrollPane1.addMouseListener(newjava.awt.event.MouseAdapter(){publicvoidmouseClicked(java.awt.event.MouseEventevt){jScrollPane1MouseClicked(evt);}});bookTable.setModel(newjavax.swing.table.DefaultTableModel(newObject[][]{{null,null,null,null},{null,null,null,null},{null,null,null,null},{null,null,null,null}},newString[]{"Title1","Title2","Title3","Title4"}));bookTable.addMouseListener(newjava.awt.event.MouseAdapter(){publicvoidmouseClicked(java.awt.event.MouseEventevt){bookTableMouseClicked(evt);}});jScrollPane1.setViewportView(bookTable);jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(newjava.awt.Color(0,0,0)),"\u4fee\u6539"));jLabel5.setText("ebookName");jLabel6.setText("ebookKind");jLabel7.setText("ebookPrice");jLabel8.setText("ebook_id");modify.setText("\u4fee\u6539");modify.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){modifyActionPerformed(evt);}});delete.setText("\u5220\u9664");delete.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){deleteActionPerformed(evt);}});org.jdesktop.layout.GroupLayoutjPanel3Layout=neworg.jdesktop.layout.GroupLayout(jPanel3);jPanel3.setLayout(jPanel3Layout);jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(jPanel3Layout.createSequentialGroup().addContainerGap().add(jPanel3Layout.createSequentialGroup().add(jLabel7,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,62,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addContainerGap(211,Short.MAX_VALUE)).add(jPanel3Layout.createSequentialGroup().add(jLabel6).addContainerGap(219,Short.MAX_VALUE)).add(org.jdesktop.layout.GroupLayout.TRAILING,jPanel3Layout.createSequentialGroup().add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING).add(org.jdesktop.layout.GroupLayout.LEADING,jPanel3Layout.createSequentialGroup().add(jLabel5).add(25,25,25).add(modifyName,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,148,Short.MAX_VALUE)).add(org.jdesktop.layout.GroupLayout.LEADING,jPanel3Layout.createSequentialGroup().add(jLabel8).add(21,21,21).add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(jPanel3Layout.createSequentialGroup().add(10,10,10).add(modifyId,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,148,Short.MAX_VALUE)).add(jPanel3Layout.createSequentialGroup().add(jLabel9,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,123,Short.MAX_VALUE).add(35,35,35)).add(jPanel3Layout.createSequentialGroup().add(10,10,10).add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(modifyPrice,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,148,Short.MAX_VALUE).add(modifyKind,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,148,Short.MAX_VALUE).add(org.jdesktop.layout.GroupLayout.TRAILING,delete)))))).add(46,46,46)))).add(jPanel3Layout.createSequentialGroup().add(21,21,21).add(modify).addContainerGap(205,Short.MAX_VALUE)));jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(jPanel3Layout.createSequentialGroup().add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(jLabel9,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,8,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(jPanel3Layout.createSequentialGroup().addContainerGap().add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel8).add(modifyId,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,20,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))).add(15,15,15).add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING).add(jLabel5).add(modifyName,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).add(16,16,16).add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel6).add(modifyKind,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).add(19,19,19).add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jLabel7).add(modifyPrice,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).add(17,17,17).add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(modify).add(delete)).addContainerGap()));queryBook.setText("\u67e5\u8be2");queryBook.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){queryBookActionPerformed(evt);}});org.jdesktop.layout.GroupLayoutlayout=neworg.jdesktop.layout.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(layout.createSequentialGroup().addContainerGap().add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(layout.createSequentialGroup().add(jPanel1,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(jPanel3,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(17,17,17).add(queryBook,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,107,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)).add(jPanel2,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).add(org.jdesktop.layout.GroupLayout.TRAILING,jScrollPane1,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,684,Short.MAX_VALUE)).addContainerGap()));layout.setVerticalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(layout.createSequentialGroup().addContainerGap().add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(jPanel1,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).add(layout.createSequentialGroup().add(136,136,136).add(queryBook)).add(jPanel3,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,225,Short.MAX_VALUE)).addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(jScrollPane1,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,181,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(jPanel2,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addContainerGap()));pack();}//</editor-fold>//GEN-END:initComponentsprivatevoidbookTableMouseClicked(java.awt.event.MouseEventevt){//GEN-FIRST:event_bookTableMouseClickedselectIndex=bookTable.getSelectedRow();modifyId.setText(""+dataRows[selectIndex][0]);modifyName.setText((String)dataRows[selectIndex][1]);modifyKind.setText((String)dataRows[selectIndex][2]);modifyPrice.setText(""+dataRows[selectIndex][3]);}//GEN-LAST:event_bookTableMouseClickedprivatevoiddeleteActionPerformed(java.awt.event.ActionEventevt){//GEN-FIRST:event_deleteActionPerformedintebook_id=Integer.parseInt(modifyId.getText());deleteInfo(ebook_id);queryInfo();updateTable(dataRows,columnNames);setStatusBar("数据删除成功");}//GEN-LAST:event_deleteActionPerformedprivatevoidmodifyActionPerformed(java.awt.event.ActionEventevt){//GEN-FIRST:event_modifyActionPerformedintebook_id=Integer.parseInt(modifyId.getText());StringebookName=modifyName.getText();StringebookKind=modifyKind.getText();floatebookPrice=Float.parseFloat(modifyPrice.getText());updateInfo(ebook_id,ebookName,ebookKind,ebookPrice);queryInfo();updateTable(dataRows,columnNames);setStatusBar("数据库更新成功");}//GEN-LAST:event_modifyActionPerformedprivatevoidaddActionPerformed(java.awt.event.ActionEventevt){//GEN-FIRST:event_addActionPerformedStringebookName=addName.getText();StringebookKind=addKind.getText();floatebookPrice=Float.parseFloat(addPrice.getText());insertInfo(ebookName,ebookKind,ebookPrice);queryInfo();updateTable(dataRows,columnNames);setStatusBar("数据添加成功");}//GEN-LAST:event_addActionPerformedprivatevoidqueryBookActionPerformed(java.awt.event.ActionEventevt){//GEN-FIRST:event_queryBookActionPerformedqueryInfo();updateTable(dataRows,columnNames);setStatusBar("数据查询成功");}//GEN-LAST:event_queryBookActionPerformed//刷新表格信息publicvoidupdateTable(Object[][]dataRows,String[]columnNames){//DefaultTableModeltableModel=(DefaultTableModel)bookTable.getModel();DefaultTableModelnewTableModel=newDefaultTableModel(dataRows,columnNames);bookTable.setModel(newTableModel);}//向数据库中插入信息publicvoidinsertInfo(StringebookName,StringebookKind,floatebookPrice){try{sqlString="insertintoEBook(ebookName,ebookKind,ebookPrice)values(?,?,?)";con=dc.getConnection();pstmt=con.prepareStatement(sqlString);pstmt.setString(1,ebookName);pstmt.setString(2,ebookKind);pstmt.setFloat(3,ebookPrice);pstmt.executeUpdate();}catch(Exceptione){e.printStackTrace();}finally{try{con.close();}catch(Exceptione){e.printStackTrace();}}}//删除数据库中的信息publicvoiddeleteInfo(intebook_id){try{con=dc.getConnection();sqlString="deletefromEBookwhereebook_id='"+ebook_id+"'";stmt=con.createStatement();stmt.executeUpdate(sqlString);}catch(Exceptione){e.printStackTrace();}finally{try{con.close();}catch(Exceptione){e.printStackTrace();}}}publicvoidupdateInfo(intebook_id,StringebookName,StringebookKind,floatebookPrice){try{con=dc.getConnection();sqlString="updateEBooksetebookName=?,ebookKind=?,ebookPrice=?whereebook_id=?";pstmt=con.prepareStatement(sqlString);pstmt.setString(1,ebookName);pstmt.setString(2,ebookKind);pstmt.setFloat(3,ebookPrice);pstmt.setInt(4,ebook_id);pstmt.executeUpdate();}catch(Exceptione){e.printStackTrace();}finally{try{con.close();}catch(Exceptione){e.printStackTrace();}}}//查询相应的信息publicvoidqueryInfo(){try{con=dc.getConnection();stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);sqlString="select*fromEBook";rst=stmt.executeQuery(sqlString);metaData=rst.getMetaData();intnumberOfColumns=metaData.getColumnCount();columnNames=newString[numberOfColumns];for(intcolumn=0;column<numberOfColumns;column++){columnNames[column]=metaData.getColumnLabel(column+1);}introwCounter=0;if(rst.last()){rowCounter=rst.getRow();}rst.beforeFirst();dataRows=newObject[rowCounter][numberOfColumns]
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年内审员试题及答案详解
- PETS5全国英语等级考试五级模拟试题及详细答案解析(完整版)
- 船舶消防设施安装工安全操作规程
- 工程或机械岗位招聘笔试题及答案
- 脑室分流管堵塞早期症状识别共识
- 2025-2026学年江苏省扬中市初中历史七年级期末下册自测黑金考题附答案
- 2026年动物防疫员(高级)理论考试题(附答案)
- OC算法面试题及详细答案(实战版)
- 古石桥桥墩加固及桥面修缮方案
- 中级钳工试题及答案
- 无人机装调检修工艺流程文件
- 古诗词诵读《将进酒》课件+2025-2026学年统编版高二语文选择性必修上册
- 文库发布:脑外伤课件
- 校园保安法制教育专题培训
- 设备检修后清理管理办法
- 介绍中医护理门诊
- 黄山市自然保护地整合优化方案
- 公立医院低值耗材管理的财会监督改进研究
- 国家开放大学2022《土木工程力学(本)》形考作业1-5参考答案
- JTG-GQS-010-85公路桥涵设计图
- JC-T 2104-2012水泥工业用耐磨件堆焊通用技术条件
评论
0/150
提交评论