package com.my.connection;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.Test;
public class ConnectionTest {
// 方式一:
@Test
public void testConnection1() throws SQLException {
// 获取Driver实现类对象
// Driver driver = new com.mysql.jdbc.Driver();
// 上面语句是错的,跟我当前使用的 MySQL80 版不兼容,换为下面语句同时更新到相应的 jdbc驱动 版本就可以了
Driver driver = new com.mysql.cj.jdbc.Driver();
// url:http://localhost:8080/gmall/keyboard.jpg
// jdbc:mysql:协议
// localhost:ip地址
// 3306:默认mysql的端口号
// test:test数据库
String url = "jdbc:mysql://localhost:3306/test";
// 将用户名和密码封装在Properties中
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "abc123");
Connection conn = driver.connect(url, info);
System.out.println(conn);
}
-
环境:java1.8.301 + MySQL8.0.26+IDEA2021/eclipse2021.9 EE
-
遇到的问题是:控制台输出 null
-
解决方法:由于我使用的是目前最新的MySQL8.0.26,所以跟之前的jdbc5.1.7不兼容,所以我更新驱动,然后重新获取最新 Driver 对象就可以了;
-
启发:在跟老师们学习的过程中,用的各种软件程序的版本尽量跟他的一样,否则最好什么各个软件程序都用最新的。



