This question already has answers here:
LocalDateTime and SQL Server JDBC 4.2 driver
(4 answers)
Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2
(1 answer)
Closed last year.
I was looking for a solution to my problem. I was coding my JdbcImpl file and I got an error when I was coding this :
rs.getDate create an error in a screen
Enchere enchereResultSet = new Enchere(
//Changer le getDate to LocalDateTime
rs.getDate("date_encheres"),
rs.getInt("montant_enchere"),
rs.getInt("no_article"),
rs.getInt("no_utilisateur"));
I found the fix! It seems simple, just go for this instead:
Enchere enchereResultSet = new Enchere(
//Changer le getDate to LocalDateTime
rs.getTimestamp("date_enchere").toLocalDateTime(),
rs.getInt("montant_enchere"),
rs.getInt("no_article"),
rs.getInt("no_utilisateur"));
The solution in a screen
Do I have the right to share the fix if I found it while writing a post here? I share it because it will maybe help beginner like me in the future.
Related
This question already has answers here:
MongoDB GUI client (cross-platform or Linux) [closed]
(5 answers)
Closed 3 years ago.
I need to perform the following MongoDB query in Java:
db.ventas.aggregate([
{
$sort: {"codArt._id": -1}
},
{
$group:{
_id: "$codArt._id",
denominacion: {"$first": "$codArt.denominacion"},
unidades: {"$sum": "$unidades"},
importe: {"$first": {"$multiply": [{"$sum": "$unidades"}, "$codArt.pvp"]}},
stock: {"$first": {"$subtract": ["$codArt.stock", "$unidades"]}}
}
}
])
Is there any library, which can do it?
Unfortunately, I can't install MongoDB Compass.
You could use something like Hibernate, but no automatic translation is provided... you'll need to build your model. It will be useful though if you do not want to build the inner query or need some portability.
http://hibernate.org/ogm/
There is a great tool to learn MongoDB Java Syntax and to work with MongoDB in general: Studio 3T, there you can generate a MongoDB query for the Java driver. Trial 30 days.
P.S. I'm not sure if this software requires admin rights to be installed.
This question already has answers here:
How do I store a string longer than 4000 characters in an Oracle Database using Java/JDBC?
(4 answers)
Closed 4 years ago.
I am facing problems to store CLOB data through JDBC to Oracle 9i database. I am using PreparedStatement and setting the data as setString(index, value). The value is a large XML string. The driver I am using is ojdbc14.jar. Still I am getting an error saying " java.lang.Exception: Data size bigger than max size for this type: 4723".
My XML is not so big that it will not fit in CLOB field. Still getting this error. Please help.
I searched various sites it it did not solve the problem, the setClob() method is not supported by ojdbc14 driver.
The XML probably isn't too big for a CLOB (4GB!) but is for using setString (4000 characters). The textbook solution would probably be to use the setCharacterStream method instead:
PreparedStatement ps = ...;
String xml = "<xml>somve verly long data... </xml>";
ps.setCharacterStream(1, new StringReader(xml), xml.length());
I have an SQL which looks into a dimension table (which stores every dates until year 2020) and then shall retrieve the todays row.
I watched into the table, todays date is in there.
The problem is, that SQL does not return any result.
I am thinking of a problem related to the use of java.sql.PreparedStatement.setDate method.
In past i think this was working fine, now I did some kine of regression test and it failed. The differences to the past are having Oracle 12 DB now instead of 11 in past and running it on CentOS 6.5 instead of AIX.
On search I found this topic here:
Using setDate in PreparedStatement
As far as I can see, I am doing as suggested.
Heres the java code and the query:
public static String SELECT_DATUM = "SELECT TIME_ID, DATE, DAY_NAME, WEEK_NAME, MONTH_NAME, YEAR_NAME, SORTING, RELATIONDATE, VALID_TO, VALID_FROM FROM DIM_TIME WHERE DATE = :date";
java.util.Calendar now = Calendar.getInstance();
now.clear(Calendar.HOUR);
now.clear(Calendar.MINUTE);
now.clear(Calendar.SECOND);
now.clear(Calendar.MILLISECOND);
Date tmpDate = now.getTime();
Date tmpDate2 = new Date(((java.util.Date)tmpDate ).getTime());
statement.setDate(1, tmpDate2 );
I notice that getTime() is called twice. But I dont think its that bad.
I also noticed some displaying formats:
in Database the date-colums shows me the date like this: '08.11.2015'
in java while debugging tmpDate2 shows me a date like this: '2015-11-08'
in java while debugging tmpDate shows me a date like this 'Sun Nov 08 12:00:00 CET 2015'
But again, these are just display formattings while it is a dateobject in background and a date-type in database. I would expect that je JDBC driver would map this itself without formattings, that why we are using setDate method and not setString.
What am I doing wrong? What could I do for further debugging to get it?
I would like see the resulting SQL query which is finally executed with the parameter.
I tried this sql on db isntance:
SELECT * FROM v$sql s WHERE s.sql_text LIKE '%select time%' ;
but only getting this then: "... where date = trunc(:1 )"
On this row at least I can see that it was using the right schema I expected it to use and where I checked whether todays date is available.
Edit:
something I found out:
I saw another code using the same function but giving an GregorianCalendar instead Calendar. When using
new GregorienCalandar();
instead of
Calendar.getInstance();
Theres no difference.
But when I assign a date and dont let the system take the current time, then it works:
Using
new GregorianCalendar(2015, Calendar.NOVEMBER, 8);
Would retrieve the row I want from SQL.
Zsigmond Lőrinczy posted this answer as comment:
Try this: SELECT TIME_ID, DATE, DAY_NAME, WEEK_NAME, MONTH_NAME,
YEAR_NAME, SORTING, RELATIONDATE, VALID_TO, VALID_FROM FROM DIM_TIME
WHERE DATE = TRUNC (:date) – 3 hours ago
This works for my problem.
I am writing this as reponse to check it later as answer on this question if hes not going to write his own response (to get the reputation-points).
But I am wondering how I could get the same by preparing on java.
The code uses the clear-methods, which where released into an own method named 'trunc'. I think the programmer intendet to do this instead of TRUNC in SQL. I am wondering if it werent possible to do so in java and if yes, how?
Edit:
And I am wondering why a TRUNC is needed at all. Because the column in Database is of type Date an not Timestampt. So wouldnt there be an automatically trunc? I would expect this. Why do I need a trunc on SQL?
This question already has answers here:
Can I query MongoDB ObjectId by date?
(13 answers)
Closed 7 years ago.
How do you query time based queries from the ObjectID.timestamp()?
db.myCollectin.findOne()._id.getTimestamp()
I've tried
Date date = new Date();
BasicDBObject query = new BasicDBObject("timestamp", new BasicDBObject("$lt", date);
myCollection.findOne(query);
Problem:
Doesnt work
It doesn't work because your query looks for a field called timestamp which naturally doesn't exist.
You could do something like the shell query below, but be aware that Mongo will evaluate the JavaScript for every document in your collection - without using indices. I would recommend storing the date in a field on the documents if you need to query it regularly.
db.myCollection.find({$where: "this._id.getTimestamp() < ISODate('2015-03-04T21:18:21.419Z')"})
I have a column in my table with timestamp datatype and the value for instance look like '2014-08-30 00:00:50'. From database point of view I know it's showing the time of 12:00:50 AM for '00:00:50' and 12:00:50 PM if the value is 2014-03-30 12:00:50. But When I process the data in my application by fetching the table values, I'm getting AM for the time which has to be PM. I want to know how could I achieve this? I also want to know that the problem weather it is from my application side ? or database side? Could I achieve this If I get time from database as unixtimestamp format?
I've seen several questions related to this but I found none of them led me to solve this issue. Please help
If you are getting the date as a string you can do the following to convert it to AM/PM.
String s = "2014-08-30 00:00:50";
SimpleDateFormat d = new SimpleDateFormat("y-MM-dd H:mm:s");
Date in24HrFormat = d.parse(s);
String in12HrFormat = new SimpleDateFormat("y-MM-dd h:mm:sa").format(in24HrFormat);
System.out.println(in12HrFormat);