java 学生管理系统连接MYSQL数据库
时间: 2023-11-03 16:37:03 浏览: 95
要连接MySQL数据库,需要先下载MySQL Connector/J驱动程序,然后在Java项目中导入该驱动程序。
1. 下载MySQL Connector/J驱动程序
在MySQL官网上下载MySQL Connector/J驱动程序,下载地址为:https://dev.mysql.com/downloads/connector/j/
2. 导入MySQL Connector/J驱动程序
将下载的MySQL Connector/J驱动程序解压后,将其中的mysql-connector-java-xxx.jar文件复制到Java项目的lib目录下,并在项目中添加该jar包到classpath中。
3. 连接MySQL数据库
连接MySQL数据库的步骤如下:
1) 加载数据库驱动程序
Class.forName("com.mysql.jdbc.Driver");
2) 建立数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名", "用户名", "密码");
3) 创建Statement对象
Statement stmt = conn.createStatement();
4) 执行SQL语句
ResultSet rs = stmt.executeQuery("SELECT * FROM 学生表");
5) 处理结果集
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
int age = rs.getInt("age");
// 处理查询结果
}
6) 关闭连接
rs.close();
stmt.close();
conn.close();
以上是Java连接MySQL数据库的基本步骤,具体实现可以根据自己的需求进行调整。
阅读全文
相关推荐


















