报错了:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jc.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.qin.mysql.main(mysql.java:8)
我写的代码:
```java
package com.qin;
import java.sql.*;
public class mysql {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
//1.加载驱动
Class.forName("com.mysql.jc.jdbc.Driver"); // 固定写法,加载驱动
//2.用户信息和url
String url = "jdbc:mysql://localhost:3306/jabcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root";
String psaaword = "123456";
//3.连接成功,数据库对象 Connection 代表数据库
Connection connection = DriverManager.getConnection(url, username, psaaword);
//4.执行SQL的对象 Statement 执行sql的对象
Statement statement = connection.createStatement();
//5.执行SQL的对象 去 执行SQL,可能存在结果,查看返回结果
String sql = "SELECT * FROM `users`";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
System.out.println("id="+resultSet.getObject("id"));
System.out.println("name="+resultSet.getObject("NAME"));
System.out.println("pwd="+resultSet.getObject("PASSWORD"));
System.out.println("email="+resultSet.getObject("email"));
System.out.println("birth="+resultSet.getObject("birthday"));
}
//6.释放连接
resultSet.close();
statement.close();
connection.close();
}
}
```