PreparedStatement 對象能夠使用輸入和輸出流提供參數(shù)數(shù)據(jù)。這使您能夠?qū)⒄麄€文件放置到可以保存大值(如 CLOB 和 BLOB 數(shù)據(jù)類型)的數(shù)據(jù)庫列中。
有以下方法,可用于流數(shù)據(jù)-
setAsciiStream(): 此方法用于提供較大的ASCII值。
setCharacterStream(): 此方法用于提供較大的UNICODE值。
setBinaryStream(): 此方法用于提供較大的二進(jìn)制值。
setXXXStream ()方法除了參數(shù)占位符之外,還需要一個額外的參數(shù),即文件大小。此參數(shù)告知驅(qū)動程序應(yīng)該使用流向數(shù)據(jù)庫發(fā)送多少數(shù)據(jù)。
例如我們想將XML文件XML_Data.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文件放在要運行此示例的目錄中。
本示例將創(chuàng)建一個數(shù)據(jù)庫表XML_Data,然后將文件XML_Data.xml上載到該表中。
復(fù)制并粘貼以下示例到JDBCExample.java中,如下編譯并運行:
// 導(dǎo)入所需的軟件包
import java.sql.*;
import java.io.*;
import java.util.*;
public class JDBCExample {
// JDBC驅(qū)動程序名稱和數(shù)據(jù)庫URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
// 數(shù)據(jù)庫憑證
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rs = null;
try{
// 注冊JDBC驅(qū)動程序
Class.forName("com.mysql.jdbc.Driver");
// 打開連接
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//創(chuàng)建一個Statement對象并構(gòu)建表
stmt = conn.createStatement();
createXMLTable(stmt);
//打開一個FileInputStream
File f = new File("XML_Data.xml");
long fileLength = f.length();
FileInputStream fis = new FileInputStream(f);
//創(chuàng)建PreparedStatement并流式傳輸數(shù)據(jù)
String SQL = "INSERT INTO XML_Data VALUES (?,?)";
pstmt = conn.prepareStatement(SQL);
pstmt.setInt(1,100);
pstmt.setAsciiStream(2,fis,(int)fileLength);
pstmt.execute();
//關(guān)閉輸入流
fis.close();
// 進(jìn)行查詢以獲取行
SQL = "SELECT Data FROM XML_Data WHERE id=100";
rs =stmt.executeQuery(SQL);
// 獲取第一行
if (rs.next ()){
//從輸入流中檢索數(shù)據(jù)
InputStream xmlInputStream =rs.getAsciiStream(1);
int c;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while (( c =xmlInputStream.read()) != -1)
bos.write(c);
//打印結(jié)果
System.out.println(bos.toString());
}
// 清理環(huán)境
rs.close();
stmt.close();
pstmt.close();
conn.close();
}catch(SQLException se){
//處理JDBC錯誤
se.printStackTrace();
}catch(Exception e){
//處理Class.forName的錯誤
e.printStackTrace();
}finally{
//用于關(guān)閉資源
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// 我們無能為力
try{
if(pstmt!=null)
pstmt.close();
}catch(SQLException se2){
}// 我們無能為力
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}//結(jié)束try
System.out.println("Goodbye!");
}//結(jié)束main
public static void createXMLTable(Statement stmt)
throws SQLException{
System.out.println("Creating XML_Data table..." );
//創(chuàng)建SQL語句
String streamingDataSql = "CREATE TABLE XML_Data " +
"(id INTEGER, Data LONG)";
//如果存在,請首先刪除表。
try{
stmt.executeUpdate("DROP TABLE XML_Data");
}catch(SQLException se){
}// 沒做什么
//建立表。
stmt.executeUpdate(streamingDataSql);
}//結(jié)束createXMLTable
}//結(jié)束JDBCExample現(xiàn)在讓我們編譯上面的示例,如下所示:
C:\>javac JDBCExample.java C:\>
運行時JDBCExample,它將產(chǎn)生以下結(jié)果-
C:\>java JDBCExample Connecting to database... Creating XML_Data table... <?xml version="1.0"?> <Employee> <id>100</id> <first>Zara</first> <last>Ali</last> <Salary>10000</Salary> <Dob>18-08-1978</Dob> <Employee> Goodbye! C:\>