下载驱动
Mysql官网: https://dev.mysql.com/downloads/connector/j/


将压缩包移动到MySQL文件夹下,并解压

Eclipse新建一个工程
打开eclipse,新建一个Student工程


选中项目右键点击Build Path>Add External Archives

选中刚刚解压出的jar包

新建一个test类


输入以下代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class test {
private final static String url = "jdbc:mysql://localhost:3306/student?serverTimezone=GMT"; //设置连接路径
private final static String username = "root"; //数据库用户名
private final static String password = "1234"; //数据库连接密码
public static void main(String arg[]) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {
e.printStackTrace();
}
try {
Connection connect = DriverManager.getConnection(url, username, password);
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select*from student_ei");
while (rs.next()){
System.out.println(rs.getString( "name")) ;
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
新建一个数据库
这个可以参考这篇文章,也可以用你已有的数据库做
MySQL数据库
运行
右键选择Run As>1 Java Application

本文指导如何下载MySQL驱动并在Eclipse中新建项目。首先,从MySQL官网下载connector/j驱动并将其添加到项目中。接着,创建一个新的Java工程,通过BuildPath添加外部jar包。然后,编写Java代码建立数据库连接,读取student_ei表中的数据。最后,运行程序,展示从数据库获取的数据。
2508

被折叠的 条评论
为什么被折叠?



