1 / 7

第二十讲 信息查询 ___ 简单查询应用

第二十讲 信息查询 ___ 简单查询应用. 20.1 简单查询窗体的设计. 本章要 点. 掌握 JAVA 中 查询记录的 方法. 20.1 简单查询窗体的设计. (1) 用 JDBC —— ODBC 连接数据库。 (2) 设计窗体,在窗体中加入控件,显示用户输入界面。 (3) 为控件加入监听者及事件处理代码段。. 查询窗体图示. 查询窗体代码段. public void jButton1_actionPerformed(ActionEvent e) { try {

willis
Download Presentation

第二十讲 信息查询 ___ 简单查询应用

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第二十讲 信息查询___简单查询应用 • 20.1 简单查询窗体的设计

  2. 本章要点 • 掌握JAVA中查询记录的方法

  3. 20.1 简单查询窗体的设计 • (1) 用JDBC——ODBC连接数据库。 • (2) 设计窗体,在窗体中加入控件,显示用户输入界面。 • (3) 为控件加入监听者及事件处理代码段。

  4. 查询窗体图示

  5. 查询窗体代码段 • public void jButton1_actionPerformed(ActionEvent e) { • try { • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); • String url = "jdbc:odbc:stu01"; • Connection conn = DriverManager.getConnection(url); • Statement stat = conn.createStatement(); • String sql = "select * from students where banji='"+bjjComboBox.getSelectedItem().toString()+"';"; • ResultSet rSet = stat.executeQuery(sql); • ResultSetMetaData rsmd = rSet.getMetaData(); //元数据 • int colnum=rsmd.getColumnCount(); //列数 • int rownum=0; //行数 • boolean more = rSet.next(); • while(more){ • rownum++;

  6. 查询窗体代码段(续一) • more=rSet.next(); • } • rSet = stat.executeQuery(sql); • rSet.next(); • Object[][] data=new Object[rownum][colnum]; • Object[] Colname={"学号","姓名","性别","年龄","班级","电话","住址"}; • for(int i=0;i<rownum;i++) • { • data[i][0] =rSet.getString(1); • data[i][1] =rSet.getString(2); • data[i][2] =rSet.getString(3);

  7. 查询窗体代码段(续二) • data[i][3] =rSet.getString(4); • data[i][4] =rSet.getString(5); • data[i][5] =rSet.getString(6); • data[i][6] =rSet.getString(7); • rSet.next(); • } • infojTable = new JTable(data,Colname); • infojTable.setDoubleBuffered(true); • infojTable.setColumnSelectionAllowed(true); • infojScrollPane.getViewport().add(infojTable,null); • jbjg.setText("查询正确!"); • rSet.close(); } • catch(java.lang.Exception exec) • { jbjg.setText("查询错误!"); } }}

More Related