?? querystudent.java
字號:
import javax.swing.*;
import java.sql.*;
public class QueryStudent extends JFrame {
//添加方本框組件用于顯示結果集
JTextArea taInfo=new JTextArea();
JScrollPane panel=new JScrollPane();
public QueryStudent() {
super("學生信息");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
setSize(400,200);
panel.getViewport().add(taInfo);
this.getContentPane().add(panel);
}
public void getRecord() throws SQLException{
try {
//加載驅動程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex){
taInfo.setText(ex.getMessage());
System.exit(-1);
}
try{
//試圖通過ODBC建立一個與學生管理數據庫的連接
String URL = "jdbc:odbc:學生信息管理";
Connection con = DriverManager.getConnection(URL);
Statement stmt = con.createStatement();
//執行查詢語句
String SQL = "SELECT 學號,姓名,年齡,系別 FROM 學生信息表";
ResultSet rs = stmt.executeQuery(SQL);
taInfo.setText("");
//獲取查詢結果集
while (rs.next()) {
//將結果集中的數據添加到文本框中
taInfo.append(rs.getString("學號") + "\t");
taInfo.append(rs.getString("姓名") + "\t");
taInfo.append(rs.getInt("年齡") + "\t");
taInfo.append(rs.getString("系別") + "\n");
}
rs.close();
stmt.close();
con.close();
}
catch(SQLException ex){
taInfo.setText(ex.getMessage());
}
}
public static void main(String[] args) throws SQLException {
QueryStudent frame = new QueryStudent();
frame.getRecord();
frame.show();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -