Android studio connectivity with postgresql

1

I followed a tutorial to connect PostgresSql with Android, but still I have not obtained any solution.

How do I connect Android Studio with PostgreSsql?

I have written this code in my MainActitvity.java.

static final String JDBC_DRIVER = "org.postgresql.Driver";
static final String DB_URL = "jdbc:postgresql://localhost:5432/user1";

//  Database credentials
static final String USER = "root";
static final String PASS = "root";

public static void main(String[] args)
{
  Connection conn = null;
  Statement st = null;
 try{
    //STEP 2: Register JDBC driver
    Class.forName("org.postgresql.Driver");

    //STEP 3: Open a connection
    System.out.println("Connecting to database...");
    conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/","root","root");

    //STEP 4: Execute a query
    System.out.println("Creating statement...");
    st = conn.createStatement();
    String sql;
    sql = "SELECT  first, last FROM Employees";
    ResultSet rs = st.executeQuery(sql);

    //STEP 5: Extract data from result set
    while(rs.next()){
        //Retrieve by column name
        String first = rs.getString("first");
        String last = rs.getString("last");

        //Display values
        System.out.print(", First: " + first);
        System.out.println(", Last: " + last);
    }
    //STEP 6: Clean-up environment
    rs.close();
    st.close();
    conn.close();
  }catch(SQLException se){
    //Handle errors for JDBC
    se.printStackTrace();
  }catch(Exception e){
    //Handle errors for Class.forName
    e.printStackTrace();
  }
  finally
  {
    //finally block used to close resources
    try{
        if(st!=null)
            st.close();
    }catch(SQLException se2){
    }// nothing we can do
    try{
        if(conn!=null)
            conn.close();
    }
    catch(SQLException se){
        se.printStackTrace();
    }//end finally try
  }
}

Is this correct or should I write it somewhere else?

    
asked by Sr. J 18.09.2017 в 11:16
source

1 answer

0

If I'm not wrong, when it comes to doing something like that, it forces you to do it asynchronously, with which you should declare an asynchronous class where you make that call. To do this you can do it like this:

static final String JDBC_DRIVER = "org.postgresql.Driver";
static final String DB_URL = "jdbc:postgresql://localhost:5432/user1";

//  Database credentials
static final String USER = "root";
static final String PASS = "root";

public static void main(String[] args)
{

    //La llamada la ejecutamos así
    new HttpAsyncTask().execute();

    private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            //En esta parte del método es donde normalmente
            //creamos un cuadro de carga (ProgressDialog)
        }

        @Override
        protected String doInBackground(String... urls) {
            //En esta parte es donde realizamos la llamada. Se realiza de manera asíncrona
            Connection conn = null;
            Statement st = null;
              try{
                //STEP 2: Register JDBC driver
                Class.forName("org.postgresql.Driver");

                //STEP 3: Open a connection
                System.out.println("Connecting to database...");
                conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/","root","root");

                //STEP 4: Execute a query
                System.out.println("Creating statement...");
                st = conn.createStatement();
                String sql;
                sql = "SELECT  first, last FROM Employees";
                ResultSet rs = st.executeQuery(sql);

                //STEP 5: Extract data from result set
                while(rs.next()){
                    //Retrieve by column name
                    String first = rs.getString("first");
                    String last = rs.getString("last");

                    //Display values
                    System.out.print(", First: " + first);
                    System.out.println(", Last: " + last);
                }
                //STEP 6: Clean-up environment
                rs.close();
                st.close();
                conn.close();
            }catch(SQLException se){
                //Handle errors for JDBC
                se.printStackTrace();
            }catch(Exception e){
                //Handle errors for Class.forName
                e.printStackTrace();
            }
            finally
            {
                //finally block used to close resources
                try{
                    if(st!=null)
                        st.close();
                }catch(SQLException se2){
                }// nothing we can do
                try{
                    if(conn!=null)
                        conn.close();
                }
                catch(SQLException se){
                    se.printStackTrace();
                }//end finally try
        }

        @Override
        protected void onPostExecute(String result) {
            //En esta parte es donde tratamos el resultado devuelto por la llamada a la BBDD.
        }
    }
}

I have not checked if the call you make is right or wrong, but I put this to you to see where you have to put your code. You have to keep in mind that you are making an asynchronous call, with which, you will have to wait to receive the data to work with them, so you have to do it in the onPostExecute method.

    
answered by 18.09.2017 в 11:39