Java使用JDBC连接SQLserver数据库(二)

将连接数据库、关闭数据库、增删改查数据等对数据库的操作封装成操作数据库的一个类,方便进行数据库的操作。

连接: 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;
    }

}

二、类的详细介绍

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;
    }

}
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值