將連接數據庫、關閉數據庫、增刪改查數據等對數據庫的操作封裝成操作數據庫的一個類,方便進行數據庫的操作。
一、類的源代碼
代碼如下:
package com.operationdb;
import java.sql.*;
/**
* 操作數據庫的類,連接SQLserver數據庫,以及對數據庫的增刪改查操作
* @author HuDongyang
*
*/
public class OperationDB {
//驅動路徑
private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//數據庫地址
private String DBURL = "jdbc:sqlserver://localhost:1434;DataBaseName=";
//數據庫登錄用戶名
private static final String DBUSER = "sa";
//數據庫用戶密碼
private static final String DBPASSWORD = "123456";
//數據庫連接
public Connection conn = null;
//執行SQL語句的接口
public Statement stmt = null;
//要執行的SQL語句
public String SQLStr = null;
//數據容器
public ResultSet rs = null;
//提示信息
public String TempInfo = "";
//構造方法
public OperationDB(String DBName) {
this.DBURL += DBName;
}
/**
* 連接數據庫
* @return 成功返回true,失敗返回false
*/
public boolean linkDB(){
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
stmt = conn.createStatement();
return true;
} catch (Exception e) {
e.printStackTrace();
try {
stmt.close();
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return false;
}
}
/**
* 關閉數據庫連接
* @return 成功返回true,失敗返回false
*/
public boolean closeDB(){
try {
stmt.close();
conn.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 添加數據
* @param TableName 要操作的數據庫表名
* @param InsertValue 要增加的數據
* @param WhereStr 查詢表中是否已存在要新增的信息
* @return 成功返回true,失敗返回false
*/
public boolean insertData(String TableName, String InsertValue, String WhereStr){
this.SQLStr = "insert into " + TableName + " values (" + InsertValue + ")";
try {
if(stmt.executeQuery("select * from " + TableName + " where " + WhereStr).next()){
TempInfo = "該記錄已存在!";
this.closeDB();
return false;
}else{
stmt.executeUpdate(SQLStr);
this.closeDB();
return true;
}
} catch (Exception e) {
e.printStackTrace();
this.closeDB();
return false;
}
}
/**
* 刪除數據
* @param TableName 要操作的數據庫表名
* @param WhereStr 刪除哪一條記錄
* @return 成功返回true,失敗返回false
*/
public boolean deleteData(String TableName, String WhereStr){
this.SQLStr = "delete from " + TableName + " where " + WhereStr;
try {
if(stmt.executeUpdate(SQLStr) != 0){
this.closeDB();
return true;
}else{
this.closeDB();
TempInfo = "數據不存在!";
return false;
}
} catch (SQLException e) {
e.printStackTrace();
this.closeDB();
return false;
}
}
/**
* 修改數據
* @param TableName 要操作的數據庫表名
* @param UpdateValue 要修改的屬性
* @param WhereStr 要修改哪一條記錄
* @return 成功返回true,失敗返回false
*/
public boolean updateData (String TableName, String UpdateValue, String WhereStr){
this.SQLStr = "update " + TableName + " set " + UpdateValue + " where " + WhereStr;
try {
if(stmt.executeUpdate(SQLStr) != 0){
this.closeDB();
return true;
}else{
this.closeDB();
TempInfo = "數據不存在!";
return false;
}
} catch (Exception e) {
e.printStackTrace();
this.closeDB();
return false;
}
}
/**
* 查詢數據,該方法沒有關閉數據庫連接,因爲不能關閉RS結果集
* @param TableName 要操作的數據庫表名
* @param QueryValue 需要查詢的數據
* @param WhereStr 查詢哪一條記錄
* @return 返回一個ResultSet集合
*/
public ResultSet queryData(String TableName, String QueryValue, String WhereStr){
if(WhereStr == ""){
this.SQLStr = "select "+ QueryValue + " from " + TableName;
}else{
this.SQLStr = "select "+ QueryValue + " from " + TableName + " where " + WhereStr;
}
try {
rs = stmt.executeQuery(SQLStr);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
}
二、類的詳細介紹
1.構造方法
//構造方法
public OperationDB(String DBName) {
this.DBURL += DBName;
}
構造方法中,初始化數據庫地址;不提供無參構造方法。
2.連接數據庫方法
/**
* 連接數據庫
* @return 成功返回true,失敗返回false
*/
public boolean linkDB(){
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
stmt = conn.createStatement();
return true;
} catch (Exception e) {
e.printStackTrace();
try {
stmt.close();
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return false;
}
}
該方法加載數據庫驅動;根據之前類中的“數據庫地址”、“數據庫登錄用戶名”、“數據庫登錄密碼”,初始化Connection對象;使用Connection的對象conn實例化Statement接口。
3.關閉數據庫連接方法
/**
* 關閉數據庫連接
* @return 成功返回true,失敗返回false
*/
public boolean closeDB(){
try {
stmt.close();
conn.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
4.添加數據方法
/**
* 添加數據
* @param TableName 要操作的數據庫表名
* @param InsertValue 要增加的數據
* @param WhereStr 查詢表中是否已存在要新增的信息
* @return 成功返回true,失敗返回false
*/
public boolean insertData(String TableName, String InsertValue, String WhereStr){
this.SQLStr = "insert into " + TableName + " values (" + InsertValue + ")";
try {
if(stmt.executeQuery("select * from " + TableName + " where " + WhereStr).next()){
TempInfo = "該記錄已存在!";
this.closeDB();
return false;
}else{
stmt.executeUpdate(SQLStr);
this.closeDB();
return true;
}
} catch (Exception e) {
e.printStackTrace();
this.closeDB();
return false;
}
}
5.刪除數據方法
/**
* 刪除數據
* @param TableName 要操作的數據庫表名
* @param WhereStr 刪除哪一條記錄
* @return 成功返回true,失敗返回false
*/
public boolean deleteData(String TableName, String WhereStr){
this.SQLStr = "delete from " + TableName + " where " + WhereStr;
try {
if(stmt.executeUpdate(SQLStr) != 0){
this.closeDB();
return true;
}else{
this.closeDB();
TempInfo = "數據不存在!";
return false;
}
} catch (SQLException e) {
e.printStackTrace();
this.closeDB();
return false;
}
}
6.修改數據方法
/**
* 修改數據
* @param TableName 要操作的數據庫表名
* @param UpdateValue 要修改的屬性
* @param WhereStr 要修改哪一條記錄
* @return 成功返回true,失敗返回false
*/
public boolean updateData (String TableName, String UpdateValue, String WhereStr){
this.SQLStr = "update " + TableName + " set " + UpdateValue + " where " + WhereStr;
try {
if(stmt.executeUpdate(SQLStr) != 0){
this.closeDB();
return true;
}else{
this.closeDB();
TempInfo = "數據不存在!";
return false;
}
} catch (Exception e) {
e.printStackTrace();
this.closeDB();
return false;
}
}
7.查詢數據方法
/**
* 查詢數據,該方法沒有關閉數據庫連接,因爲RS結果集關閉後無法使用
* @param TableName 要操作的數據庫表名
* @param QueryValue 需要查詢的數據
* @param WhereStr 查詢哪一條記錄
* @return 返回一個ResultSet集合
*/
public ResultSet queryData(String TableName, String QueryValue, String WhereStr){
if(WhereStr == ""){
this.SQLStr = "select "+ QueryValue + " from " + TableName;
}else{
this.SQLStr = "select "+ QueryValue + " from " + TableName + " where " + WhereStr;
}
try {
rs = stmt.executeQuery(SQLStr);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
}