Java向MySQL添加图片

public void insertHeadIcon() {
//图片地址
String path = “D:\Workspace\project\icon\100.jpg”;
File file = new File(path);
FileInputStream in = null;
Connection conn = null;
PreparedStatement pstat = null;
try {
//将file转为输入流
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
conn = DButils.getConnection();
String sql = “insert into userinfo (id,name,pass,state,icon,flag)values(?,?,?,?,?,?)”;
pstat = conn.prepareStatement(sql);
pstat.setInt(1, 1);
pstat.setString(2, “张三”);
pstat.setString(3, “123”);
pstat.setInt(4, “普通用户”);
pstat.setBinaryStream(5, in, in.available());
pstat.setInt(6, 1);
int i = pstat.executeUpdate();
System.out.println(i);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
DButils.close(null, pstat, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

你可能感兴趣的:(java,mysql,数据库)