java 多线程存储数据库

2025-01-01 05:13:14
推荐回答(1个)
回答1:

以mysql为数据库写的一个粗陋的demo,你参考一下,希望不会因为代码过多被百度吞了——


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Test {
       
    public static void main(String[] args) {
        allotThread();
    }
       
    /**
     * 将100条数据分成10份并启动10个线程分别操作
     */
    public static void allotThread() {
        List datas = buildDatas();
        for (int i=0; i<100; i+=10) {
            List tenDatas = datas.subList(i, i + 10);
            insertData(tenDatas);
        }
    }
       
    /**
     * 创建100条模拟数据
     * @return
     */
    public static List buildDatas() {
        List datas = new ArrayList();
        for (int i=0; i<100; i++) {
            String[] data = {"id " + i, "name " + i};
            datas.add(data);
        }
        return datas;
    }
       
    /**
     * 启动线程进行数据插入操作
     * @param tenDatas
     */
    public static void insertData(final List tenDatas) {
        new Thread(new Runnable() {
            public void run() {
                String sql = "insert into testtable (id, name) values (?, ?)";
                Connection conn = null;
                PreparedStatement pstmt = null;
                try {
                    conn = getConnection();
                    conn.setAutoCommit(false);
                    pstmt = getPstmt(conn, sql);
                    for (String[] data : tenDatas) {
                        pstmt.setString(1, data[0]);
                        pstmt.setString(2, data[1]);
                        pstmt.addBatch();
                    }
                    pstmt.executeBatch();
                    conn.commit();
                    conn.setAutoCommit(true);
                } catch (SQLException e) {
                    e.printStackTrace();
                    rollback(conn);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    close(pstmt);
                    close(conn);
                }
            }
        }).start();
    }
       
    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        String dbUrl = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8";
        Connection conn = DriverManager.getConnection(dbUrl, "root", "tooeasy");
        return conn;
    }
       
    public static PreparedStatement getPstmt(Connection conn, String sql) throws SQLException, ClassNotFoundException {
        PreparedStatement pstmt = conn.prepareStatement(sql);
        return pstmt;
    }
       
    public static void rollback(Connection conn) {
        try {
            if (null != conn) {
                conn.rollback();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
       
    public static void close(Connection conn) {
        try {
            if (null != conn) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
       
    public static void close(PreparedStatement pstmt) {
        try {
            if (null != pstmt) {
                pstmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
       
    public static void close(ResultSet rs) {
        try {
            if (null != rs) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}