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

3
  • you can do this, you will need to use threads, like you suggested. i haven't used threads in a while so i can't help you off the top of my head though. Commented Dec 18, 2012 at 5:50
  • On a sidenote: your code is vulnerable to SQL injections. I would recommend using PreparedStatement instead of Statement. Commented Dec 18, 2012 at 6:15
  • Thank you. I'll check that bug. Thanks for your support. Commented Dec 18, 2012 at 23:14

1 Answer 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.