1.
J
handling of large text types in DBC
large text data types in Oracle,
Clob Long Text type ( not supported in MySQL, using text)
Blob binary Type
MySQL Database,
Text long Text type
Blob binary Type
Requirements: manipulate long text data in JDBC.
Design: Test Table
Coding:
Saving large text data types
Reading large text data types
Saving binary data
Read binary data
Mysql:
1 -- test Big Data types 2 CREATE TABLE Test (3 INT PRIMARY KEY Auto_increment, 4 content Longtext,5 img longblob6 );
Java:
1 Public classApp_text {2 3 //Global Parameters4 PrivateConnection con;5 PrivateStatement stmt;6 PrivatePreparedStatement pstmt;7 PrivateResultSet rs;8 9 Ten @Test One //1. Save large text data type (write Longtext) A Public voidTestsavetext () { -String sql = "INSERT INTO Test (?)"; - Try { the //Connection -Con =jdbcutil.getconnection (); - //pstmt Object -PSTMT =con.preparestatement (SQL); + //Setting Parameters - //get the file path First +String Path = App_text.class. GetResource ("Tips.txt"). GetPath (); AFileReader reader =NewFileReader (NewFile (path)); atPstmt.setcharacterstream (1, reader); - - //Execute SQL - pstmt.executeupdate (); - - //Close in reader.close (); -}Catch(Exception e) { to e.printstacktrace (); +}finally { -Jdbcutil.closeall (Con, pstmt,NULL); the } * } $ Panax Notoginseng @Test - //2. Read the large text data type (read Longtext) the Public voidTestgetastext () { +String sql = "SELECT * from Test;"; A Try { the //Connection +Con =jdbcutil.getconnection (); - //pstmt Object $PSTMT =con.preparestatement (SQL); $ //Read -rs =pstmt.executequery (); - if(Rs.next ()) { the //get long text data, Mode 1: - //Reader r = Rs.getcharacterstream ("content");Wuyi the //get long text data, Mode 2: -System.out.print (rs.getstring ("content")); Wu } - About}Catch(Exception e) { $ e.printstacktrace (); -}finally { -Jdbcutil.closeall (Con, pstmt,NULL); - } A } + } the - $ blob the the Public classApp_blob { the the //Global Parameters - PrivateConnection con; in PrivateStatement stmt; the PrivatePreparedStatement pstmt; the PrivateResultSet rs; About the the @Test the //1. Binary data type (write Longblob) + Public voidTestsavetext () { -String sql = "INSERT INTO Test (IMG) VALUES (?)"; the Try {Bayi //Connection theCon =jdbcutil.getconnection (); the //pstmt Object -PSTMT =con.preparestatement (SQL); - //Get Picture Stream theInputStream in = App_text.class. getResourceAsStream ("7.jpg"); thePstmt.setbinarystream (1, in); the the //Execute Save Picture - Pstmt.execute (); the the //Close the in.close ();94 the}Catch(Exception e) { the e.printstacktrace (); the}finally {98Jdbcutil.closeall (Con, pstmt,NULL); About } - }101 102 @Test103 //2. Read the large text data type (read Longblob)104 Public voidTestgetastext () { theString sql = "Select img from test where id=2;";106 Try {107 //Connection108Con =jdbcutil.getconnection ();109 //pstmt Object thePSTMT =con.preparestatement (SQL);111 //Read thers =pstmt.executequery ();113 if(Rs.next ()) { the //Get Picture Stream theInputStream in = Rs.getbinarystream ("img"); the //Picture output stream117FileOutputStream out =NewFileOutputStream (NewFile ("C://1.jpg"));118 intLen =-1;119 byteB[] =New byte[1024]; - while(len = In.read (b))! =-1) {121Out.write (b, 0, Len);122 }123 //Close124 out.close (); the in.close ();126 }127 -}Catch(Exception e) {129 e.printstacktrace (); the}finally {131Jdbcutil.closeall (Con, pstmt,NULL); the }133 }134}
Processing of large text types in JDBC Java database connection 11)