您的位置:首页 > 数据库

数据库图片导入

2015-11-04 11:07 411 查看
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.PreparedStatement;

public class Input {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
System.out.println("数据库链接成功");
String sql="insert into user value (?,?,?,?)";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
ps.setInt(1, 1);
ps.setString(2, "张三");
ps.setString(3, "123456");

File file=new File("D:/photo/1.jpg");//1
FileInputStream fis=new FileInputStream(file);//2
ps.setBinaryStream(4, fis, (int)file.length());
ps.executeUpdate();

System.out.println("插入数据成功");
if (conn != null) {
ps.close();
fis.close();
conn.close();
System.out.println("链接关闭成功");
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: