学习java中的数据库章节 ,需要使用jdbc链接mysql,用docker做了个mysql,然后用idea的数据库链接没问题。但是编译的时候发生错误
先附上编译错误的代码,如下所示:
package connection;
import com.mysql.cj.jdbc.Driver;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
//import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
public class jdbc01_Connection {
@Test
public void testConnection1() throws SQLException {
Driver driver = new com.mysql.cj.jdbc.Driver();
//jdbc:mysql:协议
//localhost:ip地址
//3306:默认mysql的端口号
//guigujdbc指的是数据库名称
Properties info = new Properties();
String url = "jdbc:mysql://localhost:8801/guigujdbc";//guigujdbc指的是数据库名称
//将用户名和密码封装在Properties中
info.setProperty("user","root");
info.setProperty("password","123456");
Connection conn = driver.connect(url, info);
System.out.println(conn);
}
}
编译后报错误
Exception in thread "main" java.lang.UnsupportedClassVersionError
发现不能用import java.sql.Driver应该是com.mysql.cj.jdbc.Driver,我的是jdk18