This question already has an answer here:
SQLException: the result set is closed
(1 answer)
Closed 3 years ago.
I didn't find the reason for a SQLExeption, I got it at while(dbrs.next()). It is simple, it was working, but after update to Oracle Server 19, I got this error. I got still a result set in Oracle Developer with the same account.
Statement st = conn.createStatement();
if (Oracle){
ResultSet dbrs = st.executeQuery("select table_name from all_tables");
while(dbrs.next()){
if (dbrs.getString(1).equals(G_IN)){
st.execute("DROP TABLE "+G_IN);
System.out.println(G_IN+" gelöscht");
}
}
dbrs.close();
}else{
String loeschen="DROP TABLE IF EXISTS \"" + G_IN +"\"";
System.out.println(loeschen);
st.execute( loeschen );
}
You shouldn't reuse a Statement object as is the case in your while loop. To execute a new query, you need to use a new Statement object.
Replace
st.execute("DROP TABLE "+G_IN);
with
conn.createStatement().execute("DROP TABLE "+G_IN);
and it should work.
Related
This question already has answers here:
Get value from a ResultSet by name
(2 answers)
Closed 1 year ago.
I am trying to set a string value into DB using prepared statement and Result Set. But it inserts into DB row this "com.mysql.cj.jdbc.result.ResultSetImpl#fcd6521"
PreparedStatement check = connection.prepareStatement(
"SELECT buildingName FROM building GROUP BY buildingId HAVING buildingId = ?");
check.setInt(1, building_id);
ResultSet rs = check.executeQuery();
insert.setString(1, String.valueOf(rs));
insert.executeUpdate();
I think that you need to read the tutorial indicated by #user16320675
A ResultSet is a type of set. It has to be accessed like that:
//if you expects receive only one buildName you use a simple if, otherwise you have to use a loop and a list or something like that
String text;
if(rs.next()){
text = rs.getString(0);
}
//index it's related to buildingName
This question already has answers here:
Getting java.sql.SQLException: Operation not allowed after ResultSet closed
(2 answers)
Closed 4 years ago.
Can anyone point me in the write direction to solve the error: SQLException: Operation not allowed after ResultSet closed
Here is the code i believe the error occurs in
Statement stmt = con.createStatement();
String query1 = "";
String query2 = "";
int candidateNo = 42;
query1 = "SELECT * FROM Candidates WHERE CandidateNo = "+ candidateNo;
ResultSet rs = stmt.executeQuery(query1);
//If query can be completed then display name and prompt to continue
if(rs.next()){
query2 = "SELECT * FROM Achieved WHERE CandidateNo = " + candidateNo;
ResultSet ts = stmt.executeQuery(query2);
}
I have tried using resultset rs = null; to create the variable but the error still occurs
You are iterating over the ResultSet bound to a Statement , then while iterating, you are using the exact same Statement object to issue a new query and get and iterate over another ResultSet .
This won't work as long as you are not done with the processing of the first ResultSet, so consider using a distinct Statement object for your second query .
In your code you used 2 ResultSet executing the same Statement object (stmt)concurrently. Use different Statement Objects for both queries.
This question already has answers here:
ResultSet exception - before start of result set
(6 answers)
Closed 5 years ago.
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname","user","pass");
Statement myStmt1 = myConn.createStatement();
System.out.println("connected");
for(int i=1; i<1375462;i++){
ResultSet myRs1 = myStmt1.executeQuery("SELECT Name FROM table WHERE id ="+i);
String Name = myRs1.getString("Name");
System.out.print(i);
System.out.println("Name:"+Name);
}
I'm using JDBC Java to query the name from database but this time it got an error and I don't know why. I have used Java JDBC before. I can connect to database but the query seem like not working? Id column is integer 10 digit.
"run: connected java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:959)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:862)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:790)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5244)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5167)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5206)"
This kind of error appears when you try to access something without moving cursor. Try to put myRs1.next(); before getting String.
Inside for loop, when getting the ResultSet, you need a check myRs1.next() as described below.
for(int i=0; i<1;i++){
ResultSet myRs1 = myStmt1.executeQuery("SELECT Name FROM table WHERE id ="+i);
while(myRs1.next()) {
String Name = myRs1.getString("Name");
System.out.print(i);
System.out.println("Name:"+Name);
}
}
This question already has answers here:
How to get a value from the last inserted row? [duplicate]
(14 answers)
Closed 9 years ago.
I am using the following code to insert a new row to database, I need to get the id of the last inserted row but when I run the code it shows the following message:
SEVERE: java.sql.SQLException: Generated keys not requested. You need to specify
Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or
Connection.prepareStatement().
When I use the following code also it gives error although I choose executeUpdate(String sql)
ps.executeUpdate(ps.RETURN_GENERATED_KEYS);
error >> no suitable method found for executeUpdate(int)
the table is as following:
credential
int ID primary key, auto increment
varchar(10) name
my code
String Insert_Credential = "Insert into credential values("
+ "?,?)";
ps = con.prepareStatement(Insert_Credential);
ps.setInt(1, 0);
ps.setString(2, "username");
ps.executeUpdate();
ResultSet generatedKeys = ps.getGeneratedKeys();
if (generatedKeys.next()) {
System.out.println("id is"+generatedKeys.getLong(1));
return generatedKeys.getInt(1);
} else {
throw new SQLException("Creating user failed, no generated key obtained.");
}
ps.executeUpdate(ps.RETURN_GENERATED_KEYS)
You invented that. It doesn't exist.
ps = con.prepareStatement(Insert_Credential);
That doesn't tell the PreparedStatement to return generated keys either. You need this:
ps = con.prepareStatement(Insert_Credential, Statement.RETURN_GENERATED_KEYS);
This question already has answers here:
How to get the insert ID in JDBC?
(14 answers)
Closed 7 years ago.
My current method is this:
SELECT TOP 1 ID FROM DATAENTRY ORDER BY ID DESC
This assumes the latest inserted item always has the highest unique ID (primary key, autoincrementing). Something smells wrong here.
Alternatives?
If the JDBC driver supports it, you can also just use Statement#getGeneratedKeys() for that.
String sql = "INSERT INTO tbl (col) VALUES (?)";
preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, col);
preparedStatement.executeUpdate();
generatedKeys = preparedStatement.getGeneratedKeys();
if (generatedKeys.next()) {
long id = generatedKeys.getLong(1);
} else {
// Throw exception?
}
If using MySQL you can do
select last_insert_id();
If using MS SQL
select scope_identity();
For H2, I believe it's
CALL SCOPE_IDENTITY();
but I don't have any experience with that DB