`
jslfl
  • 浏览: 313738 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

JDBC Batch 批量插入

阅读更多
JdbcBatchInsert.java
import java.sql.*;
public class JdbcBatchInsert {
    public static void main(String args[]) {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);
            con.setAutoCommit(false);// Disables auto-commit.
            st = con.createStatement();
            st.addBatch("INSERT INTO person VALUES('4','Komal')");
            st.addBatch("INSERT INTO person VALUES('5','Ajay')");
            st.addBatch("INSERT INTO person VALUES('6','Santosh')");
            st.executeBatch();
            String sql = "select * from person";
            rs = st.executeQuery(sql);
            System.out.println("No  \tName");
            while (rs.next()) {
                System.out.print(rs.getString(1) + "   \t");
                System.out.println(rs.getString(2));
            }
            rs.close();
            st.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}  




Output
--------------------
No      Name
4       Komal
5       Ajay
6       Santosh



JdbcPreparedstatementAddbatch.java
import java.sql.*;

public class JdbcPreparedstatementAddbatch {

    public static void main(String args[]) {

        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";

        try {

            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);

            pst = con.prepareStatement("insert into lib value(?,?)");
            con.setAutoCommit(false);

            pst.setString(1, "6");
            pst.setString(2, "106");
            pst.addBatch();

            pst.setString(1, "7");
            pst.setString(2, "107");
            pst.addBatch();

            pst.setString(1, "8");
            pst.setString(2, "108");
            pst.addBatch();

            pst.executeBatch();

            pst.close();

            String sql = "select * from lib";
            pst = con.prepareStatement(sql);

            rs = pst.executeQuery();

            System.out.println("rno\tlibno");
            while (rs.next()) {
                System.out.print(rs.getString(1) + "   \t");
                System.out.println(rs.getString(2));
            }
            rs.close();
            pst.close();
            con.close();

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




Output
----------------------------
Table Name : Employees
Field        Type
--------------------
EmployeeID     11
FirstName     50
LastName     50


转自http://www.mytju.com/classcode/news_readNews.asp?newsID=221
分享到:
评论

相关推荐

    Mybatis与JDBC批量插入MySQL数据库性能测试

    Mybatis与JDBC批量插入MySQL数据库性能测试,资源包含文档、代码和数据库。

    hibernate-batch-size-test:Hibernate hibernate.jdbc.batch_size 测试

    Hibernate hibernate.jdbc.batch_size 测试带有 MySQL JDBC 驱动程序的 Hibernate 批量插入示例。 에 관한 설명

    使用JDBC在MySQL数据库中如何快速批量插入数据

    使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢? 在JDBC编程接口中Statement 有两个方法特别值得注意: void addBatch() throws SQLException Adds a set of ...

    Hibernate查询语言

    14.1. 批量插入(Batch inserts) 如果要将很多对象持久化,你必须通过经常的调用 flush() 以及稍后调用 clear() 来控制第一级缓存的大小。 Session session = sessionFactory.openSession(); Transaction tx = ...

    sb-jpa-batch-insert-demo

    在Spring Data JPA应用程序中批量插入示例 所有你需要的是: 将选项spring.jpa.properties.hibernate.jdbc.batch_size设置为所需的值。 将您的saveAll()方法与准备插入的实体列表一起使用。 运行此应用程序,然后...

    详解jdbc实现对CLOB和BLOB数据类型的操作

    主要介绍了详解jdbc实现对CLOB和BLOB数据类型的操作的相关资料,这里实现写入操作与读写操作,需要的朋友可以参考下

    Transwarp-Data-Hub-Collection-Project

    跨经 该存储库包含上组件的示例项目(此存储库主要包含项目)。 Transwarp的Apache Hadoop for Enterprise发行版... Inceptor JDBC批量插入数据的示例。 Kerberos支持 ComplexSearch。 通过Inceptor JDBC(由Elasti

    Hibernate+中文文档

    13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) 14. HQL: Hibernate查询语言 14.1....

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) 14. HQL: Hibernate查询语言 14.1....

    HibernateAPI中文版.chm

    13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) 14. HQL: Hibernate查询语言 14.1....

    hibernate3.2中文文档(chm格式)

    13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) 14. HQL: Hibernate查询语言 14.1....

    hibernate 框架详解

    批量插入(Batch inserts) 14.2. 批量更新(Batch updates) 14.3. 大批量更新/删除(Bulk update/delete) 15. HQL: Hibernate查询语言 15.1. 大小写敏感性问题 15.2. from子句 15.3. 关联(Association)与...

    最全Hibernate 参考文档

    13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. 大批量更新/删除(Bulk update/delete) 14. HQL: Hibernate查询语言 14.1. 大小写敏感性问题 14.2. from子句 14.3. 关联(Association)与...

    Hibernate3+中文参考文档

    13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. 大批量更新/删除(Bulk update/delete) 14. HQL: Hibernate查询语言 14.1. 大小写敏感性问题 14.2. from子句 14.3. 关联(Association)与...

    Hibernate参考文档

    13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) 14. HQL: Hibernate查询语言 14.1. 大小...

    hibernate3.04中文文档.chm

    14.1. 批量插入(Batch inserts) 14.2. 批量更新(Batch updates) 14.3. 大批量更新/删除(Bulk update/delete) 15. HQL: Hibernate查询语言 15.1. 大小写敏感性问题 15.2. from子句 15.3. 关联...

    Hibernate教程

    14.1. 批量插入(Batch inserts) 14.2. 批量更新(Batch updates) 14.3. 大批量更新/删除(Bulk update/delete) 15. HQL: Hibernate查询语言 15.1. 大小写敏感性问题 15.2. from子句 15.3. 关联(Association...

    Hibernate 中文 html 帮助文档

    13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) 14. HQL: Hibernate查询语言 14.1. 大小...

    Hibernate中文详细学习文档

    13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) 14. HQL: Hibernate查询语言 14.1....

    hibernate 体系结构与配置 参考文档(html)

    批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) 14. HQL: Hibernate查询语言 14.1. 大小...

Global site tag (gtag.js) - Google Analytics