SakuraRe 2019-05-06 17:44 采纳率: 50%
浏览 1286

关于表单提交,日期类型处理 报错信息不支持从 UNKNOWN 到 UNKNOWN 的转换。

c3p0连接已测,其他正常 只有添加功能报错
数据库中birthday为datetime类型
报错信息和关键代码贴出

java.sql.SQLException: 不支持从 UNKNOWN 到 UNKNOWN 的转换。 Query: insert into stu (sname,sex,sphone,sbirthday,shobby,sinfo) values(?,?,?,?,?,?) Parameters: [aa, 男, 21332132, Sun Dec 30 17:21:15 CST 2018, 游泳, s]
    at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:320)
    at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:489)
    at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:455)
    at com.dao.impl.StudentDaoImpl.insert(StudentDaoImpl.java:32)
    at com.service.impl.StudentServiceImpl.insert(StudentServiceImpl.java:30)
    at com.servlet.AddStudentServlet.doGet(AddStudentServlet.java:57)
    at com.servlet.AddStudentServlet.doPost(AddStudentServlet.java:82)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
AddStudentServlet.java
package com.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.damain.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

/**
 * Servlet implementation class AddStudentServlet
 */
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddStudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        try {
            //获取数据
            request.setCharacterEncoding("UTF-8");
            String name = request.getParameter("sname");
            String sex = request.getParameter("sex");
            String phone = request.getParameter("sphone");
            String birthday = request.getParameter("sbirthday"); 
            String hobby = request.getParameter("shobby");
            String info = request.getParameter("sinfo");
            //String转为date
            SimpleDateFormat sdf1 =new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
            //添加到数据库
            Date date;
            try {
                date = sdf1.parse(birthday);
                StudentService service = new StudentServiceImpl();
                Student student = new Student(name,sex,phone,date,hobby,info);
                service.insert(student);

                //跳转到列表页


                request.getRequestDispatcher("StudentListServlet").forward(request, response);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
Student.java
package com.damain;

import java.util.Date;

/*
 * 这是学生封装的对象bean 
 */
public class Student {
private int sid;
private String sname;
private String sex;
private String sphone;
private Date sbirthday;
private String shobby;
private String sinfo;


public Student() {
    super();
}
public Student( String sname, String sex, String sphone, Date sbirthday, String shobby, String sinfo) {
    super();
    this.sname = sname;
    this.sex = sex;
    this.sphone = sphone;
    this.sbirthday = sbirthday;
    this.shobby = shobby;
    this.sinfo = sinfo;
}
public int getSid() {
    return sid;
}
public void setSid(int sid) {
    this.sid = sid;
}
public String getSname() {
    return sname;
}
public void setSname(String sname) {
    this.sname = sname;
}
public String getSex() {
    return sex;
}
public void setSex(String sex) {
    this.sex = sex;
}
public String getSphone() {
    return sphone;
}
public void setSphone(String sphone) {
    this.sphone = sphone;
}
public Date getSbirthday() {
    return sbirthday;
}
public void setSbirthday(Date sbirthday) {
    this.sbirthday = sbirthday;
}
public String getShobby() {
    return shobby;
}
public void setShobby(String shobby) {
    this.shobby = shobby;
}
public String getSinfo() {
    return sinfo;
}
public void setSinfo(String sinfo) {
    this.sinfo = sinfo;
}

}

package com.dao.impl;


import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;

import com.damain.Student;
import com.dao.StudentDao;
import com.util.JDBCUtil;

/*
 * 这是StudentDao的实现
 */
public class StudentDaoImpl implements StudentDao {

    @Override
    public List<Student> findall() throws SQLException {
        // TODO Auto-generated method stub
        QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
        List<Student> list =runner.query("Select * from stu",new BeanListHandler<Student>(Student.class));
        return list;
    }

    @Override
    public void insert(Student student) throws SQLException {
        // TODO Auto-generated method stub
        QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
        runner.update("insert into stu (sname,sex,sphone,sbirthday,shobby,sinfo) values(?,?,?,?,?,?)",
                student.getSname(),
                student.getSex(),
                student.getSphone(),
                student.getSbirthday(),
                student.getShobby(),
                student.getSinfo()
                );
    }
  • 写回答

1条回答 默认 最新

  • asdf3301 2019-05-06 19:41
    关注

    最后runner.update传string吧,传date应该也是调用toString

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包