java 如何在netbeans java中将图像插入mysql数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35410171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert image to mysql database in netbeans java
提问by Ishani
I have poor knowledge about programming. I need to save image in MySQL database. I have created a database table and there is a column to add image with longblob data type. I have code to a button to choose image from folder in PC then it load to a jlable. Now I need to insert this image to a database.
我对编程知之甚少。我需要将图像保存在 MySQL 数据库中。我创建了一个数据库表,并且有一列用于添加具有 longblob 数据类型的图像。我有一个按钮的代码,可以从 PC 的文件夹中选择图像,然后将其加载到 jlable。现在我需要将此图像插入到数据库中。
This is my code;
这是我的代码;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc=new JFileChooser();
fc.showOpenDialog(this);
File f=fc.getSelectedFile();
String path=f.getAbsolutePath();
jLabel1.setIcon(new ImageIcon(path));
try{
FileInputStream fin=new FileInputStream(f);
int len=(int)f.length(); Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:my-sql://localhost/hss", "root", "bis123");
PreparedStatement ps=con.prepareStatement("Insert into profile values(?)");
ps.setBinaryStream(1, fin, len);
int status=ps.executeUpdate();
if(status > 0) {
jLabel2.setText("Successfully inserted in DB");
}else{
jLabel2.setText("Image not inserted!");
}
}catch(Exception e){
System.out.println(e);
}
}
回答by SmashCode
In MySQL when we use the blob type to store the data , it support only 5 kb image capacity.
在 MySQL 中,当我们使用 blob 类型存储数据时,它仅支持 5 kb 的图像容量。
CREATE TABLE image
(
创建表image
(
id
varchar(45) DEFAULT NULL,
id
varchar(45) 默认为空,
size
int(11) DEFAULT NULL,
size
int(11) 默认为空,
image
longblob
image
长斑点
);
);
created database table in above code
在上面的代码中创建了数据库表
this is java code to insert image in database
这是在数据库中插入图像的java代码
import java.sql.*;
import java.io.*;
public class InsertImagesMysql{
public static void main(String[] args){
System.out.println("Insert Image Example!");
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String userName = "root";
String password = "root";
Connection con = null;
try{
Class.forName(driverName);
con = DriverManager.getConnection(url+dbName,userName,password);
Statement st = con.createStatement();
File imgfile = new File("pic.jpg");
FileInputStream fin = new FileInputStream(imgfile);
PreparedStatement pre =
con.prepareStatement("insert into Image values(?,?,?)");
pre.setString(1,"test");
pre.setInt(2,3);
pre.setBinaryStream(3,(InputStream)fin,(int)imgfile.length());
pre.executeUpdate();
System.out.println("Successfully inserted the file into the database!");
pre.close();
con.close();
}catch (Exception e1){
System.out.println(e1.getMessage());
}
}
}
here is code to retrieve data from database
这是从数据库中检索数据的代码
import java.io.*;
import java.sql.*;
public class RetriveImagesMysql{
public static void main(String[] args){
System.out.println("Retrive Image Example!");
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String userName = "root";
String password = "root";
Connection con = null;
try{
Class.forName(driverName);
con = DriverManager.getConnection(url+dbName,userName,password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select image from image");
int i = 0;
while (rs.next()) {
InputStream in = rs.getBinaryStream(1);
OutputStream f = new FileOutputStream(new File("test"+i+".jpg"));
i++;
int c = 0;
while ((c = in.read()) > -1) {
f.write(c);
}
f.close();
in.close();
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
here just assign fin to jbutton action event it will trigger automatically the run of the code
这里只需将 fin 分配给 jbutton 动作事件它会自动触发代码的运行