简易JDBC数据库工具类

import java.sql.*;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class JDBCUtil {
    static String driver = "com.mysql.jdbc.Driver";
    static String url = "jdbc:mysql://IP:3306/数据库?useSSL=false&useUnicode=true&characterEncoding=utf-8";
    static String username = "用户名";
    static String password = "密码";

    public static void main(String[] args) throws Exception {
        Integer v = 1;
        Map paramMap = new TreeMap() {{
            put("title", "题目");
            put("time", new Date());
        }};
        insert("subject_record",paramMap);
        select("subject_record",paramMap);
//        insert();
//        select();
    }
    //插入
    public static int insert(String tableName, Map paramMap) throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection con = DriverManager.getConnection(url, username, password);
        StringBuilder columnSB = new StringBuilder();
        StringBuilder valueSB = new StringBuilder();
        for (Map.Entry entry : paramMap.entrySet()) {
            columnSB.append("`").append(entry.getKey()).append("`").append(",");
            if (entry.getValue() instanceof String) {
                valueSB.append("'").append(entry.getValue()).append("'").append(",");
            } else if(entry.getValue() instanceof Date){
                String date = DateFormatUtils.format((Date) entry.getValue(), "YYYY-MM-dd HH:mm:ss");
                valueSB.append("'").append(date).append("'").append(",");
            } else {
                valueSB.append(entry.getValue()).append(",");
            }
        }
        String column = columnSB.substring(0, columnSB.length() - 1);
        String value = valueSB.substring(0, valueSB.length() - 1);
        String sql = "INSERT INTO " + tableName + "(" + column + ") VALUES (" + value + ")";
        System.out.println(sql);
        PreparedStatement pst = (PreparedStatement) con.prepareStatement(sql);
        int row = pst.executeUpdate();
        System.out.println(row);  //打印结果为1,表示插入成功了
        con.close();
        pst.close();
        return row;
    }

    //查询
    public static void select(String tableName, Map paramMap) throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection con = DriverManager.getConnection(url, username, password);
        String append = "";
        if (paramMap != null) {
            StringBuilder item = new StringBuilder();
            item.append(" where ");
            for (Map.Entry entry : paramMap.entrySet()) {
                item.append("`").append(entry.getKey()).append("`").append("=");
                if (entry.getValue() instanceof String) {
                    item.append("'").append(entry.getValue()).append("'").append(" and ");
                } else if(entry.getValue() instanceof Date){
                    String date = DateFormatUtils.format((Date) entry.getValue(), "YYYY-MM-dd HH:mm:ss");
                    item.append("'").append(date).append("'").append(" and ");
                }  else {
                    item.append(entry.getValue()).append(" and ");
                }
            }
            append = item.substring(0, item.length() - 4);
        }
        String sql = "SELECT * FROM " + tableName + append;
        System.out.println(sql);
        PreparedStatement pst = (PreparedStatement) con.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        int col = rs.getMetaData().getColumnCount();
        while (rs.next()) {
            for (int i = 1; i <= col; i++) {
                System.out.println(rs.getString(i));
            }
        }
        con.close();
        pst.close();
    }
}

你可能感兴趣的:(拔出萝卜带出坑,JDBC,工具类,数据库操作)