简体   繁体   English

如何使用jsp在mysql中插入日期?

[英]how to insert date in mysql using jsp?

this is my edited code... 这是我编辑的代码...

         java.sql.Timestamp sqlNow = new java.sql.Timestamp(new java.util.Date().getTime());

                stmt = con.createStatement();
    //stmt.executeUpdate("INSERT INTO cregn values('"+appno+"','"+usname+"','"+upwd +"','"+fname+"','"+mname+"','"+lname+"','"+dob+"','"+gend+"','"+faname+"','"+saddr+"','"+caddr+"','"+staddr +"','"+pin+"','"+cno+"','"+email+"','"+occ+"','"+secques+"','"+answer+"','Processing','"+sqlNow+"')");
                pst = con.prepareStatement("INSERT INTO cregn (aplno, username, pwd, firstname,middlename,lastname, dob,gender, fathername, street,city,state, pincode, phone, email,occupation,secques,answer,dor)  VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

                pst.setInt(1, appno);
                pst.setString(2, usname);
                pst.setString(3, upwd);
                pst.setString(4, fname);
                pst.setString(5, mname);
                pst.setString(6, lname);
                pst.setString(7, dob);
                pst.setString(8, gend);
                pst.setString(9, faname);
                pst.setString(10, saddr);
                pst.setString(11, caddr);
                pst.setString(12, staddr);
                pst.setString(13, pin);
                pst.setString(14, cno);
                pst.setString(15, email);
                pst.setString(16, occ);
                // pst.setString(17,ph);
                pst.setString(17, secques);
                pst.setString(18, answer);

                pst.setTimestamp(19, sqlNow);

                pst.executeUpdate();

                out.println("Registration Successful for application " + appno);
            } catch (Exception ee) {
                out.println("Invalid Data! All fields are mandatory..." + ee.getMessage());
            }


%>

after executing this code error displayed "Invalid Data! All fields are mandatory..data truncation" 执行此代码错误后,显示“无效数据!所有字段均为必填项。数据截断”

You are not executing the statement. 您没有执行该语句。

You need to call executeUpdate 您需要致电executeUpdate

You have to call execute or executeUpdate on pst in order to actually do the work. 您必须在pst上调用executeexecuteUpdate才能真正完成工作。 Eg, at the end: 例如,最后:

pst.executeUpdate();

All that the code you've shown does is prepare the statement for execution, it doesn't execute it. 您所显示的代码所做的只是准备执行语句,而不执行该语句。 This is one of the main differences between PreparedStatement and Statement . 这是PreparedStatementStatement之间的主要区别之一。 In Statement , you pass the SQL to execute directly into executeUpdate (as seen in your commented-out line using stmt ). Statement ,将SQL直接传递给executeUpdate (如使用stmt在注释行中所示)。 With a PreparedStatement , you supply the SQL with ? 使用PreparedStatement ,可以为SQL提供? as you've done, set those parameters, and then call executeUpdate (or execute ), which you haven't done. 完成后,设置这些参数, 然后调用尚未完成的executeUpdate (或execute )。

pst上没有executeUpdate()

use pst.executeUpdate(); 使用pst.executeUpdate(); to execute the query and make sure that in transaction use setAutoCommit(true) has to be called. 执行查询并确保在事务中必须setAutoCommit(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM