Receiving data from a socket and querying database
Hi,
I have the following program, what it does is receive information from a socket, and when the information received matchs with a cell in a database, it performs some actions, when not, it should do nothing. The program is actually working, but, in the part that it should do nothing (else part of if (actualsituation.next()){ ) after some seconds (5 or so) it gives a exception,
My code is:
Code:
package pdp;
import java.io.*;
import java.net.*;
import java.sql.*;
public class Main {
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
try {
ss = new ServerSocket(6688);
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println("Listening on port 6688: " + ss);
while(true){
try {
s = ss.accept();
System.out.println("New connections accepted: " + s);
new GestorPeticion(s).start();
s = null;
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
class GestorPeticion extends Thread {
BufferedReader entrada = null;
Socket s;
public GestorPeticion(Socket s){
this.s = s;
}
@Override
public void run(){
try{
entrada = new BufferedReader(new InputStreamReader(s.getInputStream()));
String url = "jdbc:mysql://localhost/Polant";
Connection con = DriverManager.getConnection(url, "root", "1234");
while (true){
String str = entrada.readLine();
System.out.println("Receiving: " + str);
System.out.println("Checking if there is some policy to apply");
Statement select2 = con.createStatement();
ResultSet actualsituation = select2.executeQuery("SELECT ifx FROM rules WHERE ifx='"+str+"';");
if (actualsituation.next()){ // rs.next() is false if nothing is found
System.out.println("Lets retreive the values of the policy");
select2.close();
try {
con = DriverManager.getConnection(url, "root", "1234");
Statement select = con.createStatement();
ResultSet result = select.executeQuery("SELECT * FROM rules WHERE ifx='"+str+"';");
System.out.println("Got results:");
while (result.next()) { // process results one row at a time
String Nr = result.getString(1);
int Dp = result.getInt(2);
int At = result.getInt(3);
int Lv = result.getInt(4);
String Bp = result.getString(5);
String ifx = result.getString(6);
String whenx = result.getString(7);
System.out.println("Name = "+Nr);
System.out.println("Dparam = "+Dp);
System.out.println("At = "+At);
System.out.println("Lv = "+Lv);
System.out.println("Bp = "+Bp);
System.out.println("IF = "+ifx);
System.out.println("WHEN = "+whenx);
Socket echoSocket = null;
PrintWriter out = null;
select2.close();
con.close();
} else{
System.out.println("No policies to implement");
select2.close();
con.close();
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
And this is the error that the console gives after some minutes when the value retrieved from the socket does not match any value in the database
Quote:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientC onnectionException: No operations allowed after connection closed.
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Construc tor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:40 6)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:926)
at com.mysql.jdbc.ConnectionImpl.checkClosed(Connecti onImpl.java:1098)
at com.mysql.jdbc.ConnectionImpl.createStatement(Conn ectionImpl.java:2380)
at com.mysql.jdbc.ConnectionImpl.createStatement(Conn ectionImpl.java:2362)
at pdp.GestorPeticion.run(Main.java:61)
What is going wrong?
I closed all possible connections