0

I want to know if this is the correct way of executing multiple MySQL prepared statements in Android Studio.

I have created two tables in a database, namely t1 and t2. I want to be able to update both tables when the user presses a button. When the user presses the button the following code is executed:

new Post_data_Delete(Picking.this, order_var, Client_Name,scanned_tag, new_cylinder_amount, quan_type).execute();

The code is the Async task that runs when the button is pressed. The code works, but is this the correct way of doing this?

private static class Post_data_Delete extends AsyncTask<Void, Void, Void> {
    private final WeakReference<Picking> activityWeakReference;

    String records = "";
    String error = "";
    String Order_number;
    String Name_of_client;
    String data;
    String New_quanity;
    Integer Quanity_type;

    private Post_data_Delete(Picking activity,String order, String client, String scanned_data, Integer quanity, Integer quanity_type) {
        activityWeakReference = new WeakReference<Picking>(activity);

        Order_number = order;
        Name_of_client = client;
        data = scanned_data;
        New_quanity = quanity.toString();
        Quanity_type = quanity_type;

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected Void doInBackground(Void... voids) {

        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection c = DriverManager.getConnection("jdbc:mysql://10.168.09.66:3306/db", "u", "p");

            PreparedStatement update_cylinder = c.prepareStatement("UPDATE `db`.`t1` SET `IN` = ?, `CLIENT` = ? WHERE (`TAG` = ?)");
            update_cylinder.setString(1,"OUT");
            update_cylinder.setString(2,Name_of_client);
            update_cylinder.setString(3,data);
            update_cylinder.execute();
            update_cylinder.close();

            if(Quanity_type == 1)
            {
                PreparedStatement st = c.prepareStatement("UPDATE `db`.`t2` SET `QUANITY` = ? WHERE (`ORDER` = ?)");

                st.setString(1,New_quanity);
                st.setString(2, Order_number);
                st.execute();
                st.close();
            }

            else if(Quanity_type == 2)
            {
                PreparedStatement st = c.prepareStatement("UPDATE `db`.`t2` SET `QUANITY` = ? WHERE (`ORDER` = ?)");

                st.setString(1,New_quanity);
                st.setString(2, Order_number);
                st.execute();
                st.close();
            }

            else {}

            c.close();
        }

        catch(Exception e)
        {
            error = e.toString();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        Picking activity = activityWeakReference.get();
        if(activity == null || activity.isFinishing()) {
            return;
        }

        if(error != "") {
            Toast.makeText(activity, error, Toast.LENGTH_SHORT).show();
        }

        else{
            Toast.makeText(activity, "Successfully entered into database", Toast.LENGTH_LONG).show();
        
        }
    }

}
Paul
  • 21
  • 5
  • Please reconsider your use of JDBC: https://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android – CommonsWare Jul 10 '21 at 16:45
  • @CommonsWare thanks the system will run on a local MySQL database that is installed on-site. Does this still matter? – Paul Jul 10 '21 at 16:48
  • If the clients will be on the same local network as the database server (wired or WiFi), you may be able to get by with JDBC. I still would implement some sort of middleware, but I can at least see the argument for bypassing that. But if your clients are going to talk to the database over the Internet, particularly on mobile data networks, IMHO you really need middleware, for reliability and security reasons. – CommonsWare Jul 10 '21 at 19:58
  • @CommonsWare I did not know about that until you mentioned, so thank you for sharing. This app will not run via the internet, however there is no harm in exploring this new way as it also future proofs the app! – Paul Jul 10 '21 at 22:08
  • @CommonsWare what do you mean by middleware? – Paul Jul 10 '21 at 22:12
  • Sorry, I am showing my age, as [middleware](https://en.wikipedia.org/wiki/Middleware_(distributed_applications)) is a somewhat dated term. A web service would be a form of middleware. – CommonsWare Jul 10 '21 at 22:14
  • @CommonsWare So by webservice you basically mean creating a web API and then using a URL for eg. https://example.com/get.php?id=2 and in get.php you will then search the database for id=5 (get.php is a script that returns the value of the 5th row in the database)? – Paul Jul 10 '21 at 22:22
  • Yes, though it would not have to be PHP necessarily. – CommonsWare Jul 10 '21 at 22:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234731/discussion-between-paul-and-commonsware). – Paul Jul 10 '21 at 22:25

0 Answers0