为什么照着老师抄代码都会报错……
package com.imooc.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DBUtil {
private static final String URL="jdbc:mysql://127.0.0.1:3306/imooc";
private static final String USER="root";
private static final String PASSWORD="123";
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select user_name,age from imooc_goddess");
while(rs.next()){
System.out.println(rs.getString("user_name")+","+rs.getInt("age"));
}
}
}
将常量值改成这个就行了
private static final String URL = "jdbc:mysql://127.0.0.1:3306/imooc?serverTimezone=UTC";
查看一下你的jar版本 mysql-connector-java-8.0.13.jar
如果是这个版本:
private static String URL = "jdbc:mysql://localhost:3306/sakila?serverTimezone=UTC";
private static String USER = "root";
private static String PASSWORD = "root";
public static void main(String[] args) throws Exception {
// 1.加载驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.获得数据库的连接
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
// 3.通过数据库的连接操作数据库,实现增删改查
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from sakila.actor;");
while (rs.next()) {
System.out.println(rs.getInt("actor_id") + ": " + rs.getString("first_name") + " " + rs.getString("last_name"));
}
}
如果不是这个版本把你eclipse中报错的信息百度一下就行
一直提示我Class.forName("com.mysql.jdbc.Driver");这句话有错误