1.pom引入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
2.
public static void main(String[] args) {
// DriverManager.registerDriver(new com.mysql);
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/babytun?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true",
"root",
"123456"
);
Statement statement = connection.createStatement();
String sql = "select * from t_goods limit 10";
ResultSet res = statement.executeQuery(sql);
// String id =null;
while(res.next()){
String id = res.getString("goods_id");
String name = res.getString(2);
String price = res.getString(4);
System.out.println(id+"==="+name+"===="+price+"元");
}
statement.close();
connection.close();
System.out.println(connection);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}