技术求助。在web中更新数据到数据库出错

teny_yan 2012-03-02 05:31:38
错误描述:
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 'gender='?”·' address='?????·' tel='15277886670' email='hp@163.com' birthday=null' at line 1
Query: update customer set name=? gender=? address=? tel=? email=? birthday=? Parameters: [何膨, 男, 上海, 15277886670, hp@163.com, null]

Servlet代码:
private void updateCustomer(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
Customer customer = new Customer();
CrmService crmService = new CrmService();
Enumeration<String> enums = request.getParameterNames();
try {
while(enums.hasMoreElements()){
String key = (String) enums.nextElement();
String [] values = request.getParameterValues(key);
ConvertUtils.register(
new DateLocaleConverter("yyyy-MM-dd"),
java.util.Date.class);
BeanUtils.setProperty(customer,key,values);
}

crmService.upDateCustomer(customer);

} catch (Exception e) {
e.printStackTrace();
request.setAttribute("message","更新客户信息失败");
request.getRequestDispatcher("/WEB- INF/jsp/message.jsp").forward(request,response);
}
}

Service代码:
//更新客户信息
public void upDateCustomer(Customer customer) throws Exception{
try {
crmDao.upDateCustomer(customer);
} catch (SQLException e) {
e.printStackTrace();
throw new UpdateException();
}
}

Dao代码:
public void upDateCustomer(Customer customer) throws SQLException{
QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
String sql = "update customer set name=? gender=? address=? tel=? email=? birthday=? ";
try {
runner.update(sql,
new Object[]{customer.getName(),
customer.getGender(),
customer.getAddress(),
customer.getTel(),
customer.getEmail(),
customer.getBirthday()});
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}
}

数据库结构表:
create table if not exists customer(
id int auto_increment primary key,
name varchar(20) not null,
gender varchar(6) not null,
address varchar(30) not null,
tel varchar(11) not null,
email varchar(20) not null,
birthday date not null
);
...全文
135 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jacksjjjacksjj 2012-03-02
  • 打赏
  • 举报
回复
如果没看到filter的话 把项目和页面的编码都设成一致的 还有后台的应该可以的
无可救药 2012-03-02
  • 打赏
  • 举报
回复
明明就是你代码写错了么。update 的set之间要有“,”分割的啊大哥。
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 teny_yan 的回复:]

问题解决了。
Dao代码中
String sql = "update customer set name=? gender=? address=? tel=? email=? birthday=? ";
--》
String sql = "update customer set name=?,gender=?,address=?,tel=?,email=?, birthday=? ";
……
[/Quote]
哈哈 还是lz自己细心。
支持把解决了的情况跟大家分享~~:)
teny_yan 2012-03-02
  • 打赏
  • 举报
回复
问题解决了。
Dao代码中
String sql = "update customer set name=? gender=? address=? tel=? email=? birthday=? ";
--》
String sql = "update customer set name=?,gender=?,address=?,tel=?,email=?, birthday=? ";
  • 打赏
  • 举报
回复
private void updateCustomer(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");//加上
......
......
}
MiceRice 2012-03-02
  • 打赏
  • 举报
回复
第一感觉,似乎是乱码的问题导致的。
teny_yan 2012-03-02
  • 打赏
  • 举报
回复
我还没学过滤器。刚刚学了DBUtil框架使用
teny_yan 2012-03-02
  • 打赏
  • 举报
回复
jsp代码:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="cn.itcast.web.crm.domain.*" %>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>



<%
Customer customer = (Customer)request.getAttribute("customer");
Date birthday = customer.getBirthday();

//Date和Calendar的转换
Calendar c = Calendar.getInstance();
c.setTime(birthday);

//分别取得年/月/日
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH) + 1;
int d = c.get(Calendar.DAY_OF_MONTH);

//将年/月/日绑定到域对象
pageContext.setAttribute("y",y);
pageContext.setAttribute("m",m);
pageContext.setAttribute("d",d);

%>

<form action="/day15/CrmServlet?method=updateCustomer" method="post">
<table border="1" align="center" width="80%">
<caption><h1>客户详细信息</h1></caption>
<tr>
<th>姓名</th>
<td><input type="text" name="name" value="${customer.name}"/></td>
</tr>
<tr>
<th>性别</th>
<td>
<input ${customer.gender=='男'?'checked':''} type="radio" name="gender" value="${customer.gender}"/>男
<input ${customer.gender=='女'?'checked':''} type="radio" name="gender" value="${customer.gender}"/>女
</td>
</tr>
<tr>
<th>地址</th>
<td><input type="text" name="address" value="${customer.address}"/></td>
</tr>
<tr>
<th>电话</th>
<td><input type="text" name="tel" value="${customer.tel}"/></td>
</tr>
<tr>
<th>邮箱</th>
<td><input type="text" name="email" value="${customer.email}"/></td>
</tr>
<tr>
<th>生日</th>
<td>
<select >
<c:forEach var="year" begin="1990" end="2020">
<option name="Y" ${year==y?'selected':''} value="${year}">${year}</option>
</c:forEach>
</select>年
<select >
<c:forEach var="month" begin="1" end="12">
<option name="M" ${month==m?'selected':''} value="${month}">${month}</option>
</c:forEach>
</select>月
<select>
<c:forEach var="date" begin="1" end="31">
<option name="D" ${date==d?'selected':''} value="${date}">${date}</option>
</c:forEach>
</select>日
</td>
</tr>
<tr>
<td colspan="7" align="center">
<input type="submit" value="更新" align="middle"/>
<a href="/day15/index.jsp" style="text-decoration:none">返回</a>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
updateCustomer(){

var url = "/day15/CrmServlet?method=updateCustomer;
window.location.href = url;
}
</script>
</body>
</html>
昨日凡阳 2012-03-02
  • 打赏
  • 举报
回复
从表单那过来的值已经是乱码了,你看看你sql中的???
。加个过滤器就可以解决。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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