How to insert Date into mysql? - java

I am trying to insert a Date into mySql db . I accept date as a string and parse it to util.Date then convert it to sql.Date and then I am inserting into database . Here is my code
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String mydate=dsd.getDoa();
Date mydate2=format.parse(mydate);
java.sql.Date sqlDate;
sqlDate = new java.sql.Date(mydate2.getTime());
System.out.println("Date"+sqlDate.getDate()+"Month"+sqlDate.getMonth()+"Year"+sqlDate.getYear());
....
int i=st.executeUpdate("insert into device_sales_details values("+sqlDate+"));
The error is as follow ,
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '1980' for column 'device_sales_details_DOA' at row 1
The Sysout prints as follow for input 22/12/2014
Date22Month11Year114 How to solve this problem , can any one help me in this please.

Use a PreparedStatement. It's almost never correct to use string concatenation to create SQL code to send through JDBC.
PreparedStatement ps = connection.prepareStatement("insert into device_sales_details values(?)");
ps.setDate(1, sqlDate);
int i = ps.executeUpdate();
More about not using string concatenation here: http://bobby-tables.com
Separately, it's generally best to specify the columns when doing an insert, e.g.: insert into device_sales_details (columnName) values (?)

Related

How to insert sql date as 04/05/2017 am trying with this

java.util.Date date = new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("dob"));
System.out.println(date);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
PreparedStatement ps=cn.prepareStatement("insert into lol values(?,?)");
ps.setString(1, name);
ps.setDate(2, sqlDate);
But output is 05-MAY-17 this, i want store 04-05-2017 in oracle db. Can any one help me
Make the varchar column in db . Store it as a String .
When you want to do some operation at Application Level , convert the string into date .
For that you can use SimpleDateFormat

preparedStatement with TimeStamp compare two date

i have this code
preparedStatement = jdbcManager.getConnection().prepareStatement(query);
Date start; /*get from postgres column type-> Timestamp without time zone*/
java.sql.Timestamp timestamp = new java.sql.Timestamp(start.getTime());
log.debug("Parametro d'ingresso query: "+timestamp);
preparedStatement.setTimestamp(1, timestamp);
and i have this query
SELECT
DATA
FROM MY_TABLE
WHERE DATA > ?
the column DATA is DATA_TYPE = DATE in a Oracle db
the compare in the query not working,
what am I doing wrong?
You're comparing date with timestamp. Instead of fetching time stamp, get java.sql.Date. And also in prepared statement, use setDate instead of setTimeStamp.

Date specific format in mysql

Is there any way to store the below date in mysql table
Date = 2017-01-05T00:00:00+05:30
//Table
create table test(dob DATETIME);
//Insert
insert into test(dob) values ('2017-01-05T00:00:00+05:30') // Throws error saying Incorrect datetime
Is there way I can insert the below date in mysql db from java code.
If you have a Date object, good. Otherwise, parse the string into a Date object using SimpleDateFormat like this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date d = sdf.parse("2017-01-05T00:00:00+05:30");
Then you can make a PreparedStatement to do the date insert like this:
PreparedStatement ps = conn.prepareStatement("insert into test(dob) values (?)");
ps.setDate(1, new java.sql.Date(d.getTime()));
ps.executeUpdate();

Convert Joda Date Time String To Java.Sql.TimeStamp for an INSERT

Here is my string: 2016-07-29T17:15:46.838Z
I want to insert this into a MySQL DATETIME(6) column.
Here is the method I created to convert the string to a java.sql.Timestamp
private java.sql.Timestamp convertToJavaSqlTimeStamp(String p_dateTimeString) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss:SSS");
java.sql.Timestamp _timeStamp = new java.sql.Timestamp(formatter.parseDateTime(p_dateTimeString).getMillis());
return _timeStamp;
}
.
.
.
SQL Insert logic...
PreparedStatement preparedStatement ;
preparedStatement = _mysqlConn.prepareStatement("INSERT INTO myTable (my_date_time) VALUES (?)");
preparedStatement.setTimestamp(1, convertJodaDateTimeStringToJavaSqlTimeStamp("2016-07-29T17:15:46.838Z"));
preparedStatement.executeUpdate();
Error Message: Invalid format: "2016-07-29T17:15:46.432Z" is malformed at "-07-29T17:15:46.432Z"
Going off Uueerdo's comment. The following did the trick
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
Thank you sir.

insert current date in oracle database using jdbc

I am trying to insert current date into Oracle sql.I had set the datatype to be DATE in oracle sql and changed the date format to DD-MM-YYYY.Now I am trying to insert current date as follows.But I am getting some error as shown below.Don't know what is wrong
String sql6 = "insert into account(acc_no,acc_type,primary_phone_number,people_in_plan,acc_activated_date,acc_deactivated_date) values('"+n1+"','"+acctype+"','"+primaryphoneno+"','"+number_of_people+"',?,?)";
Stmt = connection.prepareStatement(sql6);
SimpleDateFormat sdf=new SimpleDateFormat("DD-MM-YYYY");
Date date1 = new Date(System.currentTimeMillis());
Stmt.setString(5, sdf.format(date1));
Stmt.setNull(6, java.sql.Types.DATE);
Stmt.executeUpdate();
java.sql.SQLException: Invalid column index
at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5386)
at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5374)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setString(OraclePreparedStatementWrapper.java:282)
It looks like your column indexes are incorrect. They should be 1 and 2 respective.
Stmt.setString(1, sdf.format(date1));
Stmt.setNull(2, java.sql.Types.DATE);
please user statement.setObject(1, new java.sql.Date());instead

Categories