JDBC的流数据

以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/streaming-data.html

PreparedStatement对象必须具备使用输入和输出流来提供参数数据的能力。这能够将整个文件存储到数据库列中,这样数据库就能存储大型数据,例如CLOB和BLOB数据类型。

用于流数据有下列几种方法:

  • setAsciiStream():该方法是用来提供较大的ASCII值。
  • setCharacterStream():该方法是用来提供较大的UNICODE值。
  • setBinaryStream():该方法是用来提供较大的二进制值。

setXXXStream()方法需要一个额外的参数,该参数是除了参数占位符的文件大小。这个参数通知驱动程序通过使用流有多少数据被发送到数据库中。

示例:

假要上传一个名为XML_Data.xml的XML文件到数据库的表中。下面是该XML文件的内容:

<?xml version="1.0"?>
<Employee>
<id>100</id>
<first>Zara</first>
<last>Ali</last>
<Salary>10000</Salary>
<Dob>18-08-1978</Dob>
<Employee>

将该XML文件和要运行的示例保存在相同的目录的。

这个示例将创建一个数据库表XML_Data,然后XML_Data.xml将被上传到该表中。

将下面的示例拷贝并粘帖到JDBCExample.java中,编译并运行它,如下所示:

//Import required packages
import java.sql.*;
import java.io.*;
import java.util.*;

public class JDBCExample {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/Test?serverTimezone=UTC";

    // Database credentials
    static final String USER = "root";
    static final String PASS = "root";

    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");

            // Open a connection
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // Create a Statement object and build table
            stmt = conn.createStatement();
            createXMLTable(stmt);

            // Open a FileInputStream
            File f = new File("XML_Data.xml");
            long fileLength = f.length();
            FileInputStream fis = new FileInputStream(f);

            // Create PreparedStatement and stream data
            String SQL = "INSERT INTO XML_Data VALUES (?,?)";
            pstmt = conn.prepareStatement(SQL);
            pstmt.setInt(1, 100);
            pstmt.setAsciiStream(2, fis, (int) fileLength);
            pstmt.execute();

            // Close input stream
            fis.close();

            // Do a query to get the row
            SQL = "SELECT Data FROM XML_Data WHERE id=100";
            rs = stmt.executeQuery(SQL);
            // Get the first row
            if (rs.next()) {
                // Retrieve data from input stream
                InputStream xmlInputStream = rs.getAsciiStream(1);
                int c;
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                while ((c = xmlInputStream.read()) != -1)
                    bos.write(c);
                // Print results
                System.out.println(bos.toString());
            }
            // Clean-up environment
            rs.close();
            stmt.close();
            pstmt.close();
            conn.close();
        } catch (SQLException se) {
            // Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            // Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            // finally block used to close resources
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
            } // nothing we can do
            try {
                if (pstmt != null)
                    pstmt.close();
            } catch (SQLException se2) {
            } // nothing we can do
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            } // end finally try
        } // end try
        System.out.println("Goodbye!");
    }// end main

    public static void createXMLTable(Statement stmt) throws SQLException {
        System.out.println("Creating XML_Data table...");
        // Create SQL Statement
        String 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

当运行JDBCExample时,它将展示下面的结果:

测试工程:https://github.com/easonjim/5_java_example/tree/master/jdbcbasics/test9

原文地址:https://www.cnblogs.com/EasonJim/p/6998111.html