JDBC Learning Note: Working with JDBC for large pieces of data

Source: Internet
Author: User

1. Database--Create a data table

1 Create Table   clob_test2(3integernotnullPrimary  key,4textnotnull5 );

2. Working with JDBC for large pieces of text data

(1) Writing text data to the database

1 @Test2      Public voidCreate ()throwsSQLException, IOException {3Connection conn =NULL;4PreparedStatement PS =NULL;5ResultSet rs =NULL;6         Try {7             //Establish a connection8conn =jdbcutils.getconnection ();9String sql = "INSERT into Clob_test (Big_text) VALUES (?)";TenPS =conn.preparestatement (SQL); OneFile File =NewFile ("Src/cn/itcast/jdbc/jdbcutils.java"); AReader reader =NewBufferedReader (Newfilereader (file)); -Ps.setcharacterstream (1, Reader, (int) file.length ()); -              the             inti =ps.executeupdate (); - reader.close (); -System.out.println ("i =" +i); -}finally { + Jdbcutils.free (RS, PS, conn); -         } +}

(2) Read the text data from the database

1 @Test2      Public voidRead ()throwsSQLException, IOException {3Connection conn =NULL;4Statement st =NULL;5ResultSet rs =NULL; 6         Try {7conn =jdbcutils.getconnection ();8St =conn.createstatement ();9rs = St.executequery ("Select Big_text from Clob_test");Ten              One              while(Rs.next ()) { AClob Clob = Rs.getclob (1); -Reader reader =Clob.getcharacterstream (); -                 //reader = Rs.getcharacterstream (1) the                  -                 //writes large pieces of text data read from the database to the Jdbcutils_bak.java file -File File =NewFile ("Jdbcutils_bak.java"); -Writer writer =NewBufferedWriter (NewFileWriter (file)); +                 Char[] Buff =New Char[1024]; -                  +                  for(inti = 0; (I=reader.read (buff)) > 0;) { AWriter.write (Buff, 0, i); at                 } - writer.close (); - reader.close (); -             } -}finally { - Jdbcutils.free (RS, St, conn); in         } -}

(3) Writing binary type data to the database

1 @Test2      Public voidCreate ()throwsSQLException, IOException {3Connection conn =NULL;4PreparedStatement PS =NULL;5ResultSet rs =NULL;6         Try {7conn =jdbcutils.getconnection ();8String sql = "INSERT into Blob_test (big_bit) VALUES (?)";9PS =conn.preparestatement (SQL);TenFile File =NewFile ("14.jpg");//file in root directory, size cannot exceed 64k OneInputStream in =NewBufferedinputstream (Newfileinputstream (file)); A              -Ps.setbinarystream (1, IN, (int) file.length ()); -              the             inti =ps.executeupdate (); - in.close (); -System.out.println ("i =" +i); -}finally { + Jdbcutils.free (RS, PS, conn); -         } +}

(4) Reading binary type data from a database

1 @Test2      Public voidRead ()throwsSQLException, IOException {3Connection conn =NULL;4Statement st =NULL;5ResultSet rs =NULL;6         Try {7conn =jdbcutils.getconnection ();8St =conn.createstatement ();9rs = St.executequery ("Select Big_bit from Blob_test");Ten              One              while(Rs.next ()) { AInputStream in = Rs.getbinarystream (1); -                  -File File =NewFile ("14_bak.jpg"); theOutputStream out =NewBufferedoutputstream (Newfileoutputstream (file)); -                 byte[] Buff =New byte[1024]; -                  for(inti = 0; (i = in.read (buff)) > 0;) { -Out.write (Buff, 0, i); +                 } - out.close (); + in.close (); A             } at}finally { - Jdbcutils.free (RS, St, conn); -         } -}

@Test
public void Read () throws SQLException, IOException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = Jdbcutils.getconnection ();
st = Conn.createstatement ();
rs = St.executequery ("Select Big_bit from Blob_test");

while (Rs.next ()) {
InputStream in = Rs.getbinarystream (1);

File File = new file ("14_bak.jpg");
OutputStream out = new Bufferedoutputstream (new FileOutputStream (file));
byte[] buff = new byte[1024];
for (int i = 0; (i = in.read (buff)) > 0;) {
Out.write (buff, 0, i);
}
Out.close ();
In.close ();
}
} finally {
Jdbcutils.free (RS, St, conn);
}
}

JDBC Learning Note: Working with JDBC for large pieces of data

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.