Get the App
SLTechnology News&Howtos  ›  Servers  › 

Code for simple implementation of java large object access

Shulou Source: shulou.com Published: 2022-06-03 07:55:31 09月28日 Update

To treasure some pieces of content that are often used in the development process, the following information is about the simple implementation of java large object access.

Package info.itlanguageexpress.LargeObject

Import java.io.File

Import java.io.FileInputStream

Import java.io.FileOutputStream

Import java.io.IOException

Import java.io.InputStream

Import java.sql.Connection

Import java.sql.DriverManager

Import java.sql.PreparedStatement

Import java.sql.ResultSet

Import java.sql.SQLException

Public class MySqlLargeObjectAccess {

Public static String DRIVER = "org.gjt.mm.mysql.Driver"; public static String USER = "root"; public static String PASSWORD = "your password"; public static void main (String [] args) throws Exception {Connection conn = null; try {Class.forName (DRIVER); conn = DriverManager.getConnection (URL, USER, PASSWORD); insertWordFileToMySql (conn); fetchWordFileFromMySql (conn);} catch (Exception ex) {ex.printStackTrace (System.out) } finally {try {conn.close ();} catch (Exception ex) {}} public static void insertWordFileToMySql (Connection conn) throws SQLException, IOException {PreparedStatement pstm = conn .prepareStatement ("insert into wordInputTest values (?)"; pstm.setString (1, "docx1"); InputStream in = new FileInputStream ("F:" + File.separator + "test1.docx") Pstm.setBinaryStream (2, in, in.available ()); pstm.executeUpdate (); pstm.close ();} public static void fetchWordFileFromMySql (Connection conn) throws SQLException, IOException {PreparedStatement pstm = conn pstm.setString (1, "docx1"); ResultSet rs = pstm.executeQuery (); int count = 1; while (rs.next ()) {InputStream in = rs.getBinaryStream ("file") FileOutputStream out = new FileOutputStream ("F:" + File.separator + "testcopy" + count + ".docx"); byte [] b = new byte [1024]; int len = 0; while ((len = in.read (b))! =-1) {out.write (b, 0, len); out.flush ();} out.close () In.close (); count++;} rs.close (); pstm.close ();}

}

Tags: Content objects access common use fragments data procedures development code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi Huawei Shulou Technology Shulou Information