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();
Related
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
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
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 (?)
I am looking for a way to get today's date and pass to sql table and save there. Call the saved date and do some task with JODA TIME API. The changed Joda time Date to sql table and save there and process continues..
I tried this way,
//prints todays date
java.sql.Date sqlDate = new java.sql.Date(new Date().getTime());
//passes wrong date to the table like 1970-07-01 instead of 2013-03-01
String insert = "INSERT INTO TEST_TABLE VALUES(1,"+sqlDate+")";
pStmt = conn.prepareStatement(insert);
pStmt.executeUpdate();
//converting to joda time
LocalDate ld = new LocalDate(sqlDate);
//some calculations, and how to convert back to sql date?
What I am trying to do here is, A table with 3 columns (id, startdate, finishdate). id will be entered by user, start date should be automatically entered todays date. after some calculations with joda time and finish date will be set to date it is finished.
Code
String insert = "INSERT INTO TEST_TABLE VALUES(2,'"+timestamp+"')";
Error
Data type mismatch in criteria expression
//I have created table using MS access
//the format of the date column is Date/Time.
You Can use Timestamp here. java.sql.Timestamp extends java.util.Date, so anything you can do with a java.util.Date you can also do with a java.sql.Timestamp.
To convert LocalDateTime to Timestamp
Timestamp timestamp = new Timestamp(localDateTime.toDateTime().getMillis());
But if You still want to convert Timestamp into java.sql.Date then use this
java.sql.Date date = new java.sql.Date(timeStamp.getTime());
I have a column in database having datatype DATETIME. I want to set this column value to current date and time using `PreparedStatement. How do I do that?
Use PreparedStatement#setTimestamp() wherein you pass a java.sql.Timestamp which is constructed with System#currentTimeMillis().
preparedStatement.setTimestamp(index, new Timestamp(System.currentTimeMillis()));
// ...
Alternativaly, if the DB supports it, you could also call a DB specific function to set it with the current timestamp. For example MySQL supports now() for this. E.g.
String sql = "INSERT INTO user (email, creationdate) VALUES (?, now())";
Or if the DB supports it, change the field type to one which automatically sets the insert/update timestamp, such as TIMESTAMP instead of DATETIME in MySQL.
conn = getConnection();
String query = "insert into your_table(id, date_column) values(?, ?)";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, "0001");
java.sql.Date date = getCurrentDatetime();
pstmt.setDate(2, date);
Where the function getCurrentDatetime() does the following:
public java.sql.Date getCurrentDatetime() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
}