请教mysql插入图片的问题(通过表单提交到servlet,然后将图片插入mysql)
接受数据的页面如下:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>报名</title>
<meta http-equiv="content-type" content="text/html; charset=gbk">
</head>
<body>
<h1 align="center">Super Girl 报名</h1>
<form method="post" action="/supergirl/apply.do">
<table>
<tr>
<td>姓名:</td><td><input type="text" name="sname" size="20"></td>
<td>出生地:</td> <td><input type="text" name="birthplace" size="40"></td>
</tr>
<tr>
<td>出生年月(如1983-12-31):</td><td><input type="text" name="birthday" size="20"></td>
<td>毕业院校:</td><td><input type="text" name="school" size="40"></td>
</tr>
<tr>
<td>经典曲目:</td><td><input type="text" name="music" size="20"></td>
<td>特长:</td><td><input type="text" name="speciality" size="40"></td>
</tr>
<tr>
<td>照片</td><td><input type="file" name="photo"></td>
</tr>
</table>
<center><input type="submit" value="提交"></center>
</form>
</body>
</html>
servlet代码如下:
public class InDatabase extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
Connection conn = (Connection)getServletContext().getAttribute("conn");
String sql = "insert into supergirls (name,birthplace,birthday,school,music,speciality,photo)" +
"values (?,?,?,?,?,?,?)";
PreparedStatement stmt;
try {
stmt = conn.prepareStatement(sql) ;
stmt.setString(1,request.getParameter("sname"));
stmt.setString(2,request.getParameter("birthplace"));
String birth = request.getParameter("birthday");
stmt.setDate(3,Date.valueOf(birth));
stmt.setString(4,request.getParameter("school"));
stmt.setString(5,request.getParameter("music"));
stmt.setString(6,request.getParameter("speciality"));
String data = request.getParameter("photo");
String url =data.replace("\\","\\\\");
File file = new File(url);
InputStream in = new FileInputStream(file);
stmt.setBinaryStream(7, in,in.available());
stmt.executeUpdate();
System.out.println("update");
stmt.clearParameters();
stmt.close();
in.close();
} catch (SQLException e) {
e.printStackTrace();
}
RequestDispatcher dis = request.getRequestDispatcher("/already");
dis.forward(request, response);
}
}
运行后的错误显示为:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?U”–?^Nm?¨í?k?ùqàòt_????0?…?á??&\'#f§Q·:????????T?xA???òz?Y?Kw
?????H%@±.?' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
at action.GirlInDatabase.doPost(InDatabase.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
我使用的是:windows xp + Tomcat6.X + mysql5.1
先在此谢谢各位!望高人相助!