我是靠谱客的博主 善良便当,最近开发中收集的这篇文章主要介绍在Jtable中显示增加删除查询修改的数据库信息,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

/**
  * 连接数据库函数
  */
 public static Connection fetchconnection()
 {
  try{
         Class.forName("oracle.jdbc.driver.OracleDriver");
      String url ="jdbc:oracle:thin:@192.168.119.108:1521:orclcn";
   String user = "zenggan";
   String pwd = "zeng";
      return DriverManager.getConnection(url, user, pwd);
  }
  catch(Exception e)
  {
   e.printStackTrace();
   return null;
  }
 }
 
 private void addsubinfoButtonActionPerformed(ActionEvent evt) throws Exception {
  /**
   * 插入语句
   */
  Statement statement = null;
  Connection connection =null;
  try{
   connection = fetchconnection();
   statement = connection.createStatement();
   
   String sql = "insert into subject values('"+this.sub_idTextField.getText()+"','"+this.sub_nameTextField.getText()+"','"+this.creditTextField.getText()+"')";
      statement.executeUpdate(sql);
      JOptionPane.showMessageDialog(this, "增加成功!!");
      this.sub_idTextField.setText("");
   this.sub_nameTextField.setText("");
   this.creditTextField.setText("");
     
  }
  catch(Exception e)
  {
      e.printStackTrace();
  }
  finally{
   statement.close();
   connection.close();
  }
 }
 
 private void modifysub_infoButtonActionPerformed(ActionEvent evt) throws SQLException {
  Statement statement = null;
  Connection connection =null;
  ResultSet resultSet = null;
  
  try{
   connection = fetchconnection();
   statement = connection.createStatement();
   
   String sql = "update subject set sub_name='"+this.editsub_nameTextField.getText()+"' where sub_id='"+this.editsub_idTextField.getText()+"'";
   resultSet = statement.executeQuery(sql);
   sql = "update subject set credit='"+this.editcreditTextField.getText()+"' where sub_id='"+this.editsub_idTextField.getText()+"'";
   resultSet = statement.executeQuery(sql);
   JOptionPane.showMessageDialog(this,"修改成功!!");
   this.editsub_idTextField.setText("");
   this.editsub_nameTextField.setText("");
   this.editcreditTextField.setText("");
  }
  catch(Exception e)
  {
      e.printStackTrace();
  }
  finally{
   resultSet.close();
   statement.close();
   connection.close();
  }
 }
 
 public static String[] data = new String[3];//定义一数组存储字段
 private void subjectinfoButtonActionPerformed(ActionEvent evt) throws SQLException {
  Statement statement = null;
  Connection connection =null;
  ResultSet resultSet = null;
  
  ((DefaultTableModel) this.subjectinfoTable.getModel()).getDataVector().clear();
  this.subjectinfoTable.updateUI();
  
  try{
   connection = fetchconnection();
   statement = connection.createStatement();
   
   String sql = "select * from subject";//查询subject表的所有信息
   resultSet = statement.executeQuery(sql);
     while(resultSet.next())  {
      data[0] = resultSet.getString("sub_id");   //将类型赋值给定义的数组
      data[1] = resultSet.getString("sub_name");
      data[2] = resultSet.getString("credit");
      ((DefaultTableModel) this.subjectinfoTable.getModel()).addRow(data);//table中显示
     }
  }
  catch(Exception e)
  {
      e.printStackTrace();
  }
  finally{
   resultSet.close();
   statement.close();
   connection.close();
  }
 }
 
 private void deletesub_infoButtonActionPerformed(ActionEvent evt) throws SQLException {
  Statement statement = null;
  Connection connection =null;
  ResultSet resultSet = null;
  
  try{
   connection = fetchconnection();
   statement = connection.createStatement();
   
   String sql = "delete subject where sub_id='"+this.editsub_idTextField.getText()+"'";
   resultSet = statement.executeQuery(sql);
   JOptionPane.showMessageDialog(this,"删除成功!!");
   this.editsub_idTextField.setText("");
   this.editsub_nameTextField.setText("");
   this.editcreditTextField.setText("");
  }
  catch(Exception e)
  {
      e.printStackTrace();
  }
  finally{
   resultSet.close();
   statement.close();
   connection.close();
  }
 }
 
 private void addtestButtonActionPerformed(ActionEvent evt) throws SQLException {
  Statement statement = null;
  Connection connection =null;
  try{
   connection = fetchconnection();
   statement = connection.createStatement();
   
   String sql = "insert into test values('"+this.test_idTextField.getText()+"','"+
                                            this.class_idTextField.getText()+"','"+
                                            this.testsub_idTextField.getText()+"','"+
                                            this.teacher_idTextField.getText()+"','"+
                                            this.test_timeTextField.getText()+"','"+
                                            this.test_addressTextArea.getText()+"')";
      statement.executeUpdate(sql);
      JOptionPane.showMessageDialog(this, "增加成功!!");
      this.test_idTextField.setText("");
            this.class_idTextField.setText("");
            this.testsub_idTextField.setText("");
            this.teacher_idTextField.setText("");
            this.test_timeTextField.setText("");
            this.test_addressTextArea.setText("");
     
  }
  catch(Exception e)
  {
      e.printStackTrace();
  }
  finally{
   statement.close();
   connection.close();
  }
 }
 
 public static String[] dataone = new String[6];
 
 private void testinfoButtonActionPerformed(ActionEvent evt) throws SQLException {
  Statement statement = null;
  Connection connection =null;
  ResultSet resultSet = null;
  
  ((DefaultTableModel) this.testinfoTable.getModel()).getDataVector().clear();
  this.testinfoTable.updateUI();
  try{
   connection = fetchconnection();
   statement = connection.createStatement();
   String sql = "select * from test";//查询test表的所有信息
   resultSet = statement.executeQuery(sql);
     while(resultSet.next())  {
      dataone[0] = resultSet.getString("test_id");   //将类型赋值给定义的数组
      dataone[1] = resultSet.getString("class_id");
      dataone[2] = resultSet.getString("sub_id");
      dataone[3] = resultSet.getString("teacher_id");
      dataone[4] = resultSet.getString("test_time");
      dataone[5] = resultSet.getString("test_adress");
      ((DefaultTableModel) this.testinfoTable.getModel()).addRow(dataone);//table中显示
     }
  }
  catch(Exception e)
  {
      e.printStackTrace();
  }
  finally{
   resultSet.close();
   statement.close();
   connection.close();
  }
 }
 
 private void edittestmodifyButtonActionPerformed(ActionEvent evt) throws SQLException {
  Statement statement = null;
  Connection connection =null;
  ResultSet resultSet = null;
  
  try{
   connection = fetchconnection();
   statement = connection.createStatement();
   
   String sql = "update test set class_id='"+this.editclass_idTextField.getText()+"' where test_id='"+this.edittest_idTextField.getText()+"'";
   resultSet = statement.executeQuery(sql);
   sql = "update test set sub_id='"+this.edittestsub_idTextField.getText()+"' where test_id='"+this.edittest_idTextField.getText()+"'";
   resultSet = statement.executeQuery(sql);
   sql = "update test set teacher_id='"+this.edittestteacheridTextField.getText()+"' where test_id='"+this.edittest_idTextField.getText()+"'";
   resultSet = statement.executeQuery(sql);
   sql = "update test set test_time='"+this.edittesttimeTextField.getText()+"' where test_id='"+this.edittest_idTextField.getText()+"'";
   resultSet = statement.executeQuery(sql);
   sql = "update test set test_adress='"+this.edittestaddressTextArea.getText()+"' where test_id='"+this.edittest_idTextField.getText()+"'";
   resultSet = statement.executeQuery(sql);
   JOptionPane.showMessageDialog(this,"修改成功!!");
   this.edittest_idTextField.setText("");
   this.editclass_idTextField.setText("");
   this.edittestsub_idTextField.setText("");
   this.edittestteacheridTextField.setText("");
   this.edittesttimeTextField.setText("");
   this.edittestaddressTextArea.setText("");
  }
  catch(Exception e)
  {
      e.printStackTrace();
  }
  finally{
   resultSet.close();
   statement.close();
   connection.close();
  }
 }
 
 private void edittestdeleteButtonActionPerformed(ActionEvent evt) throws SQLException {
  Statement statement = null;
  Connection connection =null;
  ResultSet resultSet = null;
  
  try{
   connection = fetchconnection();
   statement = connection.createStatement();
   
   String sql = "delete test where test_id='"+this.edittest_idTextField.getText()+"'";
   resultSet = statement.executeQuery(sql);
   JOptionPane.showMessageDialog(this,"删除成功!!");
   this.edittest_idTextField.setText("");
   this.editclass_idTextField.setText("");
   this.edittestsub_idTextField.setText("");
   this.edittestteacheridTextField.setText("");
   this.edittesttimeTextField.setText("");
   this.edittestaddressTextArea.setText("");
  }
  catch(Exception e)
  {
      e.printStackTrace();
  }
  finally{
   resultSet.close();
   statement.close();
   connection.close();
  }
 }
 
 private void tuichuButtonActionPerformed(ActionEvent evt) {
  System.exit(0);
 }

最后

以上就是善良便当为你收集整理的在Jtable中显示增加删除查询修改的数据库信息的全部内容,希望文章能够帮你解决在Jtable中显示增加删除查询修改的数据库信息所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部