⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addstudent.java

📁 180个针对Java初学者的简单实例,包含了180個適合與初學者的源碼實例
💻 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 + -