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");
}
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);
}
}
This question already has answers here:
SQL 1064 Syntax Error using a JDBC prepared statement
(3 answers)
Closed 6 years ago.
I'm trying to run a prepare statement and get an error in MySQL syntax. Not sure what is the error?
Code:
PreparedStatement stmt = null;
conn = ds.getConnection();
String query = "select distinct FName from PRL p1, RequestView p2 where p1.RequestId = p2.RequestId and p1.PackageId = p2.PackageId and p1.ProductId = ? and p1.ProductNumber = ? and p1.Version = ? order by p2.ArtName";
stmt = conn.prepareStatement(query);
stmt.setString(1,id);
stmt.setString(2,num);
stmt.setString(3,ver);
rs = stmt.executeQuery(query);
Error:
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? and p1.ESTProductMatNumber =? and p1.Version =? order by p2.ArtworkName' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
Never mind. I figured it out. rs = stmt.executeQuery(query); has to be rs = stmt.executeQuery();
This question already has answers here:
right syntax to use near '?'
(3 answers)
Closed 7 years ago.
I'm trying to debug my prepared statement in java and I'm stuck on this checkEmail function that I implemented. When I go into debugging and it reaches the setString line, it shows NOT SPECIFIED in place of the '?'. If I hardcode 'findEmail' into the String query it will work and find the email. Here is the piece of code:
public static boolean checkEmail(String findEmail) {
Connection conn = EstablishConnection.conn;
boolean found = false;
try {
String query = "SELECT email FROM customers WHERE email=?";
Logging.debug(query);
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1,findEmail);
ResultSet rs = preparedStatement.executeQuery(query);
//Iterate through the results of the query
if (rs.next()) {
found = true;
}
preparedStatement.close();
} catch (Exception e) {
Logging.debug("Exception thrown in CustomerOperations.getCustomerInfo(): " + e.getMessage());
e.printStackTrace();
}
return found;
}
Try to replace this :
ResultSet rs = preparedStatement.executeQuery(query);
With:
ResultSet rs = preparedStatement.executeQuery();
Because you had already pass the query to prepareStatement : conn.prepareStatement(query);
This question already has answers here:
MySQLSyntaxErrorException when trying to execute PreparedStatement [duplicate]
(2 answers)
Closed 7 years ago.
I am trying to pass string array and int array to database. Code I am trying is below, but this always shows the error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?)' at line 1
Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/javabase", "java", "password");
CompareAndCount compareAndCount = new CompareAndCount();
ReadWord bagOfWords = new ReadWord();
String[] listOfWords = bagOfWords.openFile();
int[] count =compareAndCount.compareWords();
for (int i = 0; i < listOfWords.length; i++) {
String query = "INSERT INTO angry(tagwords,termfrequency) VALUES(?,?)";
PreparedStatement pStmnt = (PreparedStatement) connection.prepareStatement(query);
pStmnt.setString(1, listOfWords[i]);
pStmnt.setLong(2, count[i]);
pStmnt.executeUpdate(query);
}
connection.close();
Replace
pStmnt.executeUpdate(query);
by
pStmnt.executeUpdate();
And prepare the statement once, before the loop, instead of re-preparing it again and again at each iteration.
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");
}