JDBC使用addBatch一次插入 万条数据

public class Conn
{

    public static void main(String[] args) throws SQLException
    {
        Connection conn = getConnection();
        show(conn);
        // addBatch(con);
        conn.close();
    }

    public static void addBatch(Connection conn) throws SQLException
    {
        PreparedStatement pre = null;
        String sql = "insert into s values(?,?)";
        pre = conn.prepareStatement(sql);
        // pre.setInt(1, 10);
        // pre.setString(2, "name" + 10);
        // pre.executeUpdate();
        pre = conn.prepareStatement(sql);
        for (int i = 10; i < 20; i++)
        {
            pre.setInt(1, i);
            pre.setString(2, "name" + i);
            pre.addBatch();
            if (i % 2 == 0)
            {
                pre.executeBatch();
                pre.clearParameters();
            }
        }
        pre.executeBatch();

        pre.close();
        conn.commit();

    }

    public static void show(Connection conn) throws SQLException
    {
        String sql = "select * from s ";
        PreparedStatement pre = null;
        pre = conn.prepareStatement(sql);
        ResultSet rs = pre.executeQuery();
        while (rs.next())
        {
            System.out.println(rs.getString(1) + rs.getString(2));
        }

    }

    public static Connection getConnection()
    {
        String ClassforName = "oracle.jdbc.driver.OracleDriver";
        String SERVandDB = "jdbc:oracle:thin:@10.164.75.166:1521:infox"; // "DataSource,DataName不能用
        String USER = "bdk"; // 用户名
        String PWD = "bdk"; // 密码
        Connection conn = null;
        try
        {
            Class.forName(ClassforName).newInstance();
            conn = DriverManager.getConnection(SERVandDB, USER, PWD);
            conn.setAutoCommit(false);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return conn;
    }

}
 

你可能感兴趣的:(oracle,sql,jdbc)