2021年最新Eclipse连接MySQL数据库教程!

羚羊公子
2021-05-24 / 0 评论 / 119 阅读 / 正在检测是否收录...

Test

前言:

1.使用Eclipse连接数据库需要MySQL驱动程序!
2.Java Database Connectivity 简称JDBC
3.JDBC下载地址:点击下载

配置教程:

连接数据库代码:
package kglx;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;import java.sql.Statement;
public class jcx {
private final static String url = "jdbc:mysql://localhost:3306/mysql?serverTimezone=GMT";
private final static String username = "root";
private final static String password = "j030110";
public static void main(String args[]) {
try {
Class.forName("com.mysql.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 student2");
while (rs.next()) {
System.out.print("id:" + rs.getInt("id"));
System.out.print("id:" + rs.getString("id"));
System.out.println("name:" + rs.getString("name"));
System.out.println("stusex:" + rs.getString("stusex"));
System.out.println("age:" + rs.getString("age"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

注意:

5

评论 (0)

取消
CDN Sponsor Server provider Upyun