Big Data Storage and batch statement execution

Source: Internet
Author: User
Package COM. itheima. dao; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. filereader; import Java. io. filewriter; import Java. io. reader; import Java. io. writer; import Java. SQL. connection; import Java. SQL. preparedstatement; import Java. SQL. resultset; import Java. SQL. sqlexception; import Org. JUnit. test; import COM. itheima. util. jdbcutil; public class d1dao {// Big Data Storage @ testpublic void addt1 () Th Rows exception {connection conn = NULL; preparedstatement PST = NULL; resultset rs = NULL; file Fr = new file ("D: // 1.txt "); reader reader = new filereader (FR); int Len = 0; try {conn = jdbcutil. getconn (); string SQL = "insert into T1 values (?,?) "; PST = Conn. preparestatement (SQL); PST. setint (1, 1); // clob represents a large number of character data PST. setcharacterstream (2, reader, (intw.fr.length(~~~pst.exe cuteupdate ();} catch (sqlexception e) {e. printstacktrace ();}}}
 
Package COM. itheima. dao; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. inputstream; import Java. io. outputstream; import Java. SQL. connection; import Java. SQL. preparedstatement; import Java. SQL. resultset; import Java. SQL. sqlexception; import Org. JUnit. test; import COM. itheima. util. jdbcutil; public class blobdao {@ testpubli C void addblob () {connection conn = NULL; preparedstatement PST = NULL; try {conn = jdbcutil. getconn (); string SQL = "insert into T2 (ID, content) values (?,?) "; PST = Conn. preparestatement (SQL); PST. setint (1, 1); inputstream is = new fileinputstream ("src/1.jpg"); PST. setbinarystream (2, is,is.available());pst.exe cuteupdate ();} catch (exception e) {e. printstacktrace ();} finally {jdbcutil. closeall (Conn, Pst, null) ;}@ testpublic void readerblob () {connection conn = NULL; preparedstatement PST = NULL; resultset rs = NULL; try {conn = jdbcutil. getconn (); string SQL = "Sel ECT * From T2 where id =? "; PST = Conn. preparestatement (SQL); PST. setint (1, 1); RS = pst.exe cutequery (); If (RS. next () {// call the preparestatement method to obtain the response stream. The getbinarystreaminputstream is = Rs. getbinarystream ("content"); outputstream Fos = new fileoutputstream ("D:/2.jpg"); byte [] buff = new byte [1024]; int Len = 0; while (LEN = is. read (buff ))! =-1) {FOS. write (buff, 0, Len);} is. close (); FOS. close () ;}} catch (exception e) {e. printstacktrace ();} finally {jdbcutil. closeall (Conn, Pst, RS );}}}

Package com. itheima. Dao;

Import java. SQL. Connection; import java. SQL. PreparedStatement; import java. SQL. ResultSet; import java. SQL. SQLException; import java. SQL. Statement;

Import javax. transaction. Transaction;

Import org. junit. Test;

Import com. itheima. util. JDBCUtil;

Public class BatchStatement {/*** Statement batch operation: You can add or delete multiple operations and execute them together * @ author simon **/@ Test public void addBatch () {Connection conn = null; Statement pst = null; conn = JDBCUtil. getConn (); try {String sql1 = "insert into t3 values (1, '')"; String sql2 = "insert into t3 values (2,' ')"; string sql3 = "insert into t3 values (3, '')"; String sql4 = "insert into t3 values (4, '')"; String sql5 = "ins Ert into t3 values (5, '') "; String sql6 =" insert into t3 values (6,' ')"; string sql7 = "delete from t3 where id = 4"; pst = conn. createStatement (); pst. addBatch (sql1); pst. addBatch (sql2); pst. addBatch (sql3); pst. addBatch (sql4); pst. addBatch (sql5); pst. addBatch (sql6); pst. addBatch (sql7); int [] is = pst.exe cuteBatch ();} catch (SQLException e) {e. printStackTrace () ;}}/*** PreparedStatement can only be used for one For example, add */@ Test public void batchPreparedStatement () {Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; conn = JDBCUtil. getConn (); try {pst = conn. prepareStatement ("insert into t3 values (?,?) "); For (int I = 0; I <99; I ++) {pst. setInt (1, I); pst. setString (2, "nihao"); pst.exe cuteUpdate () ;}} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {JDBCUtil. closeAll (conn, pst, rs );}}}

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.