Java uses JDBC to insert data into the database in batches (Big Data)

Source: Internet
Author: User

Http://www.javait.org /? P = 877

Package COM. test. bigdatatodb; import javax. swing. jframe; import javax. swing. jbutton; import javax. swing. imageicon; import javax. swing. jlabel; import javax. swing. jtextarea; import javax. swing. jtextfield; import javax. swing. jpasswordfield; import javax. swing. jcombobox; import javax. swing. jradiobutton; import javax. swing. buttongroup; import Java. AWT. flowlayout; import Java. AWT. color; import Java. SQL. connection; Im Port Java. SQL. drivermanager; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement; import Java. text. simpledateformat; import Java. util. calendar; import Java. util. date; import Java. util. gregoriancalendar;/*** @ author administrator * guoxin anbaijie high-end Java training */public class test extends jframe {public final string url = "JDBC: sqlserver: // 127.0.0.1: 1433; databasename = masdb "; public final S Tring username = "sa"; public final string Password = "sa"; public final string classdriver = "com. microsoft. sqlserver. JDBC. sqlserverdriver "; public connection conn; public statement stmt; Public resultset RS; public test () {try {class. forname (classdriver);} catch (classnotfoundexception e) {e. printstacktrace () ;}}/*** use JDBC to create a table */Public void createtable () {string SQL = "CREATE TABLE batch (A1 varch AR (10), A2 varchar (500) "; try {conn = drivermanager. getconnection (URL, username, password); stmt = Conn. createstatement (); Boolean F = stmt.exe cute (SQL); system. out. println (f);} catch (sqlexception e) {e. printstacktrace () ;}}/*** batch data insertion Operation on the database * execution times 1 million * @ throws sqlexception */Public void insertbatch () throws sqlexception {// train of thought: divide 1 million pieces of data into N equal portions, and 1 equal portion is 1000 pieces of data. // how can this problem be achieved? // 1. You must change the automatic submission method of the connection interface to manual. // 2. Use the following three methods in the statement interface: addbatch, clearbath, and executebatchsystem. out. println ("start to insert data:" + gettodaytime (); try {conn = drivermanager. getconnection (URL, username, password); Conn. setautocommit (false); stmt = Conn. createstatement (); For (INT I = 0; I <1000000; I ++) {string SQL = "insert into batch values ('" + I + "', '+ "+ I +" data') "; // use the addbatch method to add SQL statements to stmt objects. addbatch (SQL); I F (I % 1000 = 0 & I! = 0) {// your cutebatch (); stmt. clearbatch (); Conn. commit (); close (); // close the resource} catch (sqlexception e) {Conn. rollback (); // roll back e. printstacktrace ();} finally {system. out. println ("end data insertion:" + gettodaytime () ;}/ *** get today's time * @ return */public static string gettodaytime () {date = new date (); // obtain the time calendar = new Gregor Iancalendar (); Calendar. settime (date); Date = calendar. gettime (); // this time is the result of one day after the date. simpledateformat formatter = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); string datestring = formatter. format (date); Return datestring;} public void close () {try {If (RS! = NULL) Rs. Close (); If (stmt! = NULL) stmt. Close (); If (Conn! = NULL) Conn. close ();} catch (sqlexception e) {e. printstacktrace () ;}/ *** @ Param ARGs */public static void main (string [] ARGs) {test T = new test (); // T. createtable (); // test the first step. First, use this method to create a table try {T. insertbatch (); // test step 2, insert data in batches into the table} catch (sqlexception e) {e. printstacktrace ();}}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.