Set timestamp in Oracle - java

I use Prepared statements in Oracle SQL query. I have problem which I don't know how to solve.
I want to set timestamp like using this:
ps.setTimestamp(36, null);
The problem is that I don't know what parameter to set in order to execute timestamp into Oracle? How I must replace null in my case?

This example sets a Timestamp value to the current time (with precision to milliseconds):
Timestamp ts = new Timestamp(new Date().getTime());
ps.setTimestamp(36, ts);
If you also need nanoseconds you can set those separately:
Timestamp ts = new Timestamp(new Date().getTime());
ts.setNanos(12345);
ps.setTimestamp(36, ts);

If you look at the method in the PreparedStatement class, you need to create a TimeStamp object that you will replace your null with.

Related

Could not save date field as ISO date in mongo db via Camel (Caused by: java.lang.IllegalArgumentException: Invalid BSON field name $date) [duplicate]

We are trying to insert a document with the current date as it's field. We are writing in java using eclipse plugin for mongodb. We want to execute the Date() command of mongo to get the date from mongo and not from java.
How can I execute this mongo query?
db.example.insert({"date":new Date()})
I found this question in a previews question but the answer was not helpful
Link
The standard driver takes java.util.date types and serializes as BSON dates. So with a collection object to "example"
Date now = new Date();
BasicDBObject timeNow = new BasicDBObject("date", now);
example.insert(timeNow);
If you are looking for a way to use the "server" time in operations, there is the $currentDate operator, but this works with "updates", so you would want an "upsert" operation:
BasicDBObject query = new BasicDBObect();
BasicDBObject update = new BasicDBObject("$currentDate",
new BasicDBObject("date", true)
);
example.update(query,update,true,false);
Since that actually is an update statement, you need to be careful that you are not actually matching any documents if you intend this to be an insert only. So it would be best to make sure your "query" contains unique information, such as a newly generated _id or something equally unique.
You can do it trying something like this:
db.example.insert({"date":ISODate("2016-03-03T08:00:00.000")});
Use this:
db.example.insert({"date":new Date(Date.now())});
There is a key difference I noted when using Date() as follows.
{ dateWhenCreated : Date() }
vs
{ dateWhenCreated : new Date() }
Notice the "new" keyword in the second usage. Whereas the first usage loads the data "as a string", the second one loads date as a field with date data type.
This might impact your sorting capability - dates stored as strings don't get sorted the same way as dates stored as dates.
Per the mongodb documentation here

Can't retrieve correct time from database

I have fields startTime and endTime in my table with type as time. When I try to retrieve it with query it gives wrong values of time even if the data table values are correct.
The query is as follows :
SELECT
ts.id,
ts.serviceRuleId,
FROM_UNIXTIME(ts.startTime, '%h %i %p') AS tempStrtTime,
FROM_UNIXTIME(ts.endTime, '%h %i %p') AS tempEndTime,
ts.createdBy,
ts.createdOn,
ts.startDate,
ts.endDate
FROM
TemporaryTimeSlots AS ts
GROUP BY
ts.id
Why are you using the FROM_UNIXTIME etc.
You are using Java, so use the SQL.Date object and you will get the correct result
select ts.id, ts.serviceRuleId,ts.startTime as tempStrtTime, ts.endTime as tempEndTime, ts.createdBy, ts.createdOn, ts.startDate, ts.endDate from TemporaryTimeSlots as ts group by ts.id

Using timestamp with mysql and java

I'm trying to save a timestamp to a table in mySQL but whenever I look at the results it just shows 0000-00-00 00:00:00.
I assume I'm not using the timestamp right but anyways in my table I have a column named time and its property is TIMESTAMP
In my Java I have:
java.sql.Timestamp timestamp = new java.sql.Timestamp(0);
String query = "insert ignore into time(time_now) values (?)";
pstmt.setString(1, timestamp);
pstmt.executeUpdate();
My database connection is fine as I have a lot of other information that's being uploaded to it with no problem, I'm just having trouble with the timestamp
Something like this should work:
java.util.Date date = new java.util.Date();
Timestamp timestamp = new Timestamp(date.getTime());
preparedStatement = connection.prepareStatement("insert ignore into time(time_now) values (?)");
preparedStatement.setTimestamp(1, timestamp);
If you only want to store the time then use the TIME data type instead and to insert the current time of the SQL server use CURTIME() like this
insert into your_table (time_column)
values (curtime())
Take a look at java.sql.Timestamp. You are instantiating a new Timestamp object with '0'.

PreparedStatement and DateTime

I am trying to use DateTime from the Joda library and I realized that PreparedStatements doesn't support DateTime. What can I do instead? I can set TimeStamp but that's not really what I want. Is there a workaround or do I have to use TimeStamp? In my MySQL database I have also chosen DateTime as the type.
DatabaseConnection dbConnect = new DatabaseConnection();
PreparedStatement query =dbConnect.getConnect().prepareStatement(sql);
query.setInt(1, c.getMemberId());
query.setDateTime(2, c.getStartDate());
query.setDateTime(3, c.getEndDate());
setDate(java.sql.Date) seems like what you're looking for:
query.setDate(2, new java.sql.Date(c.getStartDate().getMillis());
In your PreparedStatement, call the toDate() method on your DateTime objects.
If you are not interested in times, you might as well change your database columns to DATE.

Retrieving MySQL time in java

I am running the following query to get time difference of two times
resultSet = statement.executeQuery("SELECT TIMEDIFF('14:03:55.256', '14:02:51.780') AS td");
MySQL gives time difference in this format
00:01:03.476
But both
resultSet.getTime("td"); and resultSet.getObject("td");
returns 00:01:03
According to the documentation getTime(String string) retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Time object in the Java programming language.
java.sql.Time corresponds to SQL TIME and contains information about hour, minutes, seconds and milliseconds.Then why am I getting 00:01:03 instead of 00:01:03:476?
What I basically want is to store 00:01:03.476 into a String. Is there any workaround or I am doing it wrong?
If you are verifying the result by printing it out note that java.sql.Time.toString() only returns a string in the format hh:mm:ss
You should really use rs.getTimestamp which will return a java.sql.Timestamp
Try to do this:
String s = new SimpleDateFormat("HH.mm.ss.SSS").format(t.getTime());
where t is your variable of type java.sql.Time.
Hope this will solve you problem.

Categories