日期:2014-05-16  浏览次数:20884 次

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