📄 dbrowsetframe.java
字号:
package dbrowset;import java.awt.*;import java.awt.event.*;import java.sql.*;import java.util.*;import javax.sql.*;import javax.swing.*;import javax.swing.table.*;import sun.jdbc.rowset.*; //引用rowset包/** * Title: 使用RowSet进行数据库操作 * Description: 教学示范 * Copyright: Copyright (c) 2003 * Company: 北京师范大学计算机系 * @author 孙一林 * @version 1.0 */public class dbRowSetFrame extends JFrame { private JPanel contentPane; private Button jdbcQueryButton = new Button(); String[] title = {"学号", "姓名", "年龄", "性别", "系名"}; //定义显示数据表格的标题 JdbcRowSet jRowSet = null; //定义数据库查询的JdbcRowSet结果集 CachedRowSet cRowSet = null; boolean lastQuery = false; //上次查询是否成功 Vector vector; //定义存储结果集数据的数组 AbstractTableModel tm; //定义显示数据表格的抽象类 JScrollPane scroll; //定义装载数据表格的容器 private Button cachedQuerybutton = new Button(); public dbRowSetFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { //初始化用户界面 contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(null); this.setSize(new Dimension(400, 450)); this.setTitle("使用PreparedStatement进行数据库操作"); contentPane.setLayout(null); this.setSize(new Dimension(426, 366)); this.setTitle("使用RowSet操作数据库"); jdbcQueryButton.setLabel("使用JdbcRowSet查询数据"); jdbcQueryButton.setBounds(new Rectangle(10, 288, 193, 30)); jdbcQueryButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jdbcQueryButton_actionPerformed(e); } }); cachedQuerybutton.setLabel("使用CachedRowSet查询数据"); cachedQuerybutton.setBounds(new Rectangle(217, 288, 193, 30)); cachedQuerybutton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cachedQuerybutton_actionPerformed(e); } }); contentPane.add(cachedQuerybutton, null); contentPane.add(jdbcQueryButton, null); } void initTable() { //初始化显示数据集结果的表格 JTable table; vector = new Vector(); tm = new AbstractTableModel() { //实现表格抽象类的接口 public int getColumnCount() { return title.length; } public int getRowCount() { return vector.size(); } public Object getValueAt(int row, int column) { if(!vector.isEmpty()) { return ((Vector)vector.elementAt(row)).elementAt(column); } else { return null; } } public void setValueAt(Object value, int row, int column) { } public String getColumnName(int column) { return title[column]; } public Class getColumnClass(int c) { return getValueAt(0,c).getClass(); } public boolean isCellEditable(int row, int column) { return false; } }; table = new JTable(tm); table.setToolTipText("Display Query Result"); table.setAutoResizeMode(table.AUTO_RESIZE_SUBSEQUENT_COLUMNS); table.setCellSelectionEnabled(false); table.setShowHorizontalLines(true); table.setShowVerticalLines(true); scroll = new JScrollPane(table); scroll.setBounds(10,10,400,200); contentPane.add(scroll,null); } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void jdbcQueryButton_actionPerformed(ActionEvent e) { //使用JdbcRowSet进行数据库查询操作 if(lastQuery) //若上次查询成功,则首先清除上次查询的表格 { scroll.setVisible(false); contentPane.remove(scroll); } initTable(); //初始化新的表格 try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定与数据库连接使用JDBC-ODBC桥驱动程序 jRowSet = new JdbcRowSet(); //创建JdbcRowSet类实例 String url = "jdbc:odbc:student"; //指定数据源名 jRowSet.setUrl(url); //与数据源建立连接 jRowSet.setUsername(""); //指定用户名 jRowSet.setPassword(""); //指定密码 jRowSet.setType(ResultSet.TYPE_SCROLL_INSENSITIVE); //指定表指针滚动方式 jRowSet.setConcurrency(ResultSet.CONCUR_READ_ONLY); //指定数据库查询并发方式 String sql = "select * from studentbase"; //创建取出studentbase表中所有数据的SQL语句 jRowSet.setCommand(sql); //设置JdbcRowSet所要执行的命令 jRowSet.execute(); //执行JdbcRowSet中的命令 vector.removeAllElements(); tm.fireTableStructureChanged(); //刷新表格显示结果集中的数据 while(jRowSet.next()) { Vector rec_vector = new Vector(); rec_vector.addElement(jRowSet.getString("学号")); rec_vector.addElement(jRowSet.getString("姓名")); rec_vector.addElement(jRowSet.getString("年龄")); rec_vector.addElement(jRowSet.getString("性别")); rec_vector.addElement(jRowSet.getString("系名")); vector.addElement(rec_vector); } tm.fireTableStructureChanged(); lastQuery = true; //数据库查询操作成功 } catch(SQLException ex){ //捕捉异常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException(); } } catch(Exception ex ) { ex.printStackTrace(); } } void cachedQuerybutton_actionPerformed(ActionEvent e) { //使用CachedRowSet查询数据库 if(lastQuery) //若上次查询成功,则首先清除上次查询的表格 { scroll.setVisible(false); contentPane.remove(scroll); } initTable(); //初始化新的表格 try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定与数据库连接使用JDBC-ODBC桥驱动程序 String url = "jdbc:odbc:student"; //指定数据源名 String userName = ""; //指定用户名 String password = ""; //指定密码 Connection connection = DriverManager.getConnection(url, userName, password); //与数据源建立连接 cRowSet = new CachedRowSet(); //创建CachedRowSet类实例 cRowSet.setType(ResultSet.TYPE_SCROLL_INSENSITIVE); //指定表指针滚动方式 cRowSet.setConcurrency(ResultSet.CONCUR_READ_ONLY); //指定数据库查询并发方式 String sql = "select * from studentbase"; //创建取出studentbase表中所有数据的SQL语句 cRowSet.setCommand(sql); //设置CachedRowSet所要执行的命令 cRowSet.execute(connection); //执行CachedRowSet中的命令,将数据集存入CachedRowSet实例中 connection.close(); //关闭与数据源的连接 vector.removeAllElements(); tm.fireTableStructureChanged(); //刷新表格显示结果集中的数据 while(cRowSet.next()) { Vector rec_vector = new Vector(); rec_vector.addElement(cRowSet.getString("学号")); rec_vector.addElement(cRowSet.getString("姓名")); rec_vector.addElement(cRowSet.getString("年龄")); rec_vector.addElement(cRowSet.getString("性别")); rec_vector.addElement(cRowSet.getString("系名")); vector.addElement(rec_vector); } tm.fireTableStructureChanged(); lastQuery = true; //数据库查询操作成功 } catch(SQLException ex){ //捕捉异常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException(); } } catch(Exception ex ) { ex.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -