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.