MySQL Database---Batch processing with large text/picture types

Source: Internet
Author: User
Tags uuid

To do anything in Java for the database, the first thing is to get the database connection, the author is the configuration file to load the database information, the configuration file named Db.properties, the content is as follows

Driver=com.mysql.jdbc.driver
Url=jdbc:mysql://localhost:3306/customermanage
Username=root
Password=root

The contents of the ps:properties are in the form of key-value pairs and do not require quotation marks

Next is the Dbutil class, get the configuration, get the connection

 PackageCom.cherry.utils;ImportJava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.PreparedStatement;ImportJava.sql.ResultSet;ImportJava.sql.SQLException;ImportJava.util.Properties;ImportJava.util.ResourceBundle; Public  class dbutils {    StaticString driver;StaticString URL;StaticString username;StaticString password;Static{Try{//Class.forName ("Com.mysql.jdbc.Driverr");            //Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/customermanage",            //"root", "root");ResourceBundle RB = Resourcebundle.getbundle ("DB"); Driver = rb.getstring ("Driver");            Class.forName (driver); url = rb.getstring ("url"); Username = rb.getstring ("username"); Password = rb.getstring ("Password"); }Catch(ClassNotFoundException e)        {E.printstacktrace (); }    } Public StaticConnectionGetconn()throwsSQLException {returnDrivermanager.getconnection (URL, username, password); } Public Static void Releaseres(ResultSet RS, PreparedStatement PS, Connection conn)throwsSQLException {if(rs! =NULL) {rs.close (); }if(PS! =NULL) {ps.close (); }if(Conn! =NULL) {conn.close (); }    }}

Let's start with the batch operation class.

Packagecom. Cherry. Batch;Import Java. SQL. Connection;Import Java. SQL. PreparedStatement;Import Java. SQL. SQLException;Importcom. Cherry. Utils. Dbutils;public class Batchsql {public static void main (string[] args) throws SQLException {Connection conn= dbutils. Getconn();String sql="INSERT into des value (?,?)";PreparedStatement Ps=conn. Preparestatement(SQL);for (int i=0; i<10000; i++) {Ps. setString(1,"Name"+i);Ps. setString(2,"des"+i);Ps. Addbatch();if (i% -==0) {PS. ExecuteBatch();Ps. Clearbatch();}    }}}

Look at the storage of large text or picture types again

Packagecom. Cherry. Batch;Import Java. IO. File;Import Java. IO. FileInputStream;Import Java. IO. FileReader;Import Java. IO. Reader;Import Java. SQL. Connection;Import Java. SQL. PreparedStatement;Import Java. Util. UUID;Importcom. Cherry. Utils. Dbutils;public class Textsql {public static void main (string[] args) {try {Connection Connection = dbutils. Getconn();String sql ="insert into des values (?,?)";PreparedStatement PS = connection. Preparestatement(SQL);Ps. setString(1Uuid. Randomuuid(). toString());File File = new file ("F://test.txt");Reader reader = new FileReader (file);Here is not necessarily a strong turn, look at your MySQL driver version, in short, do not turn a try PS. Setcharacterstream(2, reader, (int) file. Length());Ps. Executeupdate();} catch (Exception e) {E. Printstacktrace();}    }}

In addition, notice the Guide package, here all the database operation class is java.sql, think also know, interface programming Java June, certainly is using SQL package (universality), because mysql/sqlserver/orcale these are implementation class, with they certainly not suitable.

MySQL Database---Batch processing with large text/picture types

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.