The following is referenced from http://wiki.jikexueyuan.com/project/jdbc/streaming-data.html:
The PreparedStatement object must have the ability to use input and output streams to provide parameter data. This allows the entire file to be stored in a database column so that the database can store large data such as CLOB and BLOB data types.
There are several ways to stream data:
- Setasciistream (): This method is used to provide a large ASCII value.
- Setcharacterstream (): This method is used to provide a large Unicode value.
- Setbinarystream (): This method is used to provide a large binary value.
The Setxxxstream () method requires an extra parameter, which is the file size except for the parameter placeholder. This parameter notifies the driver how much data is sent to the database by using the stream.
Example:
False to upload an XML file named Xml_data.xml to a table in the database. The following is the contents of the XML file:
<?XML version= "1.0"?><Employee><ID>100</ID>< First>Zara</ First>< Last>Ali</ Last><Salary>10000</Salary><Dob>18-08-1978</Dob><Employee>
Save the XML file and the example that you want to run in the same directory.
This example creates a database table Xml_data, and then Xml_data.xml will be uploaded to the table.
Copy and paste the following example into Jdbcexample.java, and compile and run it as follows:
//Import Required PackagesImportJava.sql.*;ImportJava.io.*;ImportJava.util.*; Public classJdbcexample {//JDBC driver name and database URL Static FinalString jdbc_driver = "Com.mysql.jdbc.Driver"; Static FinalString Db_url = "JDBC:MYSQL://LOCALHOST/TEST?SERVERTIMEZONE=UTC"; //Database Credentials Static FinalString USER = "root"; Static FinalString PASS = "root"; Public Static voidMain (string[] args) {Connection conn=NULL; PreparedStatement pstmt=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try { //Register JDBC DriverClass.forName ("Com.mysql.jdbc.Driver"); //Open a connectionSYSTEM.OUT.PRINTLN ("Connecting to Database ..."); Conn=drivermanager.getconnection (Db_url, USER, PASS); //Create a Statement object and build tablestmt =conn.createstatement (); Createxmltable (stmt); //Open a FileInputStreamFile f =NewFile ("Xml_data.xml"); LongFilelength =f.length (); FileInputStream FIS=NewFileInputStream (f); //Create PreparedStatement and stream dataString SQL = "INSERT into Xml_data VALUES (?,?)"; Pstmt=conn.preparestatement (SQL); Pstmt.setint (1, 100); Pstmt.setasciistream (2, FIS, (int) filelength); Pstmt.execute (); //Close Input StreamFis.close (); //Do a query to get the rowSQL = "Select Data from Xml_data WHERE id=100"; RS=stmt.executequery (SQL); //Get the first row if(Rs.next ()) {//Retrieve data from input streamInputStream Xmlinputstream = Rs.getasciistream (1); intC; Bytearrayoutputstream Bos=NewBytearrayoutputstream (); while((c = Xmlinputstream.read ())! = 1) Bos.write (c); //Print ResultsSystem.out.println (bos.tostring ()); } //clean-up EnvironmentRs.close (); Stmt.close (); Pstmt.close (); Conn.close (); } Catch(SQLException se) {//Handle errors for JDBCSe.printstacktrace (); } Catch(Exception e) {//Handle errors for Class.forNameE.printstacktrace (); } finally { //finally block used to close resources Try { if(stmt! =NULL) Stmt.close (); } Catch(SQLException se2) {}//we can do Try { if(Pstmt! =NULL) Pstmt.close (); } Catch(SQLException se2) {}//we can do Try { if(Conn! =NULL) Conn.close (); } Catch(SQLException se) {se.printstacktrace (); } //End finally try}//End TrySystem.out.println ("goodbye!"); }//End Main Public Static voidCreatexmltable (Statement stmt)throwsSQLException {System.out.println ("Creating xml_data Table ..."); //Create SQL StatementString streamingdatasql = "CREATE TABLE xml_data" + "(ID INTEGER, Data LONG)"; //Drop Table First if it exists. Try{stmt.executeupdate ("DROP TABLE Xml_data"); } Catch(SQLException se) {}// do nothing//Build table.stmt.executeupdate (Streamingdatasql); }//End Createxmltable}//End Jdbcexample
When you run Jdbcexample, it will show the following results:
Test Project: Https://github.com/easonjim/5_java_example/tree/master/jdbcbasics/test9
Streaming data for JDBC