db连接驱动

1.oracle

驱动jar包--》ojdbc6.jar

驱动类--》oracle.jdbc.driver.OracleDriver

驱动连接--》

第一种:jdbc:oracle:thin:@//<host>:<port>/<service_name>

第二种:jdbc:oracle:thin:@<host>:<port>:<SID>

第三种:jdbc:oracle:thin:@<TNSName>

    /**
     * DB连接
     * @param args
     * @throws SQLException 
     */
    public static void main(String[] args) throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.105.1:1521:orcl", "zhaoqiang", "zhaoqiang1980");
        Statement statement = connection.createStatement();
        statement.executeQuery("select user_name userName from users where user_name like '%张%'");
        ResultSet resultSet = statement.getResultSet();
        while(resultSet.next()) {
            System.out.println("userName: " +  resultSet.getString("userName"));
        }
        resultSet.close();
        connection.close();
    }

2.mysql

驱动jar包--》mysql-connector-java-5.1.21.jar

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

驱动类--》com.mysql.jdbc.Driver  @deprecated    com.mysql.cj.jdbc.Driver

驱动连接--》jdbc:mysql://ip:端口/数据库名

在mysql的connection 6版本以上,一些参数需要主动传递,因此在参数上配置以下参数,要不然会出现时区差。

serverTimezone=GMT%2B8    或者   serverTimezone=Asia/Shanghai
jdbc:mysql://192.168.68.138:3306/user?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
    /**
     * DB连接
     * @param args
     * @throws SQLException 
     * @throws ClassNotFoundException 
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        // DriverManager.registerDriver(new Driver());
//        Class.forName("com.mysql.jdbc.Driver");
//       Class.forName("com.mysql.cj.jdbc.Driver").newInstance();   //高版本不需要建立驱动,会自己加载驱动
        Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.204.21:3306/order_test?user=zhaoqiang&password=zhaoqiang1980");
//       Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.204.21:3306/order_test?userSSL=false", "zhaoqiang", "zhaoqiang1980");  //这种也可以
        Statement statement = connection.createStatement();
        statement.executeQuery("select user_name userName from users where user_name like '%张%'");
        ResultSet resultSet = statement.getResultSet();
        while(resultSet.next()) {
            System.out.println("userName: " +  resultSet.getString("userName"));
        }
        resultSet.close();
        connection.close();
    }

1. 异常:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone

mysql的驱动从6.+版本开始有配置时区的要求,在url中串联serverTimezone=GMT/UTC 标准时区,GMT%2B8   为北京的东八区。

还可以通过修改mysql的server的时区,这样就不用每次连接需要添加配置。查看到的系统时间可能是System。

>show variables like '%time_zone%';

>set global time_zone='+8:00';

2.警告:WARN: Establishing SSL connection without server's identity verification is not recommended

在url中串联userSSL=false

3.Communications link failure

 如果没有连接时间,则连接没有打通,查看mysql安装时,对于链接JDBC安装支持是否存在。

可通过点击 Installer -> 查看是否有  connection/J.

 

转载于:https://www.cnblogs.com/DennyZhao/p/9364101.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值