[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[转贴]JDBC批量插入的例子(JDBC batch insert,JDBC Prepared Statement Addbatch)

上一篇:[备忘]JBOSS 配置连接工厂,JBOSS Configuring Connection Factories
下一篇:[备忘]IE不支持overrideMimeType()方法,即使是IE7.

添加日期:2009/1/8 11:18:41 快速返回   返回列表 阅读10038次

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
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved