Java数据库批量添加

package constant;
import com.QQJson;
import org.springframework.stereotype.Component;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public final class QQDB {
    //此方法为获取数据库连接
    public static Connection getConnection() {
        Connection conn = null;
        try {
            String driver = QQCom.driver; //数据库驱动
            String url = QQCom.urlAll;//数据库
            String user = QQCom.username; //用户名
            String password = QQCom.password; //密码
            Class.forName(driver); //加载数据库驱动
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            System.out.println("Sorry,can't find the Driver!");
            e.printStackTrace();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static int exeSql(String sql) {
        int result = 0;
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            stmt = conn.createStatement();
            result = stmt.executeUpdate(sql);
        } catch (SQLException err) {
            err.printStackTrace();
            free(null, stmt, conn);
        } finally {
            free(null, stmt, conn);
        }
        return result;
    }

    public static int exeSqlGetID(String sql) {
        int result = 0;
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            stmt = conn.createStatement();
            result = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
            ResultSet rs = stmt.getGeneratedKeys();
            if (rs.next()) {
                result = rs.getInt(1);
            }
        } catch (SQLException err) {
            err.printStackTrace();
            free(null, stmt, conn);
        } finally {
            free(null, stmt, conn);
        }
        return result;
    }
    /**
     * 批量插入
     *
     * @param lt
     * @return
     */
    public static int exeSqlTran(List<String> lt) {
        int result = 0;
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            stmt = conn.createStatement();
            conn.setAutoCommit(false);
            for (String s : lt) {
                stmt.addBatch(s);
            }
            result = stmt.executeBatch().length;
            conn.commit(); // 事务提交
            conn.setAutoCommit(true);
        } catch (SQLException err) {
            err.printStackTrace();
            free(null, stmt, conn);
        } finally {
            free(null, stmt, conn);
        }
        return result;
    }

    public static <T> List<T> GetList(String sql, Class<?> eClass) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<T> lt = new ArrayList<>();
        try {
            conn = getConnection();// 创建MySQL连接,参数依次为数据库连接地址、用户和密码
            pstmt = conn.prepareStatement(sql);
            // 拼接查询SQL,将id的值拼接到querySql中的第一个问号里
            rs = pstmt.executeQuery();
            // 获取ResultSet对象的列的数量、类型和属性。
            ResultSetMetaData md = rs.getMetaData();
            // 将ResultSet对象的列名和值存到map中,再将map转换为json字符串,最后将json字符串转换为实体类对象
            Map<String, Object> rowData = new HashMap<>();
            while (rs.next()) {
                for (int i = 1; i <= md.getColumnCount(); i++) {
                    rowData.put(md.getColumnLabel(i), rs.getObject(i));
                }
                String jsonStr = QQJson.toJsonString(rowData);
                T t = (T) QQJson.toModel(jsonStr, eClass);
                lt.add(t);
            }

        } catch (SQLException err) {
            err.printStackTrace();
            free(rs, pstmt, conn);
        }
        return lt;
    }

    //region 不用
    public static void free(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException err) {
            err.printStackTrace();
        }
    }

    public static void free(Statement st) {
        try {
            if (st != null) {
                st.close();
            }
        } catch (SQLException err) {
            err.printStackTrace();
        }
    }

    public static void free(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException err) {
            err.printStackTrace();
        }
    }

    public static void free(ResultSet rs, Statement st, Connection conn) {
        free(rs);
        free(st);
        free(conn);
    }
    //endregion
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值