query different in java that in mysql [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySql datetime not returning time
If I do this from consele mysql:
SELECT CREATION from MYDATABASE WHERE NAME='MyData';
I get 2012-07-12 13:42:55
but if do this from Java:
String sql = "SELECT CREATION from MYDATABASE WHERE NAME=?";
PreparedStatement ps = null;
String creationQuery;
ResultSet rs = null;
try
{
ps = conn.prepareStatement(sql);
ps.setString(1, "MyData");
rs = ps.executeQuery();
creationQuery = rs.next() ? rs.getString(1) : null;
}
I get 2012-07-12 13:42:55.0
Why?

It has to do with the fact that what you receive from your Java query is a java.sql.Timestamp, which is a wrapper to a regular util.Date that also holds nanoseconds.
If you look at the toString() method of that class you see that it is overridden to use the format
yyyy-mm-dd hh:mm:ss.fffffffff
where
fffffffff
represent nanoseconds.
Both results represent the same time though, it is just a difference in representation.

Related

Why is my result set in java reading my dbeaver column for date as null and how can I work around this? [duplicate]

This question already has answers here:
Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2
(1 answer)
Getting the date from a ResultSet for use with java.time classes
(3 answers)
Closed 3 years ago.
Why is my result set in Java reading my DBeaver column for date as null and how can I work around this?
Below you can see photos showing that Java is reading my date column from DBeaver as null when that is not the case, as well as my prepared statement and result set in Java.
public Set<Reimbursement> getReimbursementsForEmployee(Employee employee) {
Connection conn = ConnectionUtil.getConnection();
String sql = "SELECT * FROM reimbursement WHERE e_id = ?";
Set<Reimbursement> reimbursements = new HashSet<Reimbursement>();
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, employee.getEmployeeId());
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Reimbursement reimbursement = new Reimbursement();
reimbursement.setReimbursementNumber(rs.getInt("reimbursementnumber"));
reimbursement.setDate(rs.getDate("ddate"));
reimbursement.setAmount(rs.getDouble("amount"));
reimbursement.setReimbursementRequestInfo(rs.getString("reimbursementrequestinfo"));
reimbursement.setManagerComment(rs.getString("managercomment"));
reimbursement.setStatus(rs.getString("status"));
reimbursement.setEmployeeId(rs.getInt("e_id"));
reimbursement.setManagerId(rs.getInt("m_id"));
reimbursements.add(reimbursement);
}
return reimbursements;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

Getting a Date datatype in mysql [duplicate]

This question already has answers here:
How to process 0000-00-00 date in jdbc MySQL query
(5 answers)
Value '0000-00-00' can not be represented as java.sql.Date
(6 answers)
Closed 8 years ago.
I'm trying to get a Date datatype from my database in SQL.
But I get this error
java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date
ResultSet rs = stmt.executeQuery("select movie_id, movie_name, movie_dateBorrowed from movie_tbl where movie_borrower = '"+userUserID+"'");
if(rs.next()){
String moviename = rs.getString("movie_name");
Date moviedateborrowed = rs.getDate("movie_dateBorrowed");
if(movieDB!="0000-00-00"){
Date moviedateborrowed = rs.getDate("movie_dateBorrowed");
dates.add(moviedateborrowed);
}
else{
JOptionPane.showMessageDialog(null, "Unable.");
}
}
Any help would make my day. Thanks
PS. I'm using Netbeans as my IDE.
for Derby the smallest value for the Date == 0002-11-30
other Databases may have another limit.
Test it like
if(rs.next()){
String moviename = rs.getString("movie_name");
movies.add(moviename);
String DateStr = rs.getString("movie_dateBorrowed");
if (DateStr != "0000-00-00") {
Date moviedateborrowed = rs.getDate("movie_dateBorrowed");
dates.add(moviedateborrowed);
} else ...
...
}
in the else statement you can use dates.add to add 0002-11-30 (Derby)

taking timestamp from resultSet [duplicate]

This question already has answers here:
ResultSet exception - before start of result set
(6 answers)
Closed 5 years ago.
I am trying to execute the SQL query using java like
Statement st = UserDBConn.createStatement();
ResultSet dummy = st.executeQuery("select unix_timestamp('2012-03-07 00:00:00')");
How to take the timeStamp value from this resultSet. please correct me if am doing anything wrong.
Note : I tried the same query in mysql, it gives 1331058600.
Thanks. I tried
**ResultSet result = st.executeQuery("select unix_timestamp('2012-03-07 00:00:00') as theTime");
Timestamp time = result.getTimestamp("theTime");**
It throws "Before start of result set". What may be the error.
Exception is as below
java.sql.SQLException: Before start of result set at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:3628) at com.mysql.jdbc.ResultSet.getString(ResultSet.java:1767) at com.mysql.jdbc.ResultSet.getTimestampInternal(ResultSet.java:3792) at com.mysql.jdbc.ResultSet.getTimestamp(ResultSet.java:1905) at com.mysql.jdbc.ResultSet.getTimestamp(ResultSet.java:1919) at DBCount.main(DBCount.java:31)
Hope below is what you want...
while (dummy.next()) {
System.out.println(rs.getString(1));
}
Update 1
Change your query to below.
ResultSet result = st.executeQuery("select unix_timestamp('2012-03-07 00:00:00') as theTime");
result.next();
Timestamp time = result.getTimestamp("theTime");
You will get what you want...
If all you're asking is how to get the timestamp from the resultset you use the getTimestamp method of the resulset. You may be able to use, "unix_timestamp" as the column label but if not change your query to something like st.executeQuery("select unix_timestamp('2012-03-07 00:00:00') as theTime"); and use "theTime" as the column label
dummy.getTimestamp("columnLabel");
I would do something like this: Set an alias to my select and use ResultSet.getTimestamp() method to retrieve the timestamp.
Statement st = UserDBConn.createStatement();
ResultSet dummy = st.executeQuery("select unix_timestamp('2012-03-07 00:00:00') as ts");
while (dummy.hasNext()) {
Timestamp ts = dummy.getTimestamp("ts");
}

Convert Java Timestamp to MySQL timestamp vice versa

How can I Convert Java Timestamp (Timestamp data type) to MySQL timestamp vice versa?
If you're using the JDBC API to access the database, and you're using a PreparedStatement to for example execute an SQL INSERT statement, then you just set the timestamp as a parameter to the PreparedStatement:
Timestamp ts = ...; // wherever you get this from
PreparedStatement ps = connection.prepareStatement("INSERT INTO MYTABLE (ts) VALUES (?)");
ps.setTimestamp(1, ts);
ps.executeUpdate();
Likewise, when you're doing a query that returns a timestamp, get it from the ResultSet by calling getTimestamp on it. Example:
Timestamp result = null;
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT ts FROM MYTABLE WHERE ...");
if (rs.next()) {
result = rs.getTimestamp(1);
}
See the JDBC Tutorial.
Without more specifics on the trouble you are having, this will be a hard question to answer. However, Java makes this relatively straightforward if you are using prepared statements. Your code would look something like this:
Connection conn = getConnection();
PreparedStatement pStmt = conn.prepareStatement("UPDATE my_table SET my_column = ? WHERE id = ?");
pStmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pStmt.setInt(2, 42);
pStmt.executeUpdate();
As far as I can see from MySQL docs, java.sql.Timestamp should just work if you read it from or write it to a TIMESTAMP field in the database. So you should not need to do any conversion.

JDBC Problem wth select where query [duplicate]

This question already has answers here:
ResultSet exception - before start of result set
(6 answers)
Closed 5 years ago.
My Code:
con = jdbc.connect();
System.out.println("status of con"+con);
String query = " select * from BmgCPUTotalUsage where CPUserID='"+CPUserID+"' and MessageTypeID='"+MessageTypeID+"'";
System.out.println(query);
PreparedStatement statement = con.prepareStatement(query);
ResultSet result = statement.executeQuery();
System.out.println(result);
int QuotaUsed=result.getInt("QuotaUsed");
Output:
status of concom.mysql.jdbc.JDBC4Connection#182600f
select QuotaUsed from BmgCPUTotalUsage where CPUserID='msdp' and MessageTypeID='SMS'
com.mysql.jdbc.JDBC4ResultSet#16e1dd8
java.sql.SQLException: Before start of result set.
Can anybody please say me how can I remove this exception.and fetch the QuotaUsed from the table.where QuotaUsed is an integer.
You really need to do more reading of the JDBC Tutorial, but briefly, try this:
ResultSet resultSet = statement.executeQuery();
if (resultSet.next() {
int quotaUsed = resultSet.getInt("QuotaUsed");
}

Categories