将连接数据库、关闭数据库、增删改查数据等对数据库的操作封装成操作数据库的一个类,方便进行数据库的操作。
连接: Java使用JDBC连接SQLserver数据库(一)
代码如下:
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;
}
}
//构造方法
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;
}
}
该方法加载数据库驱动;根据之前类中的“数据库地址”、“数据库登录用户名”、“数据库登录密码”,初始化Connection对象;使用Connection的对象conn实例化Statement接口。
/**
* 关闭数据库连接
* @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;
}
}