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

📄 dbhandler.java

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

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

import java.sql.*;
import java.sql.ResultSet;
import java.util.*;
import informationsystem.database.DBConnection;
//////////////////
import java.awt.*;
import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
////////////////

public class DBHandler {
  private Connection conn;

  // sql数据库的用户名&密码;
  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 harddiskNo String
   * @param name String
   * @param factory String
   * @param theinterfacetype String
   * @return boolean
   */
  public boolean addHardDisk(String Price,String Capacity,String Factory, String ApplyType,//10000000000000
                            String InterfaceType) {
    try {
      String cmd =
          "insert into HardDisk(Price,Capacity,Factory,ApplyType,InterfaceType) values('" +
          Price + "','"+Capacity+"','" +Factory + "','" +ApplyType + "','" +InterfaceType +
          "')";
          //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 updateHardDisk(String Price, String Capacity, String Factory,
                               String InterfaceType,String ApplyType) {
    try {
      String cmd =
          "update HardDisk set InterfaceType='" +
          InterfaceType + "' , Capacity='" + Capacity + "' , Factory='" + Factory +
          "',ApplyType='"+ApplyType+"' 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 deleteHardDisk(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 getHardDisk(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 + -