📄 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 + -