📄 statementopdbframe.java
字号:
package statementopdb;import com.borland.jbcl.layout.*;import java.awt.*;import java.awt.event.*;import java.lang.*;import java.lang.Object.*;import java.sql.*;import java.util.*;import javax.swing.*;import javax.swing.table.*;/** * Title: java.sql.Statement操作数据库程序 * Description: 教学示范 * Copyright: Copyright (c) 2003 * Company: 北京师范大学计算机系 * @author 孙一林 * @version 1.0 */public class StatementOpDbFrame extends JFrame { private JPanel contentPane; private XYLayout xYLayout1 = new XYLayout(); // 构造XYLayout布局管理器 private Button displaydataButton = new Button(); // 创建按钮组件 Vector vector; // 声明一个向量对象 String title[] = {"课程代号","课程名","学分","学时","选修课程代号"};// 二维表列名 Connection connection = null; // 声明Connection接口对象connection ResultSet rs = null; // 定义数据库查询的结果集 Statement statement = null; // 定义查询数据库的Statement对象 AbstractTableModel tm; // 声明一个AbstractTableModel类对象tm public StatementOpDbFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); displaydataButton.setLabel("显示数据库记录"); // 初始化组件 displaydataButton.addActionListener(new java.awt.event.ActionListener() { // 设置displaydataButton按钮监听器 public void actionPerformed(ActionEvent e) { displaydataButton_actionPerformed(e); } }); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(443, 422)); this.setTitle("java.sql.Statement操作数据库"); contentPane.add(displaydataButton, new XYConstraints(162, 320, 118, 33)); createtable(); // 在初始化函数中调用createtable()函数显示表格 } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void createtable() { // 定义createtable()函数 JTable table; // 声明一个JTable类对象table JScrollPane scroll; // 声明一个滚动杠对象scroll vector = new Vector(); // 创建向量对象 tm = new AbstractTableModel() { // 创建AbstractTableModel类对象tm 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_OFF); // 设置表格调整尺寸模式 table.setCellSelectionEnabled(false); // 设置单元格选择方式 table.setShowHorizontalLines(true); // 设置是否显示单元格之间的分割线 table.setShowVerticalLines(true); scroll = new JScrollPane(table); // 给表格加上滚动杠 scroll.setBounds(6,20,540,250); contentPane.add(scroll,new XYConstraints(16, 20, 395, 200)); } void displaydataButton_actionPerformed(ActionEvent e) { // 处理displaydataButton的ActionEvent try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 实例化JDBC-ODBC桥的驱动 String url = "jdbc:odbc:TestDbStu"; // 设置连接字符串 connection = DriverManager.getConnection(url); // 连接数据库 // 创建Statement接口对象 statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql = "select * from studentclass"; // 执行statement对象的executeQuery方法,查询studentclass表,将查询结果放入rs中 rs = statement.executeQuery(sql); vector.removeAllElements(); // 初始化向量对象 tm.fireTableStructureChanged(); // 更新表格内容 while(rs.next()) { Vector rec_vector = new Vector(); // 从结果集中取数据放入向量rec_vector中 rec_vector.addElement(rs.getString("课程代号")); rec_vector.addElement(rs.getString("课程名")); rec_vector.addElement(String.valueOf(rs.getInt("学分"))); rec_vector.addElement(String.valueOf(rs.getInt("学时"))); rec_vector.addElement(rs.getString("选修课程代号")); vector.addElement(rec_vector); // 向量rec_vector加入向量vector中 } tm.fireTableStructureChanged(); // 更新表格,显示向量vector的内容 rs.close(); // 关闭结果集 } 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(); } finally { try { if(statement != null) { statement.close(); // 关闭Statement接口实例 } if(connection != null) { connection.close(); // 关闭Connection接口实例 } } catch (SQLException ex) { System.out.println("\nERROR:----- SQLException -----\n"); System.out.println("Message: " + ex.getMessage( )); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -