使用Java在数据库中插入图像
我需要插入一个带有Blob列的行,Blob列是从本地硬盘中的URL获取的图像。我的尝试是:使用Java在数据库中插入图像,java,mysql,Java,Mysql,我需要插入一个带有Blob列的行,Blob列是从本地硬盘中的URL获取的图像。我的尝试是: File file = new File(imageURL); FileInputStream input; byte[] data = null; try { input = new FileInputStream(file); data = new byte[input.available()]; input.close(); } catch (Exception e) {
File file = new File(imageURL);
FileInputStream input;
byte[] data = null;
try {
input = new FileInputStream(file);
data = new byte[input.available()];
input.close();
} catch (Exception e) {
e.printStackTrace();
}
然后我构建我的图像对象:Imagen ima=newimagen(0,data,newdate());我把它交给另一个经理。我将字节[]转换为BLOB对象,如下所示:
byte[] datos = image.getDatos();
Blob blob = null;
try {
blob = conection.createBlob();
blob.setBytes(1,datos);
} catch (SQLException e) {
e.printStackTrace();
}
java.sql.Date sqlDate = new java.sql.Date(image.getFecha().getTime());
String query = " INSERT INTO imagen VALUES('" + image.getId() + "','"
+ blob + "','" + sqlDate + "') ";
int lastID = BBDD.add(query, conection);
我可以毫无问题地运行它,但我只能得到这样的“com.mysql.jdbc”。Blob@1e1962d“我的MySQL没有显示我的任何其他内容
有人能帮我吗?。我使用SQLyog来查看它
谢谢 SQL工具只知道文件中使用的类型。blob或类似的二进制字段不为SQL工具所理解,只是在没有修改的情况下传递。您需要将此二进制数据读入另一个能够理解数据含义的工具。SQL工具只知道文件中使用的类型。blob或类似的二进制字段不为SQL工具所理解,只是在没有修改的情况下传递。您需要将此二进制数据读入另一个了解数据含义的工具。好的,您可以使用准备好的语句:
java.sql.PreparedStatement ps =
connection.prepareStatement("UPDATE table SET file = ?");
ps.setBinaryStream(1, new FileInputStream(file), (int)file.length());
更新
将调用Blob
s defaulttoString()
,这就是为什么您会看到如此奇怪的输出,唯一的方法是使用PreparedStatements,如果我正在编写任何人,请纠正我
public void insert() throws Exception {
Connection conn = null;
PreparedStatement ps = null;
InputStream is = null;
try {
conn = this.connection.getConnection();
String sql = "INSERT INTO Table (image) VALUES (?)";
ps = conn.prepareStatement(sql);
if (this.file != null && this.file.canRead()) {
is = new BufferedInputStream(new FileInputStream(this.file));
ps.setBinaryStream(1, is, (int) this.file.length());
} else {
ps.setNull(1, Types.BLOB);
}
} catch (Exception e) {
LOG.error("", e);
throw e;
} finally {
FileUtil.close(is);
DAOUtil.close(conn);
}
}
如果您有一个连接对象,我建议您这次可以跳过BBDD。好的,您可以使用准备好的语句:
java.sql.PreparedStatement ps =
connection.prepareStatement("UPDATE table SET file = ?");
ps.setBinaryStream(1, new FileInputStream(file), (int)file.length());
package com.technicalkeeda;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertImageTest {
/**
* This is used to get the Connection
*
* @return
*/
public Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/technicalkeeda", "root", "");
} catch (Exception e) {
System.out.println("Error Occured While Getting the Connection: - "
+ e);
}
return connection;
}
/**
* Insert Image
*/
public void insertImage() {
Connection connection = null;
PreparedStatement statement = null;
FileInputStream inputStream = null;
try {
File image = new File("C:/honda.jpg");
inputStream = new FileInputStream(image);
connection = getConnection();
statement = connection
.prepareStatement("insert into trn_imgs(img_title, img_data) "
+ "values(?,?)");
statement.setString(1, "Honda Car");
statement.setBinaryStream(2, (InputStream) inputStream,
(int) (image.length()));
statement.executeUpdate();
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException: - " + e);
} catch (SQLException e) {
System.out.println("SQLException: - " + e);
} finally {
try {
connection.close();
statement.close();
} catch (SQLException e) {
System.out.println("SQLException Finally: - " + e);
}
}
}
/***
* Execute Program
*
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
InsertImageTest imageTest = new InsertImageTest();
imageTest.insertImage();
}
}
更新
将调用Blob
s defaulttoString()
,这就是为什么您会看到如此奇怪的输出,唯一的方法是使用PreparedStatements,如果我正在编写任何人,请纠正我
public void insert() throws Exception {
Connection conn = null;
PreparedStatement ps = null;
InputStream is = null;
try {
conn = this.connection.getConnection();
String sql = "INSERT INTO Table (image) VALUES (?)";
ps = conn.prepareStatement(sql);
if (this.file != null && this.file.canRead()) {
is = new BufferedInputStream(new FileInputStream(this.file));
ps.setBinaryStream(1, is, (int) this.file.length());
} else {
ps.setNull(1, Types.BLOB);
}
} catch (Exception e) {
LOG.error("", e);
throw e;
} finally {
FileUtil.close(is);
DAOUtil.close(conn);
}
}
你有一个连接对象,这次我建议你可以跳过BBDD。实际上,我有一个名为Image的Javabean,带有id、数据和日期。我以上面的方式构建对象,然后通过该对象获取信息,将其插入我的BBDD中。所以你的意思是,在进行查询之前,我应该使用其他API来保存好我的图像,不是吗?不,我的意思是你需要将查询中的数据传递到另一个API。实际上,我有一个名为image的Java Bean,带有id、数据和日期。我以上面的方式构建对象,然后通过该对象获取信息,将其插入我的BBDD中。所以你的意思是,在进行查询之前,我应该使用其他API来保存好我的图像,不是吗?不,我的意思是你需要将查询中的数据传递给另一个APIC,你可以向我们展示你用来插入数据库的代码吗?我不能使用PrepareStatement,因为我执行查询的方式。使用just语句有什么办法吗?。非常感谢你!!说真的,为将来的用户稍微整理一下这个答案。如果你不这样做,它很可能会被否决或被标记。你能告诉我们为什么不能使用PreparedStatement吗?您的查询看起来如何?BBDD是公共API还是自主开发的?它应该有一个支持PreparedStatement的界面。你能告诉我们你用来插入数据库的代码吗?我不能使用PreparedStatement,因为我的查询方式。使用just语句有什么办法吗?。非常感谢你!!说真的,为将来的用户稍微整理一下这个答案。如果你不这样做,它很可能会被否决或被标记。你能告诉我们为什么不能使用PreparedStatement吗?您的查询看起来如何?BBDD是公共API还是自主开发的?它应该有一个支持preparedstatements的接口。您还有另一个问题:
InputStream.available()
不会告诉您输入流包含多少字节。您甚至没有从代码中的输入流中读取数据(为此,您必须调用read()
方法),您还有另一个问题:InputStream.available()
不会告诉您输入流包含多少字节。您甚至没有从代码中的输入流中读取数据(为此,您必须调用read()
方法)
package com.technicalkeeda;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertImageTest {
/**
* This is used to get the Connection
*
* @return
*/
public Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/technicalkeeda", "root", "");
} catch (Exception e) {
System.out.println("Error Occured While Getting the Connection: - "
+ e);
}
return connection;
}
/**
* Insert Image
*/
public void insertImage() {
Connection connection = null;
PreparedStatement statement = null;
FileInputStream inputStream = null;
try {
File image = new File("C:/honda.jpg");
inputStream = new FileInputStream(image);
connection = getConnection();
statement = connection
.prepareStatement("insert into trn_imgs(img_title, img_data) "
+ "values(?,?)");
statement.setString(1, "Honda Car");
statement.setBinaryStream(2, (InputStream) inputStream,
(int) (image.length()));
statement.executeUpdate();
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException: - " + e);
} catch (SQLException e) {
System.out.println("SQLException: - " + e);
} finally {
try {
connection.close();
statement.close();
} catch (SQLException e) {
System.out.println("SQLException Finally: - " + e);
}
}
}
/***
* Execute Program
*
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
InsertImageTest imageTest = new InsertImageTest();
imageTest.insertImage();
}
}