1

Good day. I have coded a server application that handles many socket connections from app clients, each client request to the server certain information from a sql database to display in its gui interface, calling a method inside the server to make the query and retrieve the data.

In some cases, the application hangs while tries to retrieve the data from the server, i want to my application does not freeze, but instead show a progress bar or an loading animation while retrieving the request. Is this somehow possible? I've read about threads, workers and sql pools, but i dont know exactly what it fits on my needs.

I have this class on server app to manage my queries:

final class QueryManager {
//abstract System.out.println("Ya");
static Connection con = null;
static String LoginMsg = "No conectado";
public static void init(){
try{
   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
   con=DriverManager.getConnection("jdbc:sqlserver://PERSONAL:49617;databaseName=prueba","sa", "12345678");
   System.out.println("Conexión Establecida");
   LoginMsg = "Conectado al Servidor";
}catch(ClassNotFoundException | SQLException e){
    e.printStackTrace();
    System.out.println("Exception: " + e.getMessage());
}
}
 public static void Query(String query, int i){
try{
Statement st = con.createStatement();
    try (ResultSet rset = st.executeQuery(Pquery(query))) {
        rset.next();
        System.out.println(rset.getString(1)+i);
    }
}catch(SQLException e){
System.out.println(e);
}


}

public static String Pquery(String pquery){
return pquery;
}
public static boolean Login(String user, String pass){
boolean r = false;
try{
Statement getCon = con.createStatement();
ResultSet EQuery = getCon.executeQuery("select * from users where usuario ='"+user+"'      and contrasena ='"+pass+"'");
EQuery.next();       
    if(EQuery.getString(1) != null){
        r = true;  
        LoginMsg = "Bienvenido";
    }else{
        r = false;

    }
    EQuery.close();
}catch(SQLException e){
    System.out.println(e);
}
return r;

}
public static String getLoginMsg(){
return LoginMsg;
}
public static void printer(){
    System.out.println("Ya");

}
}

Thank you in advance, any help on logic also will be much appreciated.

EDIT:

I have found this example and might be usefull. Thank you for your support.

https://sites.google.com/site/drjohnbmatthews/randomdata

4

1 回答 1

1

run your client side logic of sending request and getting results from server in a separate backgroud thread. you can use SwingWorker to achive that. With the help of this you can run a progress bar for the UI while that logic is running in backgroud thread. For example: swing worker

于 2012-12-18T05:51:07.560 回答