Friends of StackOverFlow!
I Try "return" a ResultSet in select statement and have erros... I Search in Google and StackOverFlow, but nothing Solve This Problem!
The Statement Insert Work OK! But the Select not Work...
Database Layer:
package Database;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBConnection {
private static Connection conn = null;
private static ResultSet rs = null;
private static Statement stmt = null;
private static PreparedStatement pst = null;
private static final String DATABASE_PATH_NAME="jdbc:sqlite:MiraReal.Xitano";
private Connection Connect()
{
try
{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(DATABASE_PATH_NAME);
return conn;
}
catch (Exception ex)
{
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
public boolean RunQuery(String sql)
{
Statement stmt;
try {
Connect();
stmt = conn.createStatement();
stmt.execute(sql);
Disconnect();
return true;
} catch (SQLException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
// create a new table
}
public ResultSet RunSelect(String sql){
try {
Connect();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
//rs..moveToFirst();
Disconnect();
} catch (SQLException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return rs;
}
public int RunCount(String sql, String where)
{
return 1;
}
private void Disconnect()
{
try
{
DBConnection.conn.close();
DBConnection.conn = null;
} catch (SQLException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Actions Layer:
package Actions;
import Models.CustomersModels;
import java.sql.ResultSet;
import java.util.logging.Logger;
public class CustomersActions extends CustomersModels {
protected String sqlInsert = "INSERT INTO customers (uuid, fullname, email, created_at, updated_at) VALUES ('%s', '%s', '%s', '%s', '%s') ";
protected String sqlUpdate = "UPDATE customers SET fullname='%s' WHERE uuid='%s'";
protected String sqlDelete = "DELETE FROM customers WHERE uuid='%s'";
protected String sqlSelect = "SELECT %s FROM customers %s %s %s";
public boolean insert()
{
String sql = String.format(sqlInsert, this.getUuid(),
this.getFullname(), this.getEmail(), this.getCreatedAt(),
this.getUpdatedAt());
return this.RunQuery(sql);
}
public boolean update()
{
String sql = String.format(sqlUpdate, this.getFullname(), this.getUuid());
return this.RunQuery(sql);
}
public boolean delete()
{
String sql = String.format(sqlDelete, this.getUuid());
return this.RunQuery(sql);
}
public ResultSet select(String columns, String where, String order, String limit)
{
String sql = String.format(sqlSelect, columns, where, order, limit);
System.out.println(sql);
return this.RunSelect(sql);
}
}
Try use it (Result Set to jTable using net.proteanit.sql.DbUtils):
Customers customers = new Customers();
String customersColumns = "*";
String customersWhere = "";
String customersOrder = "";
String customersLimit = "";
ResultSet dsCustomers = customers.select(customersColumns, customersWhere, customersOrder, customersLimit);
this.jTable1.setModel(DbUtils.resultSetToTableModel(dsCustomers));
Error:
run:
SELECT * FROM customers
java.sql.SQLException: The prepared statement has been finalized
at org.sqlite.core.NativeDB.throwex(NativeDB.java:471)
at org.sqlite.core.NativeDB.column_name_utf8(Native Method)
at org.sqlite.core.NativeDB.column_name(NativeDB.java:232)
at org.sqlite.jdbc3.JDBC3ResultSet.getColumnName(JDBC3ResultSet.java:721)
at org.sqlite.jdbc3.JDBC3ResultSet.getColumnLabel(JDBC3ResultSet.java:714)
at net.proteanit.sql.DbUtils.resultSetToTableModel(DbUtils.java:21)
at Frames.MiraReal.seederCustomers(MiraReal.java:142)
at Frames.MiraReal.<init>(MiraReal.java:37)
at Frames.MiraReal$2.run(MiraReal.java:177)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Frames.MiraReal.seederCustomers(MiraReal.java:142)
at Frames.MiraReal.<init>(MiraReal.java:37)
at Frames.MiraReal$2.run(MiraReal.java:177)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
CONSTRUÍDO COM SUCESSO (tempo total: 2 segundos)
What i do?
Thank You!
Please re-read carefully the Javadoc for Statement
(superclass of PreparedStatement
).
Note:When a Statement object is closed, its current ResultSet object, if one exists, is also closed.
What is happening is that the PreparedStatement
is declared in RunQuery
, and it goes out of scope when that method ends, at which point the ResultSet
is no longer valid.
You cannot structure the code as you currently have it. The PreparedStatement
must be defined in the execution scope where the ResultSet
is used.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.