public void insertDB() throws FileNotFoundException,ClassNotFoundException,
IllegalAccessException, InstantiationException, SQLException {
String filePathFrom = "D://my.jpg" ;
File file = new File(filePathFrom);
FileInputStream fis = new FileInputStream(file);
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/test?user=root&password=";
Connection conn = DriverManager.getConnection(url);
String sql = "insert into user(id,image) values(?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1,88);
stmt.setBinaryStream(2,fis,(int)file.length());
stmt.executeUpdate();
conn.close();
System.out.println("ok0");
}
IllegalAccessException, InstantiationException, SQLException {
String filePathFrom = "D://my.jpg" ;
File file = new File(filePathFrom);
FileInputStream fis = new FileInputStream(file);
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/test?user=root&password=";
Connection conn = DriverManager.getConnection(url);
String sql = "insert into user(id,image) values(?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1,88);
stmt.setBinaryStream(2,fis,(int)file.length());
stmt.executeUpdate();
conn.close();
System.out.println("ok0");
}
本文介绍了一个使用Java将本地文件系统中的图片插入到MySQL数据库的方法。具体步骤包括:加载数据库驱动、建立连接、准备SQL语句并设置参数,最终执行插入操作。
1415

被折叠的 条评论
为什么被折叠?



