初学JDBC连接MySQL遇到返回null的情况

时间:2025-02-18 07:20:16
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 (); // 上面语句是错的,跟我当前使用的 MySQL80 版不兼容,换为下面语句同时更新到相应的 jdbc驱动 版本就可以了 Driver driver = new com.mysql.cj.jdbc.Driver(); // url:http://localhost:8080/gmall/ // 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); }