roguewave
20-04-2016, 18:51
Ciao a tutti.
Mi sto esercitando con java e i database.
Fin qui tutto bene.
Una query mi da problemi, ma non la query in se, ma il risultato in eclipse, visto che in sqldeveloper la query da come risultato quello aspettato.
praticamente ho la mia classe Db in cui ho la query
package mobili;
import java.sql.Connection;
import java.util.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class Db {
private Connection con;
public Db() throws SQLException {
// establishConnection(); //ogni metodo non deve rivare la connessione
// avendo instanziato conn come variabile di istanza.
String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
String username = "SYSTEM";
String password = "*********";
con = DriverManager.getConnection(dbURL, username, password);
}
public List<EmployeeGroupJob> retrieveEmployeeJobId() throws SQLException {
List<EmployeeGroupJob> empList = new ArrayList<>();
String sql = "select job_id, max(salary) from HR.EMPLOYEES group by job_id";
Statement Stmt = con.createStatement();
ResultSet rs = Stmt.executeQuery(sql);
String id;
int maxSalary;
while (rs.next()) {
id = rs.getString(1);
maxSalary = rs.getInt(2);
// creating a Product object
mobili.EmployeeGroupJob empl=new mobili.EmployeeGroupJob();
empl.setJobId(id);
empl.setMaxSalary(maxSalary);
// adding a bean to arraylist
empList.add(empl);
}
return empList;
}
public void finalize() {
try {
if (con != null) {
con.close();
}
} catch (SQLException sqlex) {
System.out.println(sqlex);
}
}
}
La query, richiamatta dall'app
public class AppJobId {
public static void main(String[] args) {
Db db = null;
try {
db = new Db();
} catch (SQLException e) {
View.print("Errore db: " + e.getMessage());
return;
}
while (true) {
List <EmployeeGroupJob> empJob =null;
try {
empJob=db.retrieveEmployeeJobId();
} catch (SQLException e) {
View.print("Errore db: " + e.getMessage());
break;
}
View.print(empJob.size());
for (EmployeeGroupJob e : empJob) {
View.print(e.getJobId() + " " + e.getMaxSalary());
}
}
}
}
funziona ma mostra i risultati in loop fino a quando non riceve un
Errore db: ORA-01000: maximum open cursors exceeded
Mi sto esercitando con java e i database.
Fin qui tutto bene.
Una query mi da problemi, ma non la query in se, ma il risultato in eclipse, visto che in sqldeveloper la query da come risultato quello aspettato.
praticamente ho la mia classe Db in cui ho la query
package mobili;
import java.sql.Connection;
import java.util.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class Db {
private Connection con;
public Db() throws SQLException {
// establishConnection(); //ogni metodo non deve rivare la connessione
// avendo instanziato conn come variabile di istanza.
String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
String username = "SYSTEM";
String password = "*********";
con = DriverManager.getConnection(dbURL, username, password);
}
public List<EmployeeGroupJob> retrieveEmployeeJobId() throws SQLException {
List<EmployeeGroupJob> empList = new ArrayList<>();
String sql = "select job_id, max(salary) from HR.EMPLOYEES group by job_id";
Statement Stmt = con.createStatement();
ResultSet rs = Stmt.executeQuery(sql);
String id;
int maxSalary;
while (rs.next()) {
id = rs.getString(1);
maxSalary = rs.getInt(2);
// creating a Product object
mobili.EmployeeGroupJob empl=new mobili.EmployeeGroupJob();
empl.setJobId(id);
empl.setMaxSalary(maxSalary);
// adding a bean to arraylist
empList.add(empl);
}
return empList;
}
public void finalize() {
try {
if (con != null) {
con.close();
}
} catch (SQLException sqlex) {
System.out.println(sqlex);
}
}
}
La query, richiamatta dall'app
public class AppJobId {
public static void main(String[] args) {
Db db = null;
try {
db = new Db();
} catch (SQLException e) {
View.print("Errore db: " + e.getMessage());
return;
}
while (true) {
List <EmployeeGroupJob> empJob =null;
try {
empJob=db.retrieveEmployeeJobId();
} catch (SQLException e) {
View.print("Errore db: " + e.getMessage());
break;
}
View.print(empJob.size());
for (EmployeeGroupJob e : empJob) {
View.print(e.getJobId() + " " + e.getMaxSalary());
}
}
}
}
funziona ma mostra i risultati in loop fino a quando non riceve un
Errore db: ORA-01000: maximum open cursors exceeded