【simple】JDBC访问数据库 (入门) tool1

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("");
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值