?? addstudent.java
字號:
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class AddStudent extends JFrame {
JTextArea taInfo=new JTextArea();
JScrollPane panel=new JScrollPane();
public AddStudent() {
super("添加學生信息");
setSize(350,260);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
panel.getViewport().add(taInfo);
getContentPane().add(panel);
}
public void MidifyStudentTb() 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_UPDATABLE);
SQL = "SELECT 學號,姓名,年齡,系別 FROM 學生信息表";
ResultSet rs=stmt.executeQuery(SQL);
taInfo.setText("更新前的學生信息表\n");
while (rs.next()) {
//將結果集中的數據添加到文本框中
taInfo.append(rs.getString("學號") + "\t");
taInfo.append(rs.getString("姓名") + "\t");
taInfo.append(rs.getString("年齡") + "\t");
taInfo.append(rs.getString("系別") + "\n");
}
taInfo.append("更新后的學生信息表\n");
//刪除第10行記錄
rs.absolute(10);
rs.deleteRow();
//添加一個新的記錄
rs.moveToInsertRow();
rs.updateString("學號","2006071209");
rs.updateString("姓名","李煜");
rs.updateInt("年齡",20);
rs.updateString("系別","計算機");
rs.insertRow();
rs.close();
rs=stmt.executeQuery(SQL);
rs.beforeFirst();
while(rs.next()){
taInfo.append(rs.getString("學號") + "\t");
taInfo.append(rs.getString("姓名") + "\t");
taInfo.append(rs.getString("年齡") + "\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) throws SQLException {
AddStudent frame = new AddStudent();
frame.show();
frame.MidifyStudentTb();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -