首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

JSP向数据库插入数据的有关问题

2012-04-12 
JSP向数据库插入数据的问题用DAO模式时package zeng.dao.implimport java.sql.Connectionimport java.sq

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(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at zeng.encoding.EncodingFilter.doFilter(EncodingFilter.java:18)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)


at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

这是个什么情况啊?应该怎么样插入呢?求教%%%

[解决办法]
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
你的SQL语句写错了。。你把SQL语句拿到数据库中区运行看看。肯定出错了
[解决办法]
int add=pstmt.executeUpdate(sql);

改成

int add=pstmt.executeUpdate();

热点排行