简体   繁体   中英

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

You have to call execute or executeUpdate on pst in order to actually do the work. 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 . In Statement , you pass the SQL to execute directly into executeUpdate (as seen in your commented-out line using stmt ). With a PreparedStatement , you supply the SQL with ? as you've done, set those parameters, and then call executeUpdate (or execute ), which you haven't done.

pst上没有executeUpdate()

use pst.executeUpdate(); to execute the query and make sure that in transaction use setAutoCommit(true) has to be called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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