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");
}
Related
This question already has answers here:
ResultSet: Exception: set type is TYPE_FORWARD_ONLY -- why?
(9 answers)
Closed 20 days ago.
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop?autoReconnect=true&useSSL=false","root",DatabaseConnection.root);
PreparedStatement ps=con.prepareStatement("select * from custinfo");
ResultSet rs=ps.executeQuery();
/*******
//for counting the number of rows in result set
if(rs.last()) {
x=rs.getRow();
rs.beforeFirst();
}
When I'm trying to execute it it showing me the error:
java.sql.SQLException: Operation not allowed for a result set of type
ResultSet.TYPE_FORWARD_ONLY.
Can you help me how can I solve this?
The error message says it all - the ResultSet is of TYPE_FORWARD_ONLY, so you can't scroll it back with beforeFirst(). Having said that, querying the entire table (and having to send all that data from the database to your application) just to get its size is probably not a good practice. It would be much easier to use an aggregate query to get the number of rows:
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop?autoReconnect=true&useSSL=false","root",DatabaseConnection.root);
PreparedStatement ps = con.prepareStatement("select count(*) from custinfo");
ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
size = rs.getInt(1);
}
}
After getting results from a SQL query, I can access the results via a while loop :
String query = "SELECT TAX_RATE_ID FROM dbo.TAX_RATE where zip_code = 38401";
ResultSet rs= conn.createStatement().executeQuery(query);
while (rs.next()) {
System.out.println(rs.getString("TAX_RATE_ID"));
}
But I don't want to access them in a while loop, I want to just get the single result and return the value, since I know what to expect when the query is ran. So I tried this:
String query = "SELECT TAX_RATE_ID FROM dbo.TAX_RATE where zip_code = 38401";
ResultSet rs= conn.createStatement().executeQuery(query);
rs.first();
System.out.println(rs.getString("TAX_RATE_ID"));
But I get this error:
Exception in thread "main" java.sql.SQLException: ResultSet may only be accessed in a forward direction.
at net.sourceforge.jtds.jdbc.JtdsResultSet.checkScrollable(JtdsResultSet.java:319)
at net.sourceforge.jtds.jdbc.JtdsResultSet.first(JtdsResultSet.java:545)
at com.kirklands.automation.ecom.Testing.main(Testing.java:12)
You don't have to call next() in a loop. If you just want to get the first row, use:
String query = "SELECT TAX_RATE_ID FROM dbo.TAX_RATE where zip_code = 38401";
ResultSet rs= conn.createStatement().executeQuery(query);
if (rs.next()) {
System.out.println(rs.getString("TAX_RATE_ID"));
}
Unrelated, but:
conn.createStatement().executeQuery() will create a Statement instance that you can't clean up properly using close().
here is the code there is catch close but i delete it so can anyone replay to me
String id = null;
String root="root",student="root";
String name=jTextField1.getText();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection
con=(Connection)DriverManager.getConnection
("jdbc:mysql://localhost:3306
/dijlafinal1",root,student);
String query2="SELECT * FROM name WHERE name like ? ";
PreparedStatement pstm2=null;
pstm2=con.prepareStatement(query2);
pstm2.setString(1,"%"+name+"%");
ResultSet rs = pstm2.executeQuery(query2 );
while (rs.first()){
String name1=rs.getString("name");
id= rs.getString("id");
int epo =rs.getInt("epo");
}
jTextField2.setText(id);
}
You need to use the no-argument version of executeQuery(), i.e.
ResultSet rs = pstm2.executeQuery();
The reason is that you've already prepared the statement when you called con.prepareStatement(query2). Calling executeQuery(query2) will throw away the prepared SQL and execute the query without bind variables, leaving the ? in place -- as the error message suggests.
When working with a PreparedStatement you always call the .execute... methods without any arguments because you have already supplied the SQL command text with the .prepareStatement call. So
ResultSet rs = pstm2.executeQuery(query2 );
is incorrect. You need to simply do
ResultSet rs = pstm2.executeQuery();
I wonder if it might work if you use this?
pstm2.setString(1,"'%"+name+"%'");
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.
How to write a Java prepared statement for MySQL so that it would pick a row containing the highest Timestamp value from a column that would be greater from given parameter?
I got as far as:
SELECT * FROM table WHERE timestamp_col=(SELECT MAX(timestamp_col) FROM table)
But how to convert it to prepared statement?
Since there's not really a parameter, it's pretty simple:
Connection con = null;
PreparedStatement prest;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://<server>:3306/<db>","<login>","<pass>");
try{
String sql = "SELECT * FROM table WHERE timestamp_col=(SELECT MAX(timestamp_col) FROM table)";
prest = con.prepareStatement(sql);
// prest.setInt(1,12000); // example of using a parameter for prepared statement
// parameters go in the SQL as ?
ResultSet rs1 = prest.executeQuery();
} catch( SQLException s ) { /* etc */ }
You don't need a parameter if you're just asking for a MAX(). It's perfectly safe.