show progress-bar while downloading table row from sql server in android

462 Views Asked by At

I am fetching table row from sql server in android and i want to display in progress-bar but i am facing problem.I am using SQL Connection class which not allowing to take length of a table row.my connection classs.

String ip="192.168.43.85";
String classs="net.sourceforge.jtds.jdbc.Driver";            
String db="mydatabasename";
String uname="myname";                                                 
String pass="pass";

@SuppressLint("New Api")
public Connection CON() throws IOException{

    StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection connection=null;
    String ConUrl=null;

    try {

        Class.forName(classs);
        ConUrl="jdbc:jtds:sqlserver://"+ip+";"
                + "databaseName="+db+";user="+uname+";password="
                +pass+";";
        connection= DriverManager.getConnection(ConUrl);


    } catch (ClassNotFoundException e) {
        Log.e("ERRO",e.getMessage());

    } catch (SQLException e) {
        Log.e("ERROR",e.getMessage());
    } catch (Exception e) {
        Log.e("ERROr", e.getMessage());
    }

    return connection;
}

I tried some examples from google but all examples uses http and url but i don't have url to find length.

    Connection conn = null;
    try {
        conn = connectionClass.CON();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (conn == null) {

        Toast.makeText(getApplicationContext(), "Error in Connection with server.Make sure your internet connection is ON", Toast.LENGTH_SHORT).show();
    } else {

        Statement stmt = null;

        try {
            //String Slqquery = "select * from Zmat";
            String Slqquery1 = "select GlobalSummaryReportFlag,StudyID,StoreCode,StudyDesc from GlobalCountSummaryReport where GlobalSummaryReportFlag='1' and StudyId='" + strStudyId1 + "' and StoreCode='" + strStudyCode2 + "' and StudyDesc='" + strStudyDes3 + "'";

            stmt = conn.createStatement();
            ResultSet rs3;
            rs3 = stmt.executeQuery(Slqquery1);
            if (rs3.next()) {

                progress1=new ProgressDialog(MenuActivity.this);
                progress1.setMessage("Downloading GC");
                progress1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progress1.setIndeterminate(true);
                progress1.setProgress(0);
                progress1.setMax(100);
                progress1.show();
        final int totalProgressTime = 100;
        new Thread(new Runnable() {
            @Override
            public void run() {
                int jumpTime = 0;

                while(jumpTime < totalProgressTime) {
                    try {
                        Data();
                        if (countForHireMapping == 0 || CountForArticle == 0) {
                            Toast.makeText(getApplicationContext(), "Downloaded UnSuccessfully Please Retry", Toast.LENGTH_SHORT).show();
                            delete2();
                            progress1.dismiss();
                        } else {

                            sleep(200);
                            jumpTime += 5;
                            progress1.setProgress(jumpTime);
                            progress1.dismiss();
                            Intent intent = new Intent(MenuActivity.this, LocationActivity.class);
                            startActivity(intent);
                            Toast.makeText(getApplicationContext(), "Downloaded Successfully", Toast.LENGTH_SHORT).show();
                        }

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }).start();

            } else {
                Toast.makeText(getApplicationContext(), "Gc Not Completed", Toast.LENGTH_SHORT).show();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }`
1

There are 1 best solutions below

0
On

You should use AsyncTask in your MainActvity

It contains 3 methods:

  1. onPreExecute(): Inside this method place whatever you want to do before the class is executed.(In your case a progressBar )

  2. doInBackground(): Inside this method place whatever you want the app to do in background.(Example: getting data from server)

  3. onPostExecute(): Inside this method, place whatever you want to do once the background process is done. (Example: Setting the views according to the received data from online server)

Here is a tutorial to get you upto speed on AsyncTask.

Happy coding.