一、下载安装mysql
下载地址: https://dev.mysql测试数据/downloads/connector/j/
可以选择对应的版本下载,最新的是8.0版本,本章使用的是5.0版本
二、引入驱动包
驱动包下载:mysql-connector- java -5.1.39-bin.jar
MySQL 8.0 以上版本的 数据库 连接 有所不同:
1、MySQL 8.0 以上版本驱动包版本 mysql-connector-java-8.0.16.jar。 2、com.mysql.jdbc.Driver 更换为 com.mysql.cj.jdbc.Driver。 MySQL 8.0 以上版本不需要建立 SSL 连接的,需要显示关闭。 allowPublicKeyRetrieval=true 允许客户端从服务器获取公钥。 最后还需要设置 CST。加载驱动与连接数据库方式如下:
1 2 |
Class.forName( "com.mysql.cj.jdbc.Driver" ); conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test_demo?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC" , "root" , "password" ); |
三、示例
1、引驱动包
2、建表
1 2 3 4 5 6 7 8 |
CREATE TABLE `websites` ( `id` int (11) NOT NULL AUTO_INCREMENT, ` name ` char (20) NOT NULL DEFAULT '' COMMENT '站点名称' , `url` varchar (255) NOT NULL DEFAULT '' , `alexa` int (11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名' , `country` char (10) NOT NULL DEFAULT '' COMMENT '国家' , PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; |
3、添加数据
1 2 3 4 5 |
INSERT INTO `test_demo`.`websites` (`id`, ` name `, `url`, `alexa`, `country`) VALUES ( '1' , 'Google' , 'https://HdhCmsTestgoogle.cm/' , '1' , 'USA' ); INSERT INTO `test_demo`.`websites` (`id`, ` name `, `url`, `alexa`, `country`) VALUES ( '2' , '淘宝' , 'https://HdhCmsTesttaobao测试数据/' , '13' , 'CN' ); INSERT INTO `test_demo`.`websites` (`id`, ` name `, `url`, `alexa`, `country`) VALUES ( '3' , 'java入门' , 'https://blog.csdn.net/wwwzhouzy/category_11249888.html' , '5892' , '' ); INSERT INTO `test_demo`.`websites` (`id`, ` name `, `url`, `alexa`, `country`) VALUES ( '4' , '微博' , 'http://weibo测试数据/' , '20' , 'CN' ); INSERT INTO `test_demo`.`websites` (`id`, ` name `, `url`, `alexa`, `country`) VALUES ( '5' , 'Facebook' , 'https://HdhCmsTestfacebook测试数据/' , '3' , 'USA' ); |
4、代码连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package com.zhouzy.base.t11; import java.sql.*;
public class MysqlTest { // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver" ; static final String DB_URL = "jdbc:mysql://localhost:3306/test_demo" ; // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL //static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; //static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
// 数据库的用户名与密码,需要根据自己的设置 static final String USER = "root" ; static final String PASS = "123456" ; public static void main(String[] args) { Connection conn = null ; Statement stmt = null ; try { // 注册 JDBC 驱动 Class.forName(JDBC_DRIVER); // 打开链接 System.out.println( "连接数据库..." ); conn = DriverManager.getConnection(DB_URL,USER,PASS); // 执行查询 System.out.println( " 实例化Statement对象..." ); stmt = conn.createStatement(); String sql; sql = "SELECT id, name, url FROM websites" ; ResultSet rs = stmt.executeQuery(sql); // 展开结果集数据库 while (rs.next()){ // 通过字段检索 int id = rs.getInt( "id" ); String name = rs.getString( "name" ); String url = rs.getString( "url" ); // 输出数据 System.out.print( "ID: " + id); System.out.print( ", 站点名称: " + name); System.out.print( ", 站点 URL: " + url); System.out.print( "\n" ); } // 完成后关闭 rs.close(); stmt.close(); conn.close(); } catch (SQLException se){ // 处理 JDBC 错误 se.printStackTrace(); } catch (Exception e){ // 处理 Class.forName 错误 e.printStackTrace(); } finally { // 关闭资源 try { if (stmt!= null ) stmt.close(); } catch (SQLException se2){ } // 什么都不做 try { if (conn!= null ) conn.close(); } catch (SQLException se){ se.printStackTrace(); } } System.out.println( "再见!" ); } } |
结果:
连接数据库...
实例化Statement对象...
ID: 1, 站点名称: Google, 站点 URL: https://HdhCmsTestgoogle.cm/
ID: 2, 站点名称: 淘宝, 站点 URL: https://HdhCmsTesttaobao测试数据/
ID: 3, 站点名称: java入门, 站点 URL: https://blog.csdn.net/wwwzhouzy/category_11249888.html
ID: 4, 站点名称: 微博, 站点 URL: http://weibo测试数据/
ID: 5, 站点名称: Facebook, 站点 URL: https://HdhCmsTestfacebook测试数据/
再见!
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!
原文链接:https://blog.csdn.net/wwwzhouzy/article/details/119300925
查看更多关于一篇文章带你了解java数据库连接的详细内容...