mysql-connector 那个包已经导进去了,但就是显示找不到这个驱动
2020-04-24 15:06:15
package com.imooc.jdbc.demo1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import com.mysql.cj.jdbc.Driver;
public class JDBCDemo1 {
@Test
/**
* JDBC 的入门程序
*/
public void demo1() throws ClassNotFoundException {
//1、加载驱动
try {
DriverManager.registerDriver(new Driver());
Class.forName("com.mysql.cj.jdbc.Driver");
//2、获得链接
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GM\r\n" +
"T%2B8","root","1234");
//3、创建执行sql语句的对象,并且执行sql
//3、1创建执行sql的对象
String sql = "select * from user";
Statement stmt = conn.createStatement();
//3、2执行sql
ResultSet resultSet = stmt.executeQuery(sql);
while(resultSet.next()) {
int uid=resultSet.getInt("uid");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
String name = resultSet.getString("name");
System.out.println(uid+" "+username+" "+password+" "+name);
}
//4、释放资源
resultSet.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
老师这是我的代码
2020-04-24 15:06:50
同学你好,serverTimezone=GM\r\n" +
"T%2B8"不可以这样写,要写到一个整体哦:
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8","root","1234");
2020-04-24 15:15:35
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星