日期:2014-05-19  浏览次数:20654 次

JSP向数据库插入数据的问题
用DAO模式时

package zeng.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import zeng.vo.Member;
import zeng.dao.MemberDAO;
public class MemberDAOImpl implements MemberDAO{//通过Impl完成对数据库的操作
private Connection conn=null;
private PreparedStatement pstmt=null;
public MemberDAOImpl(Connection conn){//构造方法
this.conn=conn;
}
public boolean findLogin(Member member)throws Exception{
boolean flag=false;
try{
String sql="SELECT name FROM member WHERE mid=? and password=?";
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1, member.getMid());
this.pstmt.setString(2, member.getPassword());
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
member.setName(rs.getString(1));//取得用户名
flag=true;
}
}catch(Exception e){
throw e;
}finally{//记得关闭
if(this.conn!=null){
try{
this.conn.close();
}catch(Exception ee){
throw ee;
}
}
if(this.pstmt!=null){
try{
this.pstmt.close();
}catch(Exception e){
throw e;
}
}
}
//System.out.println(flag);
return flag;
}
public boolean addMember(Member member)throws Exception{//注册新用户,插入数据库中
boolean flag=false;
try{
String sql="INSERT INTO member(mid,password,name,address,telephone,stamp,email) VALUES(?,?,?,?,?,?,?)";
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1, member.getMid());
this.pstmt.setString(2, member.getPassword());
this.pstmt.setString(3, member.getName());
this.pstmt.setString(4, member.getAddress());
this.pstmt.setString(5, member.getTelephone());
this.pstmt.setString(6, member.getStamp());
this.pstmt.setString(7, member.getEmail());
int add=pstmt.executeUpdate(sql);
if(add!=0){
flag=true;
}
}catch(Exception e){
throw e;
}finally{
if(conn!=null){
try{
conn.close();
}catch(Exception e){
throw e;
}
}
if(pstmt!=null){
try{
pstmt.close();
}catch(Exception ee){
throw ee;
}
}
}
return flag;
}
}
报错如下
com.mysql.jdbc.exceptions.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 '?,?,?,?,?,?,?)' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2548)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1605)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1524)
at zeng.dao.impl.MemberDAOImpl.addMember(MemberDAOImpl.java:58)
at zeng.dao.proxy.MemberDAOProxy.addMember(MemberDAOProxy.java:33)
at zeng.addmember.AddMember.doGet(AddMember.java:61)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServle