This question already has answers here:
When is ResultSet closed?
(3 answers)
Closed 2 years ago.
I cant not do the further process because I get ResultSet closed while running this code. I am adding the value after 15 row of the sqlite database table and I want to find average value of 15 row and that should be store in ArrayList.
Here is the code:-
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully3");
stmt = c.createStatement();
rs = stmt
.executeQuery("SELECT TIME,BID,ASK FROM '" + title + "' ;");
System.out.println("Opened database successfully 20");
while (rs.next()) {
i++;
System.out.println(i);
if (i > 15) {
System.out.println("i am in");
List<String> stocklist = new ArrayList<String>();
String main = rs.getString(1);
System.out.println(rs.getString(1));
String s[] = main.split(" ", 2);
String date = s[0];
String time = s[1];
stocklist.add(date);
stocklist.add(time);
stocklist.add(df.format(rs.getFloat("BID")));
stocklist.add(df.format(rs.getFloat("ASK")));
rs1 = stmt
.executeQuery("SELECT ASK FROM '" + title + "' ;");
int j = 1;
while (rs1.next()) {
if (j < i) {
System.out.println(rs1.getFloat("ASK"));
avg = avg + rs1.getFloat("ASK");
}
j++;
}
rs1.close();
System.out.println("i am out");
avg = avg / 15;
changepercent = ((rs.getFloat("ASK") - avg) / avg) * 100;
stocklist.add(df.format(changepercent));
stocklist.add("ETE");
stocklist.add("HUN");
stocklist.add("ALU");
stocklist.add("ETE");
stocklist.add("HUN");
stocklist.add("ALU");
stocklist.add("ETE");
stocklist.add("HUN");
hntm.addRow(stocklist);
}
}
rs.close();
stmt.close();
c.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
you should not reuse a Statement. When you create a new Query, you need so use a new Statement Object. Replace rs1 = stmt.executeQuery("SELECT ASK FROM '" + title + "' ;"); with rs1=c.createStatement().executeQuery("SELECT ASK FROM '" + title + "' ;");
This is not the answer for this question, but i was also having problem with java.sql.SQLException: ResultSet closed.
In my case the result from the query was empty and i was using SQLite, so the ResultSet returned closed.
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
// resultSet closed == didn't find anything
if(resultSet.isClosed())
return false;
I tested using PostgreSQL and ResultSet stay open even when query is empty. I didn't expect different behavior from different databases.
If anyone want to check my test: https://gist.github.com/thiagola922/fcd80f0f29b83106304c3b64cf64b2b1
As #marquis-of-lorne said, a better way to check if the connection still open is using resultSet.next().
Related
Every time I run this code it gives me an exhauset resultset error. Im not sure what Im doing wrong but Ive tried removing the .next(); code for either one or all resultsets and then the error given is the ResultSet next wasnt called.
Im not sure what Im doing wrong. Just curious what people might think the issue could be? Ive done similar earlier in my servlet code but only used 1 statement and then 1 prepared statement. This time Im using 2 statements and 1 prepared statement.
String opt1 = req.getParameter("RecName"); //Retrieves info from HTML form
String ingr1 = req.getParameter("Ing1"); //Retrieves info from HTML form
stmt = con.createStatement();
stmt1 = con.createStatement();
ResultSet rs11 = stmt.executeQuery("SELECT recipe_ID FROM GM_Recipes WHERE rec_name='" + op1 + "'"); //choose recipe_ID from sql table
rs11.next();
ResultSet rs12 = stmt.executeQuery("SELECT ingredient_ID FROM GM_IngredientDB WHERE ing_name='" + ingr1 + "'"); //choose ingredient_ID from sql table
rs12.next();
int olo = ((Number) rs11.getObject(1).intValue(); //convert resultset value to int
int olo1 = ((Number) rs11.getObject(1).intValue(); //convert resultset value to int
PreparedStatement pstmt1 = con.prepareStatement("INSERT INTO GM_RecLnk(recipe_ID,ingredient_ID) VALUES (?,?)");
pstmt1.clearParameters();
pstmt1.setInt(1,olo);
pstmt1.setInt(2,olo1);
ResultSet rs1 = pstmt1.executeQuery();
rs1.next();
Some ideas on your code (in comments)
stmt = con.createStatement();
stmt1 = con.createStatement();
ResultSet rs11 = stmt.executeQuery("SELECT recipe_ID FROM GM_Recipes WHERE rec_name='" + op1 + "'"); //choose recipe_ID from sql table
//Check if you HAVE a line here!
if(!rs11.next()) {
System.out.println("No Recipe Found");
}
//Use stmt1 - that's why you created it?!
ResultSet rs12 = stmt1.executeQuery("SELECT ingredient_ID FROM GM_IngredientDB WHERE ing_name='" + ingr1 + "'"); //choose ingredient_ID from sql table
if(!rs12.next()) {
System.out.println("No Ingredient Found");
}
int olo = ((Number) rs11.getObject(1).intValue(); //convert resultset value to int
//Read Ingredient from rs12 -> that's where you selected it into
int olo1 = ((Number) rs12.getObject(1).intValue(); //convert resultset value to int
While this might point you into the right direction for solving the current issue, you should consider learning about clean code.
Consider this code making use of try-with-resource, refactored out some methods, using prepared statements.
//Replace exiting code
String opt1 = req.getParameter("RecName"); //Retrieves info from HTML form
String ingr1 = req.getParameter("Ing1"); //Retrieves info from HTML form
int recipieId = getRecipeId(con, opt1);
int ingredientId = getIngredientId(con, ingr1);
if(recipeId > 0 && ingredientId > 0) {
//Process result
insertRecLnk(con, recipeId, ingredientId);
} else {
System.out.println("No INSERT");
}
//Helper functions
protected int getRecipeId(Connection con, String rec) {
try(PreparedStatement st = con.prepareStatement("SELECT recipe_ID FROM GM_Recipes WHERE rec_name=?")) {
st.setString(1, rec);
try(ResultSet rs11 = st.executeQuery()) {
//choose recipe_ID from sql table
if(rs11.next()) {
return rs11.getInt(1);
}
}
} catch(SQLException e) {
e.printStackTrace();
}
System.out.println("No Recipe Found");
return -1;
}
protected int getIngredientId(Connection con, String ing) {
try(PreparedStatement st = con.prepareStatement("SELECT ingredient_ID FROM GM_IngredientDB WHERE ing_name=?")) {
st.setString(1, ing);
try(ResultSet rs11 = st.executeQuery()) {
//choose recipe_ID from sql table
if(rs11.next()) {
return rs11.getInt(1);
}
}
} catch(SQLException e) {
e.printStackTrace();
}
System.out.println("No Ingredient Found");
return -1;
}
protected void insertRecLnk(Connection con, int rId, int iId) {
try(PreparedStatement ps = con.prepareStatement("INSERT INTO GM_RecLnk(recipe_ID,ingredient_ID) VALUES (?,?)")) {
ps.setInt(1, rId);
ps.setInt(2, iId);
ps.executeUpdate();
} catch(SQLException e) {
e.printStackTrace();
}
}
I'm working on shifts manager program which calculates monthly salary and etc.
the program based on SQLite database which keeps getting updated by the user input.
my question is , how can i use the SQLite function in java to retrieve information, lets say monthly salary in one command (i know i can use " select sum(tips) between date1 and date2",but how can i get the function result inside a variable?)
so far i've created a function which gets two dates and retrieves all the shifts salary between these dates and summarise them with ResultSet.
here's my code:
public static String tipsMade(String date1, String date2){
Connection c = null;
Statement stmt = null;
ResultSet rs = null;
String ans= null;
int sum = 0;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Gil\\test.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
rs = stmt.executeQuery("select tips from shifts where fulldate between "+"'"+date1+"'"+"and " +"'"+date2+"'"+ ";");
while(rs.next()){
sum += rs.getInt("tips");
}
ans = Integer.toString(sum);
//
//close connections and etc
stmt.close();
c.commit();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
return ans;
}
I edited your code.
Note that in case you select the sum of the tips, the column name changes. You also get only one row with one column as your result, so you should not need the while loop anymore.
The sum of the tips is now saved in the variable sum
public static String tipsMade(String date1, String date2){
Connection c = null;
Statement stmt = null;
ResultSet rs = null;
String ans= null;
int sum = 0;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Gil\\test.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
rs = stmt.executeQuery("select sum(tips) from shifts where fulldate between "+"'"+date1+"'"+"and " +"'"+date2+"'"+ ";");
while(rs.next()){
sum = rs.getInt("sum(tips)");
}
ans = Integer.toString(sum);
//
//close connections and etc
stmt.close();
c.commit();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
return ans;
}
Hello and thank you for reading my post.
I have a PostgreSQL table "t" with a column "c" which type is "character varying(32)".
Values in this column look like this: "2014100605".
I am using the "MAX()" aggregate function to retrieve the maximum value in this column.
SELECT MAX(c) AS max FROM t;
In Java, if I prepare the query above, get a "resultSet" object and send it the getString("max") message, I get max = null.
If I send it the getInt("max") method instead, I get the result I'm expecting, something like "2014100605".
Is this normal behavior?
Am I really allowed to do this or is it by chance I'm getting the expected result?
Is "MAX()" actually using the lexicographical order?
Best regards.
A bit of Java code:
s_preparedSqlQuery =
"SELECT MAX(quotinv_nro) AS quotinv_nro_max "
+ "FROM imw_quotation_invoice "
+ "WHERE quotinv_type = ? "
+ "AND quotinv_nro LIKE '" + s_quotinvDate + "%'";
preparedStatement = m_connection.prepareStatement(s_preparedSqlQuery);
preparedStatement.setString(1, s_quotinvType);
resultSet = preparedStatement.executeQuery();
if(resultSet != null)
{
if(resultSet.next())
{
// s_quotinvNroMax = resultSet.getString("quotinv_nro_max");
n_quotinvNroMax = resultSet.getInt("quotinv_nro_max");
// if(s_quotinvNroMax == null)
if(n_quotinvNroMax == 0)
{
n_nbQuotinvsThisSameDate = 0;
return n_nbQuotinvsThisSameDate;
}
else
{
s_quotinvNroMax = Integer.toString(n_quotinvNroMax);
n_length = s_quotinvDate.length();
s_currentMaxNro = s_quotinvNroMax.substring(n_length - 1);
n_nbQuotinvsThisSameDate = Integer.valueOf(s_currentMaxNro);
}
}
}
If you are hitting on a unique Id column....
int maxID = 0;
Statement s2 = con.createStatement();
s2.execute("SELECT MAX(UniqueId) FROM MyTable");
ResultSet rs2 = s2.getResultSet();
if (rs2.next())
{
maxID = rs2.getInt(1);
}
If you are hitting on any other non-key column....
int maxID = 0;
Statement s2 = con.createStatement();
s2.execute("SELECT MAX(ColumnValue) FROM MyTable");
ResultSet rs2 = s2.getResultSet();
while (rs2.next())
{
maxID = rs2.getInt(1);
}
I'm working on the next step of the class assignment and I want to do a query where I pull the metadata from the database to list the tables in it then include the count of records in the tables. I have it where it kind of works but in the first half shown it only puts a number 1 where the count of records should go. In the second half (I'm doing this for testing purposes now and will only have the first half if I can get it to work) it prints out the number of records just fine but I have the table name hard coded in. I want it to be able to read in the table name from the metadata result then execute the query for the count of records. I am running this in netbeans with a MySQL database and am using the JDBC driver.
connect();
try
{
String db = conn.getCatalog();
data.append("The current database " + db + " contains the following tables:\r\n");
}
catch(Exception err)
{
System.out.print(err + "\r\n");
}
try
{
ResultSet rs = null;
DatabaseMetaData md = conn.getMetaData();
rs = md.getTables(null, null, "%", null);
while (rs.next())
{
String tbl = rs.getString(3);
System.out.print(tbl + "\r\n");
ResultSet ct = null;
ct = conn.createStatement().executeQuery("SELECT COUNT(*) FROM" + tbl);
while (ct.next())
{
System.out.print(ct.getString(1) + "\r\n");
data.append("Table \"" + rs.getString(3) + "\" contains " + ct.getString(1) + " records.\r\n");
}
}
data.append("\r\n");
}
catch(Exception err)
{
System.out.print(err + "\r\n");
}
try
{
ResultSet ct = null;
ct = conn.createStatement().executeQuery("SELECT COUNT(*) FROM customer");
while (ct.next())
{
data.append(ct.getString(1) + "\r\n");
}
ResultSet pt = null;
pt = conn.createStatement().executeQuery("SELECT COUNT(*) FROM product");
while (pt.next())
{
data.append(pt.getString(1) + "\r\n");
}
}
catch(Exception err)
{
System.out.print(err + "\r\n");
}
With following query you can query all tables with number of rows, date size, index size etc on a specific database.
SELECT concat(table_schema,'.',table_name) tables,
concat(round(table_rows),'') rows,
concat(round(data_length/(1024),2),'') data_size,
concat(round(index_length/(1024),2),'') index_size,
concat(round((data_length+index_length)/(1024),2),'') total_size,
round(index_length/data_length,2) index_data_ratio
FROM information_schema.TABLES
WHERE table_schema like "%YOUR-DATABASE-NAME-HERE%"
ORDER BY rows DESC LIMIT 20;
This is what I ended up doing and it works exactly as I wanted. I ended up using a prepared statement in the while loop to make it work.
connect();
try
{
String db = conn.getCatalog();
data.append("The current database " + db + " contains the following tables:\r\n");
}
catch(Exception err)
{
System.out.print(err + "\r\n");
}
try
{
ResultSet rs = null;
DatabaseMetaData md = conn.getMetaData();
rs = md.getTables(null, null, "%", null);
while (rs.next())
{
String tbl = rs.getString(3);
String query = ("SELECT COUNT(*) FROM " + tbl);
PreparedStatement cnt = conn.prepareStatement(query);
ResultSet ct = cnt.executeQuery();
while (ct.next())
{
data.append("Table " + rs.getString(3) + " has " + ct.getString(1) + " records\r\n");
}
}
data.append("\r\n");
}
catch(Exception err)
{
System.out.print(err + "\r\n");
}
I'm trying to count same values from one column of the table and return the value and counter, for example: value 1 repeat 3 time, value 2 repeat 2 time, ...
So far my program can store all value of desired column in an array. My problem is I don't know how to compare each value with other value. I know I need a counter and maybe one more array. So I stuck in If condition.
try{
Statement stmt = conn.createStatement();
//Statement stmt1 = conn.createStatement();
ResultSet rset = stmt.executeQuery( "SELECT N_REGIONKEY FROM NATION " );
List N_REGIONKEY = new ArrayList ();
while (rset.next()){
N_REGIONKEY.add(rset.getInt("N_REGIONKEY"));
if (rset.next ==N_REGIONKEY){
.
.
.
}
//System.out.println(N_REGIONKEY);
}
}
This is new version of what I wrote with your help, still the answer is not what it should be..
try{
System.out.println("counting rows in NATION table");
Statement stmt = conn.createStatement();
// Statement stmt1 = conn.createStatement();
ResultSet rset = stmt.executeQuery( "SELECT N_REGIONKEY FROM NATION " );
ArrayList N_REGIONKEY = new ArrayList ();
while (rset.next()){
N_REGIONKEY.add(rset.getInt("N_REGIONKEY"));
int occurrences = 0;
for (int i = 0; i<N_REGIONKEY.size();i++)
{
occurrences = Collections.frequency(N_REGIONKEY,N_REGIONKEY.get(i) );
System.out.println("There is " + occurrences + "occurrences for " + N_REGIONKEY.get(i));
}
Try to count by sql query, not by array
select N_REGIONKEY, count(N_REGIONKEY) as 'COUNT'
FROM NATION
group by N_REGIONKEY
int occurrences = 0;
for (int i = 0; i<N_REGIONKEY.size();i++){
occurrences = Collections.frequency(REGIONKEY, REGIONKEY.get(i));
System.out.println("There is " + ocurrences "ocurrences for " + N_RegionKey.get(i));
}
Thanks every one for your helps.Finally, I wrote this program without group by(even-though I know it's much slower). This ones has the same result as with "group by".
try{
System.out.println("counting simillar rows in NATION table");
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery( "SELECT N_REGIONKEY FROM NATION " );
ArrayList N_REGIONKEY = new ArrayList ();
int[] MyCounter = new int[5];
while (rset.next()){
MyCounter[rset.getInt("N_REGIONKEY")] ++;
}
for (int i = 0; i<5;i++){
if (MyCounter[i] > 0) {
System.out.println("There is " + MyCounter[i] + " ocurrences for " + i);
}