PreparedStatement接口的setBinaryStream()方法接受一個(gè)表示參數(shù)索引的整數(shù)和一個(gè)InputStream對(duì)象,并將參數(shù)設(shè)置為給定的InputStream對(duì)象。每當(dāng)您需要發(fā)送非常大的二進(jìn)制值時(shí),您都可以使用此方法。
SQL 數(shù)據(jù)庫提供了一種名為 Blob(二進(jìn)制大型對(duì)象)的數(shù)據(jù)類型,您可以在其中存儲(chǔ)大型二進(jìn)制數(shù)據(jù),例如圖像。
使用 JDBC 存儲(chǔ)圖像
如果您需要使用 JDBC 程序?qū)D像存儲(chǔ)在數(shù)據(jù)庫中,請(qǐng)創(chuàng)建一個(gè) Blob 數(shù)據(jù)類型的表,如下所示:
CREATE TABLE Tutorial(Name VARCHAR(255), Type INT NOT NULL, Logo BLOB);
登錄后復(fù)制
現(xiàn)在,使用 JDBC 連接到數(shù)據(jù)庫并準(zhǔn)備一個(gè) PreparedStatement 將值插入到上面創(chuàng)建的表中:
String query = "INSERT INTO Tutorial(Name, Type, Logo) VALUES (?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query);
登錄后復(fù)制
使用PreparedStatement接口的setter方法設(shè)置占位符的值,并使用setBinaryStream()方法設(shè)置Blob數(shù)據(jù)類型的值。
FileInputStream fin = new FileInputStream("javafx_logo.jpg");
pstmt.setBinaryStream(3, fin);
登錄后復(fù)制
示例
以下示例演示如何使用 JDBC 程序?qū)D像插入 MySQL 數(shù)據(jù)庫。在這里,我們創(chuàng)建一個(gè)包含 Blob 數(shù)據(jù)類型的表,將值插入到表中(Blob 類型的 BinaryStream 對(duì)象),并檢索表的內(nèi)容。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class InsertingImageToDatabase {
public static void main(String args[]) throws Exception {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
//Executing the statement
String createTable = "CREATE TABLE Tutorial( "
+ "Name VARCHAR(255), "
+ "Type VARCHAR(50), "
+ "Logo BLOB)";
stmt.execute(createTable);
//Inserting values
String query = "INSERT INTO Tutorial(Name, Type, Logo) VALUES (?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "JavaFX");
pstmt.setString(2, "Java_library");
FileInputStream fin = new FileInputStream("E:\images\javafx_logo.jpg");
pstmt.setBinaryStream(3, fin);
pstmt.execute();
pstmt.setString(1, "CoffeeScript");
pstmt.setString(2, "scripting Language");
fin = new FileInputStream("E:\images\coffeescript_logo.jpg");
pstmt.setBinaryStream(3, fin);
pstmt.execute();
pstmt.setString(1, "Cassandra");
pstmt.setString(2, "NoSQL database");
fin = new FileInputStream("E:\images\cassandra_logo.jpg");
pstmt.setBinaryStream(3, fin);
pstmt.execute();
System.out.println("Data inserted");
ResultSet rs = stmt.executeQuery("Select *from Tutorial");
while(rs.next()) {
System.out.print("Name: "+rs.getString("Name")+", ");
System.out.print("Tutorial Type: "+rs.getString("Type")+", ");
System.out.print("Logo: "+rs.getBlob("Logo"));
System.out.println();
}
}
}
登錄后復(fù)制
輸出
Connection established...... Data inserted Name: JavaFX, Tutorial Type: Java_library, Logo: com.mysql.jdbc.Blob@7dc5e7b4 Name: CoffeeScript, Tutorial Type: scripting Language, Logo: com.mysql.jdbc.Blob@1ee0005 Name: Cassandra, Tutorial Type: NoSQL database, Logo: com.mysql.jdbc.Blob@75a1cd57
登錄后復(fù)制
注意:您只能使用 JDBC 程序存儲(chǔ)和檢索 .gif 或 .jpeg 或 .png 類型的圖像。
以上就是如何使用 JDBC 將圖像插入數(shù)據(jù)庫?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!






