JDBC 驱动程序的链接
1.mysql的JDBC驱动程序的下载
mysql
http://www.mysql.com/products/connector/j/
Shipped. But need to download the latest for MySQL 4.1 or higher.
可以直接进入下面这个网址进行下载,下载后直接解压,不需要配置环境变量。
https://dev.mysql.com/downloads/file/?id=485766
2.IntelliJ IDEA 的JDBC导入、配置
-
file->project structure->libraries-> + ->java
-
找到JDBC的安装路径下的jar文件,并点击OK确定链接。
3.数据库的建立
运行Mysql 5.5程序 创建数据库
4.程序代码的编写
package demo;
import java.sql.*;
/**
* 测试数据库
*/
public class halo {
//mysql驱动包名
private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
//数据库连接地址
private static final String URL = "jdbc:mysql://localhost:3306/xyc?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
//用户名
private static final String USER_NAME = "root";
//密码
private static final String PASSWORD = "root";
public static void main(String[] args){
Connection connection = null;
try {
//加载mysql的驱动类
Class.forName(DRIVER_NAME);
//获取数据库连接
connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
//mysql查询语句
String sql = "SELECT name FROM xyc";
PreparedStatement prst = connection.prepareStatement(sql);
//结果集
ResultSet rs = prst.executeQuery();
while (rs.next()) {
System.out.println("用户名:" + rs.getString("name"));
}
rs.close();
prst.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
参考网址:
各种数据库的jdbc驱动下载及连接方式:
https://blog.csdn.net/u012338954/article/details/52294851
JDBC中The server time zone value ‘???ú±ê×??±??’ is … 的错误:
https://blog.csdn.net/weixin_37577564/article/details/80329775
Spsingboot学习mysql驱动报错This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver’. The dri:
https://blog.csdn.net/qq_16217663/article/details/83629466
IntelliJ IDEA 如何创建一个普通的java项目,及创建java文件并运行:
https://blog.csdn.net/oschina_41790905/article/details/79475187