Why am I getting an Resultset already closed error?
public class Server implements Runnable {
private static int port = 1600, maxConnections = 0;
public static Connection connnew = null;
public static Connection connnew1 = null;
public static Statement stnew, stnew1, stnew2, stnew3, stnew4;
public void getConnection() {
try {
Class.forName("org.gjt.mm.mysql.Driver");
connnew = DriverManager.getConnection("jdbc:mysql://localhost/db_alldata", "root", "flashkit");
connnew1 = DriverManager.getConnection("jdbc:mysql://localhost/db_main", "root", "flashkit");
stnew = connnew.createStatement();
stnew1 = connnew.createStatement();
stnew2 = connnew1.createStatement();
stnew3 = connnew1.createStatement();
stnew4 = connnew1.createStatement();
} catch (Exception e) {
System.out.print("Get Connection Exception---"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "----- > " + e);
}
}
public void closeConnection() {
try {
if (!(connnew.isClosed())) {
stnew.close();
stnew1.close();
connnew.close();
}
if (!(connnew1.isClosed())) {
stnew2.close();
stnew3.close();
stnew4.close();
connnew1.close();
}
} catch (Exception e) {
System.out.print("Close Connection Closing Exception-----"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "------->" + e);
}
}
Server() {
try {
} catch (Exception ee) {
System.out.print("Server Exceptions in main connection--"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "------>" + ee);
}
}
public static void main(String[] args) throws SQLException {
int i = 0;
Server STUD = new Server();
STUD.getConnection();
try {
ServerSocket listener = new ServerSocket(port);
Socket server;
while ((i++ < maxConnections) || (maxConnections == 0)) {
@SuppressWarnings("unused")
doComms connection;
server = listener.accept();
try {
ResultSet checkconnection = stnew4
.executeQuery("select count(*) from t_studentdetails");
if (checkconnection.next()) {
// DO NOTHING IF EXCEPTION THEN CLOSE ALL CONNECTIONS AND OPEN NEW
// CONNECTIONS
}
} catch (Exception e) {
System.out.print("Db Connection Lost Closing And Re-Opning It--------"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
+ "--------" + e);
STUD.closeConnection();
STUD.getConnection();
}
doComms conn_c = new doComms(server, stnew, stnew1, stnew2, stnew3);
Thread t = new Thread(conn_c);
t.start();
}
} catch (IOException ioe) {
System.out.println("Main IOException on socket listen: " + ioe);
}
}
public void run() {
}
}
class doComms implements Runnable {
private Socket server;
private String input;
static Connection conn = null;
static Connection conn1 = null;
static Statement st, st1, st2, st3;
doComms(Socket server, Statement st, Statement st1, Statement st2, Statement st3) {
this.server = server;
doComms.st = st;
doComms.st1 = st1;
doComms.st2 = st2;
doComms.st3 = st3;
}
@SuppressWarnings("deprecation")
public void run() {
input = "";
// char ch;
try {
DataInputStream in = new DataInputStream(server.getInputStream());
OutputStreamWriter outgoing = new OutputStreamWriter(server.getOutputStream());
while (!(null == (input = in.readLine()))) {
savetodatabase(input, server.getPort(), outgoing);
}
} catch (IOException ioe) {
System.out.println("RUN IOException on socket listen:-------"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "----- " + ioe);
ioe.printStackTrace();
}
}
public void savetodatabase(String line, int port1, OutputStreamWriter outgoing) {
try {
String Rollno = "-", name = "-", div = "-", storeddate = "-", storedtime = "-", mailfrom = "";
String newline = line;
String unitid = "-";
storeddate = new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
storedtime = new SimpleDateFormat("HH:mm:ss").format(new java.util.Date());
String sql2 = "delete from t_currentport where PortNumber='" + port1 + "''";
st2.executeUpdate(sql2);
sql2 = "insert into t_currentport (unitid, portnumber,thedate,thetime) values >('"
+ unitid + "','" + port1 + "','" + storeddate + "','" + storedtime + "')";
st2.executeUpdate(sql2);
String tablename = GetTable();
String sql = "select * from t_studentdetails where Unitid='" + unitid + "'";
ResultSet rst = st2.executeQuery(sql);
if (rst.next()) {
Rollno = rst.getString("Rollno");
name = rst.getString("name");
div = rst.getString("div");
}
String sql1 = "insert into studentInfo StoredDate,StoredTime,Subject,UnitId,Body,Status,Rollno,div,VehId,MailDate,MailTime,MailFrom,MailTo,Header,UnProcessedStamps) values('"
+ storeddate + "','" + storedtime + "','" + unitid + "','" + unitid + "','" + newline
+ "','Pending','" + Rollno + "','" + div + "','" + name + "','" + storeddate
+ "','" + storedtime + "','" + mailfrom + "','" + mailfrom + "','-','-')";
st1.executeUpdate(sql1);
} catch (Exception e) {
System.out.print("Save to db Connection Exception--"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "-->" + e);
}
}
}