日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JDBC的流数据

發(fā)布時間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JDBC的流数据 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

以下內(nèi)容引用自http://wiki.jikexueyuan.com/project/jdbc/streaming-data.html:

PreparedStatement對象必須具備使用輸入和輸出流來提供參數(shù)數(shù)據(jù)的能力。這能夠?qū)⒄麄€文件存儲到數(shù)據(jù)庫列中,這樣數(shù)據(jù)庫就能存儲大型數(shù)據(jù),例如CLOB和BLOB數(shù)據(jù)類型。

用于流數(shù)據(jù)有下列幾種方法:

  • setAsciiStream():該方法是用來提供較大的ASCII值。
  • setCharacterStream():該方法是用來提供較大的UNICODE值。
  • setBinaryStream():該方法是用來提供較大的二進(jìn)制值。

setXXXStream()方法需要一個額外的參數(shù),該參數(shù)是除了參數(shù)占位符的文件大小。這個參數(shù)通知驅(qū)動程序通過使用流有多少數(shù)據(jù)被發(fā)送到數(shù)據(jù)庫中。

示例:

假要上傳一個名為XML_Data.xml的XML文件到數(shù)據(jù)庫的表中。下面是該XML文件的內(nèi)容:

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

將該XML文件和要運(yùn)行的示例保存在相同的目錄的。

這個示例將創(chuàng)建一個數(shù)據(jù)庫表XML_Data,然后XML_Data.xml將被上傳到該表中。

將下面的示例拷貝并粘帖到JDBCExample.java中,編譯并運(yùn)行它,如下所示:

//Import required packages import java.sql.*; import java.io.*; import java.util.*;public class JDBCExample {// JDBC driver name and database URLstatic final String JDBC_DRIVER = "com.mysql.jdbc.Driver";static final String DB_URL = "jdbc:mysql://localhost/Test?serverTimezone=UTC";// Database credentialsstatic 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 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 = new File("XML_Data.xml");long fileLength = f.length();FileInputStream fis = new FileInputStream(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 stream fis.close();// Do a query to get the rowSQL = "SELECT Data FROM XML_Data WHERE id=100";rs = stmt.executeQuery(SQL);// Get the first rowif (rs.next()) {// Retrieve data from input streamInputStream 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 resourcestry {if (stmt != null)stmt.close();} catch (SQLException se2) {} // nothing we can dotry {if (pstmt != null)pstmt.close();} catch (SQLException se2) {} // nothing we can dotry {if (conn != null)conn.close();} catch (SQLException se) {se.printStackTrace();} // end finally try} // end trySystem.out.println("Goodbye!");}// end mainpublic static void createXMLTable(Statement stmt) throws SQLException {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

當(dāng)運(yùn)行JDBCExample時,它將展示下面的結(jié)果:

?

測試工程:https://github.com/easonjim/5_java_example/tree/master/jdbcbasics/test9

==>如有問題,請聯(lián)系我:easonjim#163.com,或者下方發(fā)表評論。<==

總結(jié)

以上是生活随笔為你收集整理的JDBC的流数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。