jdbc封装

package com.prosay.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbUtil {
    public static void main(String[] args) throws SQLException {
        DbUtil db = new DbUtil();
        int i = db.doUpdate("insert into tb_cinema (c_id,c_name,c_pingfen,c_entry_time,c_loc)values(?,?,?,?,?) ", 8, "李小龙传奇",200,"2018-11-11","上海");
        System.out.println(i);
        ResultSet rs=db.doQuery("select * from tb_cinema");
     db.showResultSet(rs);
    }

    private String user = "root";
    private String password = "123456";
    private String url = "jdbc:mysql://localhost:3306/db_star?character=utf-8";
    private String database = "db_star";
    private Connection conn = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;
    private String className = "com.mysql.jdbc.Driver";
    private String charset = "utf-8";
    private int port = 3306;
    private String serverName="localhost";

    public DbUtil() {
        this(null, null, null, null);
        this.updateUrl();
    }

    public DbUtil(String className) {
        this(null, null, null, className);
        this.updateUrl();

    }

    public DbUtil(String user, String password) {

        this(null, null, user, password);
        this.updateUrl();
    }

    public DbUtil(String url, String user, String password) {
        this(null, url, user, password);
this.updateUrl();
    }

    public DbUtil(String className, String url, String user, String password) {
        if (className != null) {
            this.className = className;
        }
        if (url != null) {
            this.url = url;
        }
        if (user != null) {
            this.user = user;
        }
        if (password != null) {
            this.password = password;
        }
        init();

    }
public void updateUrl() {
    this.url="jdbc:mysql://"+serverName+":"+port+"/"+database+"?characterEncoding="+charset;
}
    public void init() {
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public Connection getConnection() throws SQLException {
        conn = DriverManager.getConnection(url, user, password);
        return conn;
    }

    public PreparedStatement getpreparedstatement(String sql, Object[] params) throws SQLException {
        ps = getConnection().prepareStatement(sql);
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
        }

        return ps;

    }

    public ResultSet doQuery(String sql, Object... params) throws SQLException {
        ResultSet rs = getpreparedstatement(sql, params).executeQuery();

        return rs;

    }

    public void close() throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }
/**
 * 更新语句
 * @param sql
 * @param params
 * @return
 * @throws SQLException
 */
    public int doUpdate(String sql, Object... params) throws SQLException {
        PreparedStatement ps = getpreparedstatement(sql, params);
        return ps.executeUpdate();
    }
/**
 * 展示最终结果
 * @param rs
 * @throws SQLException
 */
    public void showResultSet(ResultSet rs) throws SQLException {
        if (rs != null) {
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                System.out.print(rs.getMetaData().getColumnName(i));
                if (i == rs.getMetaData().getColumnCount()) {
                    System.out.println();
                } else {
                    System.out.print("\t");
                }
            }
        }
        rs.beforeFirst();
        while (rs.next()) {
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                System.out.print(rs.getObject(i));
                if (i == rs.getMetaData().getColumnCount()) {
                    System.out.println();
                } else {
                    System.out.print("\t");
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值