?? dbhandler.java~39~
字號:
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,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 + -