数据截断:日期时间值不正确:'' [英] Data truncation: Incorrect datetime value: ''

查看:648
本文介绍了数据截断:日期时间值不正确:''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以通过示例JSP代码来帮助我,通过JDBC在MySql数据库中存储日期?
当我尝试执行下面给出的代码时,我得到以下异常:


com.mysql.jdbc.MysqlDataTruncation:数据截断:日期时间值不正确:第1行列日期的列表


如何克服这个问题?以下是我的代码:

 连接con = null; 

String StaffName = request.getParameter(StaffName);
// String subcode = request.getParameter(subcode);
String hourId = request.getParameter(hourId);
if(hourId == null)
hourId =;
String day = request.getParameter(day);
if(day == null)
day =;
String date = request.getParameter(date);
尝试{
Class.forName(com.mysql.jdbc.Driver);
con = DriverManager.getConnection(jdbc:mysql:// localhost:3306 / StaffAllocation,root,success);

// PreparedStatement stat = con.PrepareStatement();
String updateString =INSERT INTO tblstaffallocation(StaffName,hourId,daysId,date)VALUES(?,?,?,?);
PreparedStatement preparedStatement = con.prepareStatement(updateString);

preparedStatement.setString(1,StaffName);
preparedStatement.setInt(2,0);
preparedStatement.setInt(3,0);
preparedStatement.setString(4,date);
} catch(Exception e){
out.print(e);
}


解决方案

将日期设置为准备声明您需要更改类型的值:

  String date = request.getParameter(date); 
SimpleDateFormat formatter = new SimpleDateFormat(yyyy-MM-dd); //你的模板
java.util.Date dateStr = formatter.parse(date);
java.sql.Date dateDB = new java.sql.Date(dateStr.getTime());

现在将String日期转换为java.sql.Date并使用其他方法:

  preparedStatement.setDate(4,dateDB); 


Can anyone help me with a sample JSP code to store date in a MySql database through JDBC? When I try to execute the code given below, I get the following exception:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '' for column 'date' at row 1

How to overcome this problem? Following is my code:

Connection con = null;

String StaffName = request.getParameter("StaffName");
// String subcode = request.getParameter("subcode");
String hourId = request.getParameter("hourId");
if (hourId == null)
    hourId = "";
String day = request.getParameter("day");
if (day == null)
    day = "";
String date = request.getParameter("date");
try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/StaffAllocation", "root", "success");

    // PreparedStatement stat = con.PrepareStatement();
    String updateString = "INSERT INTO tblstaffallocation (StaffName,hourId,daysId,date) VALUES (?,?,?,?)";
    PreparedStatement preparedStatement = con.prepareStatement(updateString);

    preparedStatement.setString(1, StaffName);
    preparedStatement.setInt(2, 0);
    preparedStatement.setInt(3, 0);
    preparedStatement.setString(4, date);
} catch (Exception e) {
    out.print(e);
}

解决方案

To set date to prepared statement you need change type of value:

String date = request.getParameter("date");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // your template here
java.util.Date dateStr = formatter.parse(date);
java.sql.Date dateDB = new java.sql.Date(dateStr.getTime());

now convert String date to java.sql.Date and use another method:

preparedStatement.setDate(4,dateDB);

这篇关于数据截断:日期时间值不正确:''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆