package com.tool;
import java.sql.*;
public class JdbcTool {
private String driverUrl = "com.mysql.cj.jdbc.Driver";// = "com.mysql.cj.jdbc.Driver";
private String jdbcUrl = "jdbc:mysql://localhost:3306/zhuo_tmp?serverTimezone=GMT%2B8&useSSL=false";//="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
private String user = "root";// = "root";
private String pass = "root";// = "root";
Connection conn = null;
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rs =null;
JdbcTool(int mode) throws SQLException, ClassNotFoundException {
Class.forName(driverUrl);
conn = DriverManager.getConnection(jdbcUrl,user,pass);
if (mode == 1){
stmt=conn.createStatement();
} else if (mode == 2){
}
}
public void prepareStatementDemo1(String sql) throws SQLException {
}
public void statementDemo1(String sql) throws SQLException {
sql = "select * from t1 limit 10";
rs = stmt.executeQuery(sql);
//输出结果:
while(rs.next()){
System.out.print("==获取字段1:" + rs.getObject("id"));
System.out.print("==获取字段2:" + rs.getObject("name"));
System.out.println();
}
close(stmt,conn,rs);
}
public void closeOfP(PreparedStatement preparedStatement,Connection conn,ResultSet resultSet ) throws SQLException {
preparedStatement.close();
conn.close();
resultSet.close();
}
public void close(Statement preparedStatement,Connection conn,ResultSet resultSet ) throws SQLException {
preparedStatement.close();
conn.close();
resultSet.close();
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
JdbcTool jdbcTool = new JdbcTool(1);
jdbcTool.statementDemo1("");
}
}