below is my stored procedure
create procedure sp_process_job(#request_id varchar(25), #host varchar(20),
created_on varchar(25)
)
as
begin
set dateformat mdy
SELECT CAST(#created_on as datetime)
insert into t_job_details(request_id ,host, created_on)
values(#request_id,#host,#created_on)
end
When I cast date using SELECT CAST(#created_on as datetime)
I get output as 2012-06-22 00:00:00.0 Time is 00:00:00.0
I want it as 12:45:06.0. Why I get 0 in all places?
Edit:
Calling the above procedure from java code
Date date = new Date();
Date insert_date = new java.sql.Date(date.getTime());
String insertquery = "{ call sp_process_job (?,?,?) }";
cs = con.prepareCall(insertquery.toString());
cs.setString(1, id);
cs.setString(2, host);
cs.setDate(3, (java.sql.Date) insert_date);
cs.execute();
con.commit();
I guess you should use java.sql.Timestamp in the Java code instead of java.sql.date which saves only date not time. And also cs.setTimestamp
java.sql.Timestamp insert_date = new java.sql.Timestamp(date.getTime());
....
cs.setTimestamp(3, insert_date);
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
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();
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.
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 have MSSQL procedure:
my_proc
I get data from java
String sql = "exec my_proc '2016-01-01','2016-01-20','2016-01-01'";
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();
and get all data fine!
but if I use
String msisdn, String startDate, String endDate
String sql = "exec my_proc ?,?,?";
stmt = connection.prepareStatement(sql);
stmt.setString(1, msisdn);
stmt.setString(2, startDate);
stmt.setString(3, endDate);
rs = stmt.executeQuery();
I get error:
com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type varchar to datetime.
In procedure I have variables:
#Startdtin datetime,
#Enddtin datetime,
#msisdnin varchar(18)
I tried use
stmt.setTimestamp(2, startDate); //startDate - convert to Timestamp
and
stmt.setDate(2, startDate); //startDate - convert to Date (sql and util)
It is not helped. How pass date to PreparedStatementcorrectly`?
Your startDate is of type String. So convert it into Date type.
java.util.Date myDate = new java.util.Date(startDate);
java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
-------------------------------
stmt.setDate(5, sqlDate);
Use CallableStatement to execute procedures.
Once corrected the format of your data you have to change the code.
To execute a procedure you need to use a CallableStatement, not a PreparedStatement.
If you have your date in string format, first convert it to Date in java using a SimpleDateFormat.
Than simply set it as Date on the callableStatement.
String dateString = ...;
Date date = ...;
myCallableStatement.setDate(1, date);