关于表单提交,日期类型处理 报错信息不支持从 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个回答

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

gsh1075035316
SakuraRe 呃 不是太明白 那个 我觉得 问题确实是birthday的date属性 我试了 我用String替换掉birthday的date 是可以插入的 难道以后表单里和实体中碰见日期型数据 我都用String吗?所以很疑惑 请指教 好吧?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐