📄 paraqueryframe.java
字号:
package paraquery;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 ParaQueryFrame extends JFrame { private JPanel contentPane; private XYLayout xYLayout1 = new XYLayout();// 构造XYLayout布局管理器 // 创建显示信息用的组件 private Label label1 = new Label(); private TextField numberField = new TextField(); private Button queryButton = new Button(); Connection connection = null; // 定义与数据库进行连接的Connection对象 ResultSet rSet = null; // 定义数据库查询的结果集 Statement statement = null; // 定义查询数据库的Statement对象 public ParaQueryFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); // 初始化组件 label1.setText("查询学号"); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("使用参数查询数据库"); queryButton.setLabel("查询"); queryButton.addActionListener(new java.awt.event.ActionListener() { // 设置queryButton按钮监听器 public void actionPerformed(ActionEvent e) { queryButton_actionPerformed(e); } }); contentPane.add(label1, new XYConstraints(72, 106, 65, 30)); contentPane.add(numberField, new XYConstraints(154, 109, 90, 29)); contentPane.add(queryButton, new XYConstraints(154, 186, 92, 33)); } 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接口对象 statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql = "select * from studentbase where 学号="+Integer.parseInt(numberField.getText()) ; rSet = statement.executeQuery(sql); // 执行学号为输入学号的查询语句 if (rSet.next()==false) { // 判断数据库中是否有要查询学号的记录,如没有则输出提示框 JOptionPane msg = new JOptionPane(); JOptionPane.showMessageDialog(ParaQueryFrame.this, "数据库中没有您查询的学号", "数据库中没有您查询的学号!", 1); } else { rSet.previous() ; // 使指针返回到ResultSet对象的开始 ResultSetMetaData data = rSet.getMetaData(); // 定义ResultSetMetaData对象data numberField.setText("") ; // 清空学号文本框 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 + -