回不去的jdbc
package com.she.service;
import java.sql.*;
public class JdbcService {
public static void main(String[] args) {
JdbcService jdbc = new JdbcService();
jdbc.comnectionJDBC();
}
public void comnectionJDBC(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://127.0.0.1:3306/fate?serverTimezone=UTC";
Connection conn = null;
Statement stat = null;
ResultSet resultSet = null;
try{
conn = DriverManager.getConnection(url,"root","root123");
stat = conn.createStatement();
String sql = "select * from t_user";
resultSet = stat.executeQuery(sql);
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("id="+id+",name="+name);
}
}catch (SQLException e){
e.printStackTrace();
}finally{
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stat != null) {
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}