【发布时间】:2016-02-16 21:11:23
【问题描述】:
我想将图像上传到数据库中,但遇到了问题。这是我的代码, Pojo类
public class Pojo {
String fnm;
String lnm;
InputStream b;
public String getFnm() {
return fnm;
}
public void setFnm(String fnm) {
this.fnm = fnm;
}
public String getLnm() {
return lnm;
}
public void setLnm(String lnm) {
this.lnm = lnm;
}
public InputStream getB() {
return b;
}
public void setB(InputStream b) {
this.b = b;
}
}
BL 经理
public class BusinessLogic {
public boolean insert(Pojo bean)
{
boolean check_connection=false;
try
{
System.out.println("inside Registration");
Connection conn= (Connection) DBConnection.connect();
String sql = "INSERT INTO image1 (first_name, last_name, photo) values (?, ?, ?)";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
ps.setString(1,bean.getFnm());
ps.setString(2,bean.getLnm());
ps.setBlob(3, bean.getB());
ps.executeUpdate();
check_connection =true;
conn.close();
}
catch(Exception e)
{
System.out.println(""+e);
}
return check_connection;
}
}
Servlet 文件
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
InputStream inputStream = null; // input stream of the upload file
Part filePart = request.getPart("photo");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
Pojo p=new Pojo();
p.setFnm(firstName);
p.setLnm(lastName);
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
p.setB(inputStream);
// statement.setBlob(3, inputStream);
}
BusinessLogic bl=new BusinessLogic();
boolean b=bl.insert(p);
if(b)
{
System.out.println("success");
}
else {
System.out.println("success");
}
}
数据库连接文件...
public class DBConnection {
public static Connection con;
public static Connection connect()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/img","root","");
System.out.println("connected!!");
}
catch(Exception e)
{
System.out.println(e);
}
return con;
}
public static void close() throws SQLException
{
if(con!=null)
{
con.close();
}
}
}
当我尝试将图像插入数据库时,出现以下错误:
java.lang.AbstractMethodError: 方法 com/mysql/jdbc/PreparedStatement.setBlob(ILjava/io/InputStream;)V 是 摘要于 com.mysql.jdbc.PreparedStatement.setBlob(PreparedStatement.java) 在 com.image.model.BusinessLogic.insert(BusinessLogic.java:22)
请帮我解决这个问题...
【问题讨论】: