Gbase8s如何配置Druid连接池(java.sql.SQLException: unknown jdbc driver : jdbc:gbasedbt-sql)

今天测试使用Druid连接池来连接Gbase8s

遇到了以下错误:

三月 02, 2024 8:43:31 下午 com.alibaba.druid.pool.DruidDataSource error
严重: {dataSource-1} init error
java.sql.SQLException: unknown jdbc driver : jdbc:gbasedbt-sql://192.168.1.100:9088/mydb:GBASEDBTSERVER=gbase01
    at com.alibaba.druid.util.JdbcUtils.getDriverClassName(JdbcUtils.java:533)
获得连接出异常!!!
    at com.alibaba.druid.pool.DruidDataSource.resolveDriver(DruidDataSource.java:1204)
    at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:888)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1403)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1399)
    at com.panda.com.gbasedbt.orm.DBUtil.getConnection(DBUtil.java:49)
    at com.panda.com.gbasedbt.orm.UserDao.updateById2(UserDao.java:187)
    at com.panda.com.gbasedbt.orm.TestUserDao.main(TestUserDao.java:51)
 

一、演示失败案例(将forname,url,username,password放在jdbc.properties文件里)

1.加载Gbase8s驱动,加载Druid.1.2.9.jar驱动

Gbase8s如何配置Druid连接池(java.sql.SQLException: unknown jdbc driver : jdbc:gbasedbt-sql)_第1张图片

2.创建jdbc.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:gbasedbt-sql://192.168.1.100:9088/mydb:GBASEDBTSERVER=gbase01
username=gbasedbt
password=123456
# ----- 加入druid的一些连接配置
#
initialSize=10
#最大连接数量
maxActive=50
#
minIdle=5
#
maxWait=5000

3.创建DButil

package com.panda.com.gbasedbt.orm;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @desc
 */
public class DBUtil {

    // 创建Properties类对象,专用于操作properties文件
    private static final Properties properties = new Properties( );
    // 声明Druid连接池的连接池对象
    // 数据连接,一般称作数据源 dataSource
    private static DruidDataSource dataSource;


    static {

        try {
            InputStream inputStream = DBUtil.class.getResourceAsStream("/jdbc.properties");
            properties.load(inputStream);
            // 不需要由我们加载驱动
            // 需要给dataSource赋值
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);

        } catch (Exception e) {
            System.out.println("加载驱动异常!!");
            e.printStackTrace( );
        }
    }

    public static Connection getConnection() {
        Connection conn = null;
        try {
            // 不需要我们获得连接
            // 而是通过Druid获得
            conn = dataSource.getConnection( );
        } catch (Exception e) {
            System.out.println("获得连接出异常!!!");
            e.printStackTrace( );
        }
        return conn;
    }


    /**
     * 关闭所有流
     */
    public static void closeAll(Connection conn, Statement s) {
        if (conn != null) {
            try {
                conn.close( );
            } catch (SQLException throwables) {
                throwables.printStackTrace( );
            }
        }

        if (s != null) {
            try {
                s.close( );
            } catch (SQLException throwables) {
                throwables.printStackTrace( );
            }
        }
    }

    public static void closeAll(Connection conn, Statement s, ResultSet rs) {
        if (conn != null) {
            try {
                conn.close( );
            } catch (SQLException throwables) {
                throwables.printStackTrace( );
            }
        }

        if (s != null) {
            try {
                s.close( );
            } catch (SQLException throwables) {
                throwables.printStackTrace( );
            }
        }

        if (rs != null) {
            try {
                rs.close( );
            } catch (SQLException throwables) {
                throwables.printStackTrace( );
            }
        }
    }


    /**
     * 封装查询方法,返回一个对象
     * 参数1 执行查询的SQL,预处理的,条件用?占位
     * select * from tb_user where id = ? and username = ? and password = ?
     * 参数2 结果要封装的类
     * 参数3 给?赋值,不定长参数,是数组
     * 1,admin,123456
     */
    public static  T selectOne(String sql, Class t, Object... args) {
        Connection conn = getConnection( );
        PreparedStatement ps = null;
        ResultSet rs = null;
        T target = null;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; args != null && i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery( );
            /**
             * 创建对象
             * 从数据库取出数据,并设置对象属性
             */
            while (rs.next( )) {
                target = t.newInstance( );
                Field[] declaredFields = t.getDeclaredFields( );
                for (int i = 0; i < declaredFields.length; i++) {
                    Field field = declaredFields[i];
                    Object value = rs.getObject(field.getName( ));

                    // 破解私有
                    field.setAccessible(true);

                    // 给对象的该字段赋值
                    field.set(target, value);
                }

            }
        } catch (Exception e) {
            e.printStackTrace( );
        } finally {
            closeAll(conn, ps, rs);
        }
        return target;
    }

    public static  List selectAll(String sql, Class t, Object... args) {
        Connection conn = getConnection( );
        PreparedStatement ps = null;
        ResultSet rs = null;
        T target = null;
        ArrayList list = new ArrayList<>( );
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; args != null && i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery( );
            /**
             * 创建对象
             * 从数据库取出数据,并设置对象属性
             */
            while (rs.next( )) {
                target = t.newInstance( );
                Field[] declaredFields = t.getDeclaredFields( );
                for (int i = 0; i < declaredFields.length; i++) {
                    Field field = declaredFields[i];
                    Object value = rs.getObject(field.getName( ));

                    // 破解私有
                    field.setAccessible(true);

                    // 给对象的该字段赋值
                    field.set(target, value);
                }
                list.add(target);

            }
        } catch (Exception e) {
            e.printStackTrace( );
        } finally {
            closeAll(conn, ps, rs);
        }
        return list;
    }

    /**
     * 增删改方法一样
     */
    public static boolean update(String sql, Object... args) {
        Connection conn = getConnection( );
        PreparedStatement ps = null;
        int num = 0;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; args != null && i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            num = ps.executeUpdate( );
        } catch (Exception e) {
            e.printStackTrace( );
        } finally {
            closeAll(conn, ps);
        }
        return num > 0 ? true : false;
    }
}

4.测试报错:java.sql.SQLException: unknown jdbc driver

三月 02, 2024 8:43:31 下午 com.alibaba.druid.pool.DruidDataSource error
严重: {dataSource-1} init error
java.sql.SQLException: unknown jdbc driver : jdbc:gbasedbt-sql://192.168.1.100:9088/mydb:GBASEDBTSERVER=gbase01
    at com.alibaba.druid.util.JdbcUtils.getDriverClassName(JdbcUtils.java:533)
获得连接出异常!!!
    at com.alibaba.druid.pool.DruidDataSource.resolveDriver(DruidDataSource.java:1204)
    at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:888)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1403)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1399)
    at com.panda.com.gbasedbt.orm.DBUtil.getConnection(DBUtil.java:49)
    at com.panda.com.gbasedbt.orm.UserDao.updateById2(UserDao.java:187)
    at com.panda.com.gbasedbt.orm.TestUserDao.main(TestUserDao.java:51)

二,演示成功案例

参考:Linux下Druid连接池GBase 8s数据库配置_druid 配置连接kingbase8-CSDN博客

1.创建Druid.properties文件(不包含url,username,password的配置信息)

maxWait=12000
testWhileIdle=true
testOnBorrow=true
timeBetweenEvictionRunsMillis=60000
validationQueryTimeout=30000
defaultAutoCommit=true
removeAbandoned = true
removeAbandonedTimeout = 180
poolPreparedStatements = true
maxOpenPreparedStatements = 100

2.将url,usrname,password直接写在DBUtil文件,执行成功

package com.panda.com.gbasedbt.orm;

import java.io.File;
        import java.io.FileInputStream;
        import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
        import java.sql.PreparedStatement;
        import java.sql.ResultSet;
        import java.sql.ResultSetMetaData;
        import java.sql.SQLException;
        import java.sql.Statement;
        import java.util.Properties;

        import com.alibaba.druid.pool.DruidDataSource;
        import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.gbasedbt.DButil.DBUtil;

import javax.sql.DataSource;

public class druid_test {

    //private static String DRUIDPool = "Druid.properties";

    public static void main(String[] args) throws IOException, SQLException{
        DruidDataSource dds = initDruidDataSource();
        PreparedStatement pStat = null;
        ResultSet rs = null;
        Connection conn = dds.getConnection();

        conn.setAutoCommit(false);

        pStat = conn.prepareStatement("insert into tb_user(id) values(?)");
        pStat.setInt(1, 1);
        pStat.executeUpdate();
        conn.commit();

        Statement stat = conn.createStatement();
        rs = stat.executeQuery("select count(*) from tb_user");
        while (rs.next()) {
            System.out.println("total insert " + rs.getInt(1) + " rows");
        }

        rs = stat.executeQuery("select * from t1 where 1=0");
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        for (int i = 0; i < columnCount; i++) {
            System.out.println(rsmd.getColumnName(i+1) + "\t");
        }
        closeAll(null, pStat, conn);
    }

    public static  DruidDataSource initDruidDataSource() {
        Properties p = new Properties();
        //FileInputStream fi = null;
        InputStream fi=null;
        DruidDataSource source = null;
        try{
            fi = DBUtil.class.getResourceAsStream("/Druid.properties");
          //  fi = new FileInputStream("DRUIDPool");
            p.load(fi);
            String DRIVER = "com.gbasedbt.jdbc.Driver";
            String URL = "jdbc:gbasedbt-sqli://192.168.1.100:9088/mydb:GBASEDBTSERVER=gbase01";
            String USERNAME = "gbasedbt";
            String PASSWORD = "123456";
            source =  (DruidDataSource)DruidDataSourceFactory.createDataSource(p);
            System.out.println(source.getMaxOpenPreparedStatements());
            System.out.println(source.isPoolPreparedStatements());
            source.setUrl(URL);
            source.setUsername(USERNAME);
            source.setPassword(PASSWORD);
            source.setDriverClassName(DRIVER);
            source.setInitialSize(5);
            source.setMaxActive(10);
            source.setMinIdle(5);
            source.setValidationQuery("SELECT 1 from dual");


            p.clear();
            p = null;
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                if (fi!=null){
                    fi.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return source;
    }

    public static void closeAll(ResultSet rs, PreparedStatement stat, Connection conn) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        if (stat != null)
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }
}

你可能感兴趣的:(数据库)