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

📄 dbhandler.java~38~

📁 简单的硬盘配置界面
💻 JAVA~38~
字号:
package informationsystem.database;

/**
 * 这个类主要是负责数据库的操作,例如添加,修改,删除等;
 */

import java.sql.*;
import java.sql.ResultSet;
import java.util.*;
import informationsystem.database.DBConnection;

public class DBHandler {
  private Connection conn;

  // Mysql数据库的用户名&密码;
  final String db_user = "sa";//"";
  final String db_password = "7111";//"";

  public DBHandler() {
    try {
      this.conn = new DBConnection(db_user, db_password).getConnection();
    }
    catch (SQLException ex) {
      ex.printStackTrace();
    }
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * 判断学号是否存在,如果存在则不允许添加,否则就将其加入数据库;
   * @param number String
   * @return boolean
   */
  public boolean validate(String number) {
    try {
      String cmd = "select * from HardDisk where Price='" +     ///******************************
          number + "'";
      Statement stat = this.conn.createStatement();
      ResultSet rs = stat.executeQuery(cmd);
      int i = 0;
      while (rs.next()) {
        i++;
      }

      if (i > 0) {
        return true;
      }
      else {
        return false;
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }
  }

  /**
   * 添加学生
   * @param studentNo String
   * @param name String
   * @param department String
   * @param theclass String
   * @return boolean
   */
  public boolean addStudent(String Price,String Factory, String ApplyType,
                            String InterfaceType) {
    try {
      String cmd =
          "insert into HardDisk(Price,Capacity,Factory,InterfaceType,ApplyType) values('" +
          Price + "',"+Factory + "','" +InterfaceType+ "','" +ApplyType  +
          "')";

      Statement stat = this.conn.createStatement();
      stat.executeUpdate(cmd);

      return true;
    }
    catch (Exception ex) {
      ex.printStackTrace();

      return false;
    }
  }

  /**
   * 修改学生的基本信息;
   * @param studentNo String
   * @param name String
   * @param department String
   * @param theclass String
   * @return boolean
   */
  public boolean updateStudent(String Price, String Capacity, String Factory,
                               String InterfaceType) {
    try {
      String cmd =
          "update HardDisk set InterfaceType='" +
          InterfaceType + "' , Capacity='" + Capacity + "' , Factory='" + Factory +
          "' where Price='" + Price + "'";

      Statement stat = this.conn.createStatement();
      stat.executeUpdate(cmd);

      return true;
    }
    catch (Exception ex) {
      ex.printStackTrace();

      return false;
    }

  }

  /**
   * 删除某个学生信息
   * @param number String
   * @return boolean
   */
  public boolean deleteStudent(String number) {
    try {
      String cmd = "delete from HardDisk where Price='" + number + "'";  ///*******

      Statement stat = this.conn.createStatement();
      stat.executeUpdate(cmd);

      return true;
    }
    catch (Exception ex) {
      ex.printStackTrace();

      return false;
    }

  }

  /**
   * 返回某个学生的信息;
   * @param number String
   * @return ResultSet
   */

  public ResultSet getStudent(String number) {
    try {
      String cmd =
          "select Price,Capacity,Factory,InterfaceType,ApplyType,DetailContent from HardDisk where Price= '" +
          number + "'";

      Statement stat = this.conn.createStatement();
      ResultSet rs = stat.executeQuery(cmd);

      return rs;
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

    return null;
  }

  private void jbInit() throws Exception {
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -