0

JAVA code

public boolean userReg(User user) throws SQLException, ClassNotFoundException {
    Connection conn = null;
    CallableStatement cs = null;
    try {
        conn = OracleDAOFactory.createConnection();
        cs = conn.prepareCall("{call REG_USER(?,?,to_date(?,'yyyy-MM-dd'),?,?)}");
        cs.setString(1, user.getEmail());
        cs.setString(2, user.getPassword());
        cs.setString(3, user.getBirthday());
        cs.setString(4, user.getAbout());
        cs.setString(5, user.getFullName());
        cs.execute();
        cs.close();
    } catch (SQLException e) {
        e.getStackTrace();
    } finally {
        assert conn != null;
        conn.close();
    }
    return true;
}

Oracle SQL

CREATE OR REPLACE PROCEDURE REG_USER 
( 
EMAIL IN VARCHAR2, 
PASSWORD IN VARCHAR2, 
BIRTHDAY IN DATE, 
ABOUT IN VARCHAR2, 
FULLNAME IN VARCHAR2 
) AS BEGIN 
INSERT INTO MAILBOX_USERS VALUES(EMAIL,FULLNAME,PASSWORD,BIRTHDAY,ABOUT,1,0); 
END REG_USER;

I have the following problem:

I can`t add date to my database, When executing code without date adding, everything works correctly.

How can I prepare Date field for adding to DB?

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76

1 Answers1

1

Change you code to use setDate and pass parsed date using java.sql.Date.valueOf(String) which require date string in format yyyy-MM-dd

public boolean userReg(User user) throws SQLException, ClassNotFoundException { 
Connection conn = null; 
CallableStatement cs = null; 
try { 
  conn = OracleDAOFactory.createConnection(); 
  cs = conn.prepareCall("{call REG_USER(?,?,?,?,?)}"); 
  cs.setString(1, user.getEmail()); 
  cs.setString(2, user.getPassword()); 
  cs.setDate(3, java.sql.Date.valueOf(user.getBirthday()));
  cs.setString(4, user.getAbout()); 
  cs.setString(5, user.getFullName()); 
  cs.execute(); 
  cs.close(); 
}catch (SQLException e){ 
e.getStackTrace(); 
}finally { 
assert conn != null; 
conn.close(); 
} 
return true; 
}

Check this out for more on passing date and timestamp parameters.

Community
  • 1
  • 1
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76