JAVA德鲁伊连接MySQL数据库

1,百度中搜索这几个工具类下载复制到ieda的libs目录中
在这里插入图片描述
2,写一个管道类,用于连接MySQL的参数传递

driverClassName=com.mysql.cj.jdbc.Driver
//数据库名
url=jdbc:mysql://localhost:3306/db01?useSSL=true&serverTimezone=GMT&useUnicode=true&characterEncoding=utf8
//如果是配置在本地计算机上的数据库还可以写成
#//url=jdbc:mysql:///db
//用户名
username=root
//密码
password=qwe123
//初始连接数量
initialSize=5
minIdle=5
//最大连接数量
maxActive=10
//最长超时等待
maxWait=3000

在这里插入图片描述

1,jdbcUtilesDruid工具包为了让java连接MySQL

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class jdbcUtilesDruid {
    //创建数据源
    public static DataSource ds;
    static {
        Properties properties = new Properties();
        try {
            //获取管道类
            properties.load(new FileInputStream("src\\druid.properties"));
            //管道连接数据源
            ds= DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //java连接MySQL
    public static Connection getconnection() throws SQLException{
        return ds.getConnection();
    }

    //关闭连接,在数据库连接池方法中,close不是真的断掉连接
    //而是把使用的connection对象放回到连接池中
    public static void close(ResultSet resultSet, Statement statement,Connection connection){
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement!=null){
                statement.close();
            }
            if (connection!=null){
                connection.close();
            }
        } catch (SQLException e){
            throw new RuntimeException();
        }
    }
}

2,BsaicDAO工具包为了执行MySQL的操作(增删改查)
3,再创建一个包去继承工具包中的方法即可实现复用

import com.zouwentao.mhl.utlis.jdbcUtilesDruid;
import com.zouwentao.mhl.utlis.jdbcUtilesDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BsaicDAO<T> {
    private QueryRunner qr= new QueryRunner();
    //修改数据表中列的值
    public int update(String sql,Object... parameters ){
        Connection connection=null;

        try {
            connection = jdbcUtilesDruid.getconnection();
            int update = qr.update(connection, sql, parameters);
            return update;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            jdbcUtilesDruid.close(null,null,connection);
        }
    }

    /**
     *
     * @param sql:sql语句,可以有?
     * @param clazz:传入一个类的class对象 比如Actor.class
     * @param parameters:传入?的具体值,可以是多个
     * @return:根据Acror。class,返回对应的ArrayList集合
     */
    //查询数据库中数据表语句
    public List<T> queryMulti(String sql,Class<T> clazz,Object...parameters){
        Connection connection=null;

        try {
            connection = jdbcUtilesDruid.getconnection();
            return qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);




        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            jdbcUtilesDruid.close(null,null,connection);
        }
    }
    //查询单行结果通用方法
    public T querysingle(String sql,Class<T> clazz,Object...parameters){
        Connection connection=null;

        try {
            connection = jdbcUtilesDruid.getconnection();
            return qr.query(connection,sql,new BeanHandler<T>(clazz),parameters);




        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            jdbcUtilesDruid.close(null,null,connection);
        }
    }
    //查询单行单列方法,即返回单值的方法
    public Object queryScalar(String sql,Object...parameters){

        Connection connection=null;

        try {
            connection = jdbcUtilesDruid.getconnection();
            return qr.query(connection,sql,new ScalarHandler(),parameters);




        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            jdbcUtilesDruid.close(null,null,connection);
        }
    }
}


import com.zouwentao.mhl.domain.employee;

public class EmployeeDAO extends BsaicDAO<employee> {
}

1,以员工查询为例,调用查询语句


```java
import java.util.List;

//该类完成对employee表的各种操作
public class EmployeeService {
    //定义一个EmployeeDAO属性
    private EmployeeDAO employeeDAO=new EmployeeDAO();

    public employee getEmployeeDAO(String empId,String pwd){
        return employeeDAO.querysingle("select * from employee where empId=? and pwd=md5(?)", employee.class//自己定义的员工类,存放员工属性, empId, pwd);

    }
}

总结:到此完成了德鲁伊方法MySQL连接Java的全部过程

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值