?? scrolldisplay.java
字號:
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
public class ScrollDisplay extends JFrame
implements ActionListener{
JLabel lbRow=new JLabel("行數");
JTextField tfRow = new JTextField();
JButton btnQuery=new JButton("查詢");
JPanel panel=new JPanel();
JTextArea taInfo=new JTextArea();
public ScrollDisplay() {
super("滾動顯示學生信息");
setSize(320,260);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
panel.setLayout(new BorderLayout());
panel.add(lbRow,BorderLayout.WEST);
panel.add(tfRow,BorderLayout.CENTER);
panel.add(btnQuery,BorderLayout.EAST);
btnQuery.addActionListener(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel,BorderLayout.NORTH);
getContentPane().add(taInfo,BorderLayout.CENTER);
}
public void getRecord(int Row) throws SQLException{
String URL,SQL;
Connection con=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex){
taInfo.setText(ex.getMessage());
System.exit(-1);
}
try{
URL = "jdbc:odbc:學生信息管理";
con = DriverManager.getConnection(URL);
//創建一個可滾動但不可更新的結果集
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
SQL = "SELECT 學號,姓名,年齡,系別 FROM 學生信息表";
ResultSet rs=stmt.executeQuery(SQL);
rs.absolute(Row);//將游標移到指定的行
taInfo.setText("");
taInfo.setText(rs.getString("學號") + "\t");
taInfo.append(rs.getString("姓名") + "\t");
taInfo.append(String.valueOf(rs.getInt("年齡")) + "\t");
taInfo.append(rs.getString("系別") + "\n");
rs.close();
stmt.close();
}
catch(SQLException ex){
taInfo.setText(ex.getMessage());
}
finally{
con.close();
}
}
public static void main(String[] args) {
ScrollDisplay frame = new ScrollDisplay();
frame.show();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnQuery){
String strRow=tfRow.getText();//獲取用戶輸入的行號
int intRow=Integer.parseInt((strRow));
try {
getRecord(intRow);
}
catch (SQLException ex) {
taInfo.setText(ex.getMessage());
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -