📄 queryframe.java
字号:
package query;import com.borland.jbcl.layout.*;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;/** * Title: 查询数据库程序 * Description: 教学示范 * Copyright: Copyright (c) 2003 * Company: 北京师范大学计算机系 * @author 孙一林 * @version 1.0 */public class QueryFrame extends JFrame { private JPanel contentPane; private XYLayout xYLayout1 = new XYLayout(); // 构造XYLayout布局管理器 private Button queryButton = new Button(); // 创建按钮组件 Connection connection = null; // 定义与数据库进行连接的Connection对象 ResultSet rSet = null; // 定义数据库查询的结果集 Statement statement = null; // 定义查询数据库的Statement对象 public QueryFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); queryButton.setLabel("查询"); // 初始化组件 queryButton.addActionListener(new java.awt.event.ActionListener() { // 设置queryButton按钮监听器 public void actionPerformed(ActionEvent e) { queryButton_actionPerformed(e); } }); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("查询数据库"); contentPane.add(queryButton, new XYConstraints(154, 171, 92, 34)); } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void queryButton_actionPerformed(ActionEvent e) { // 处理queryButton的ActionEvent try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 实例化JDBC-ODBC桥的驱动 String url = "jdbc:odbc:TestDbStu"; // 设置连接字符串 connection = DriverManager.getConnection(url); // 连接数据库 statement = connection.createStatement(); // 创建Statement接口对象 String sql = "select * from studentbase"; rSet = statement.executeQuery(sql); // 执行查询语句 ResultSetMetaData data = rSet.getMetaData(); // 定义ResultSetMetaData对象data int col = data.getColumnCount(); // 获取列数 for (int i=1; i<=col; i++) { // 打印列名 if (i<col) { System.out.print(data.getColumnName(i) + " "); } else { System.out.println(data.getColumnName(i)); } } while(rSet.next()) { // 打印数据 for (int i=1; i<=col; i++) { if (i<col) { System.out.print(rSet.getString(i) + " "); } else { System.out.println(rSet.getString(i)); } } } rSet.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 + -