请高人指教到底哪里错了呢???

xingshen100 2013-10-12 10:22:30

package cn.itcast.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
import cn.itcast.exception.DaoException;
import cn.itcast.utils.JdbcUtils;

public class UserDaoJdbcImpl implements UserDao {

public void add(User user) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;

try {
conn=JdbcUtils.getConnection();
String sql="insert into users(id,username,password,email,birthday) values(?,?,?,?,?)";
st=conn.prepareStatement(sql);
st.setString(1,user.getId());
st.setString(2,user.getUsername());
st.setString(3, user.getPassword());
st.setString(4,user.getEmail());
st.setDate(5,new java.sql.Date(user.getBirthday().getTime()));

st.executeUpdate(sql);

} catch (Exception e) {
e.printStackTrace();
}finally{
JdbcUtils.release(conn, st, rs);
}
}

public User find(String username, String password) {
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;

try {
conn=JdbcUtils.getConnection();
String sql="select * from users where username=? and password=?";
st=conn.prepareStatement(sql);
st.setString(1,username);
st.setString(2,password);

rs=st.executeQuery();
if(rs.next()){
User user=new User();
user.setId(rs.getString("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
return user;
}
return null;
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new DaoException(e);
}finally{
JdbcUtils.release(conn, st, rs);
}
}

public User find(String username) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;

try {
conn=JdbcUtils.getConnection();
String sql="select * from users where username=?";
st=conn.prepareStatement(sql);
st.setString(1,username);

rs=st.executeQuery();
if(rs.next()){
User user=new User();
user.setId(rs.getString("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
return user;
}
return null;
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new DaoException(e);
}finally{
JdbcUtils.release(conn, st, rs);
}
}

}




错误信息:2013-10-12 22:12:55 org.apache.catalina.startup.HostConfig checkResources
信息: Reloading context [/myday14_user]
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:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
at cn.itcast.dao.impl.UserDaoJdbcImpl.add(UserDaoJdbcImpl.java:32)
at cn.itcast.service.impl.BusinessServiceImpl.registerUser(BusinessServiceImpl.java:25)
at cn.itcast.web.controller.RegisterServlet.doGet(RegisterServlet.java:47)
at cn.itcast.web.controller.RegisterServlet.doPost(RegisterServlet.java:67)
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(ApplicationFilterChain.java:290)
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:128)
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:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
我实在没发现啊。。。我是对着写的呀
...全文
445 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunbo624 2013-10-14
  • 打赏
  • 举报
回复
个人觉得PreparedStatement不应该继承Statement,JDK这个设计的不太好 因为获取的方式不一样 一个是createStatement 一个是preparedStatement 这里就已经说明他俩不是一个东西了 至少不能一样 继承本来就应该是子类可以当作父类用 但是这两个类的API不同 换句话说PreparedStatement isn't a Statement
xingshen100 2013-10-13
  • 打赏
  • 举报
回复
引用 6 楼 rainbowsix 的回复:
[quote=引用 4 楼 xingshen100 的回复:] [quote=引用 3 楼 u011222719 的回复:] st=conn.prepareStatement(sql); st.executeUpdate(); 这样看看
还真是,把sql去掉就能正常运行了?你知道为什么吗??写不写sql有什么区别呢??[/quote] 带sql参数是直接执行sql,用于拼接好的sql[/quote] executeUpdate() 括号里面写不写sql有何区别呢??我这个程序就是不写时能正常运行,写了运行时会报错。
无聊找乐 2013-10-13
  • 打赏
  • 举报
回复
引用 4 楼 xingshen100 的回复:
[quote=引用 3 楼 u011222719 的回复:] st=conn.prepareStatement(sql); st.executeUpdate(); 这样看看
还真是,把sql去掉就能正常运行了?你知道为什么吗??写不写sql有什么区别呢??[/quote] 带sql参数是直接执行sql,用于拼接好的sql
xingshen100 2013-10-13
  • 打赏
  • 举报
回复
引用 4 楼 xingshen100 的回复:
[quote=引用 3 楼 u011222719 的回复:] st=conn.prepareStatement(sql); st.executeUpdate(); 这样看看
把第一个问号去掉
xingshen100 2013-10-13
  • 打赏
  • 举报
回复
引用 3 楼 u011222719 的回复:
st=conn.prepareStatement(sql); st.executeUpdate(); 这样看看
还真是,把sql去掉就能正常运行了?你知道为什么吗??写不写sql有什么区别呢??
无聊找乐 2013-10-13
  • 打赏
  • 举报
回复
引用 14 楼 xingshen100 的回复:
[quote=引用 11 楼 u011222719 的回复:] 那楼主问题解决了没有?没有报错ok了
引用 10 楼 rainbowsix 的回复:
[quote=引用 7 楼 xingshen100 的回复:] [quote=引用 6 楼 rainbowsix 的回复:] [quote=引用 4 楼 xingshen100 的回复:] [quote=引用 3 楼 u011222719 的回复:] st=conn.prepareStatement(sql); st.executeUpdate(); 这样看看
还真是,把sql去掉就能正常运行了?你知道为什么吗??写不写sql有什么区别呢??[/quote] 带sql参数是直接执行sql,用于拼接好的sql[/quote] executeUpdate() 括号里面写不写sql有何区别呢??我这个程序就是不写时能正常运行,写了运行时会报错。[/quote] executeUpdate int executeUpdate(String sql) throws SQLException Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. Parameters: sql - an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement. Returns: either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing Throws: SQLException - if a database access error occurs, this method is called on a closed Statement or the given SQL statement produces a ResultSet object ================================================================== executeQuery ResultSet executeQuery() throws SQLException Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query. Returns: a ResultSet object that contains the data produced by the query; never null Throws: SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement does not return a ResultSet object[/quote] 这并没有说关于是否在executeUpdate()方法中传入sql呀???[/quote] 晕~~ 没看到两个函数的申明不一样吗? 一个有参数,一个没参数~~
xingshen100 2013-10-13
  • 打赏
  • 举报
回复
引用 11 楼 u011222719 的回复:
那楼主问题解决了没有?没有报错ok了
引用 10 楼 rainbowsix 的回复:
[quote=引用 7 楼 xingshen100 的回复:] [quote=引用 6 楼 rainbowsix 的回复:] [quote=引用 4 楼 xingshen100 的回复:] [quote=引用 3 楼 u011222719 的回复:] st=conn.prepareStatement(sql); st.executeUpdate(); 这样看看
还真是,把sql去掉就能正常运行了?你知道为什么吗??写不写sql有什么区别呢??[/quote] 带sql参数是直接执行sql,用于拼接好的sql[/quote] executeUpdate() 括号里面写不写sql有何区别呢??我这个程序就是不写时能正常运行,写了运行时会报错。[/quote] executeUpdate int executeUpdate(String sql) throws SQLException Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. Parameters: sql - an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement. Returns: either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing Throws: SQLException - if a database access error occurs, this method is called on a closed Statement or the given SQL statement produces a ResultSet object ================================================================== executeQuery ResultSet executeQuery() throws SQLException Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query. Returns: a ResultSet object that contains the data produced by the query; never null Throws: SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement does not return a ResultSet object[/quote] 这并没有说关于是否在executeUpdate()方法中传入sql呀???
xingshen100 2013-10-13
  • 打赏
  • 举报
回复
引用 12 楼 ch656409110 的回复:
原来这边已经有答案了 。。。 我刚刚试了一下 发现我错了,两边指定sql语句一样有效果也没有报错。。。我是用的是oracle数据库。。 你试试更新高版本的jar包
我用的是mysql数据库 在百度上问,大家都说PreparedStatement对象在Connnection对象调用prepareStatement()方法时,传过了,不用再st.executeUdate()中传。
  • 打赏
  • 举报
回复
原来这边已经有答案了 。。。 我刚刚试了一下 发现我错了,两边指定sql语句一样有效果也没有报错。。。我是用的是oracle数据库。。 你试试更新高版本的jar包
C-J 2013-10-13
  • 打赏
  • 举报
回复
那楼主问题解决了没有?没有报错ok了
无聊找乐 2013-10-13
  • 打赏
  • 举报
回复
引用 7 楼 xingshen100 的回复:
[quote=引用 6 楼 rainbowsix 的回复:] [quote=引用 4 楼 xingshen100 的回复:] [quote=引用 3 楼 u011222719 的回复:] st=conn.prepareStatement(sql); st.executeUpdate(); 这样看看
还真是,把sql去掉就能正常运行了?你知道为什么吗??写不写sql有什么区别呢??[/quote] 带sql参数是直接执行sql,用于拼接好的sql[/quote] executeUpdate() 括号里面写不写sql有何区别呢??我这个程序就是不写时能正常运行,写了运行时会报错。[/quote] executeUpdate int executeUpdate(String sql) throws SQLException Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. Parameters: sql - an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement. Returns: either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing Throws: SQLException - if a database access error occurs, this method is called on a closed Statement or the given SQL statement produces a ResultSet object ================================================================== executeQuery ResultSet executeQuery() throws SQLException Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query. Returns: a ResultSet object that contains the data produced by the query; never null Throws: SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement does not return a ResultSet object
xingshen100 2013-10-13
  • 打赏
  • 举报
回复
引用 8 楼 u011222719 的回复:
有区别的,刚学jdbc的吧, 你要清楚st=conn.prepareStatement(sql);是做什么的?简单来说这句才是执行sql语句 st.executeUpdate();又是做什么的?这句用来执行数据库的插入,修改以及删除操作 (stmt是Statement)ResultSet rs=stmt.executeQuery(sql);才是返回结果集; stmt.executeQuery();这是完成数据库的查询 还有用PreparedStatement和Statement的区别。。。。 这些都要清楚的,不过后面用hibernate的话就不用那么烦了
说了一大堆,没说到关键的!!!
C-J 2013-10-13
  • 打赏
  • 举报
回复
有区别的,刚学jdbc的吧, 你要清楚st=conn.prepareStatement(sql);是做什么的?简单来说这句才是执行sql语句 st.executeUpdate();又是做什么的?这句用来执行数据库的插入,修改以及删除操作 (stmt是Statement)ResultSet rs=stmt.executeQuery(sql);才是返回结果集; stmt.executeQuery();这是完成数据库的查询 还有用PreparedStatement和Statement的区别。。。。 这些都要清楚的,不过后面用hibernate的话就不用那么烦了
C-J 2013-10-12
  • 打赏
  • 举报
回复
st=conn.prepareStatement(sql); st.executeUpdate(); 这样看看
xingshen100 2013-10-12
  • 打赏
  • 举报
回复
引用 1 楼 u011222719 的回复:
报错在32行: at cn.itcast.dao.impl.UserDaoJdbcImpl.add(UserDaoJdbcImpl.java:32) add方法里st=conn.prepareStatement(sql);和这个st.executeUpdate(sql); ?
没有啊,没看出来哪里错了。
C-J 2013-10-12
  • 打赏
  • 举报
回复
报错在32行: at cn.itcast.dao.impl.UserDaoJdbcImpl.add(UserDaoJdbcImpl.java:32) add方法里st=conn.prepareStatement(sql);和这个st.executeUpdate(sql); ?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧