How to add postgres library in eclipse / android

0

I am making an app on android, which I will connect directly with a postgres database on a server. But I do not know how to add the jar from postgres, if I add it in the Java Build Path in the project properties of the error and it does not execute anymore. So how do I add the jar, and which jar should I add, jdbc 3, 4, or 41. Thank you in advance.

I did what I was told to put in the lib folder and add it to the Java Build Path. And gave this error below.

10-14 16:51:53.720: E/CONEXAO(9610): NAO CONECTADO No suitable driver
10-14 16:51:53.722: D/AndroidRuntime(9610): Shutting down VM
10-14 16:51:53.724: E/AndroidRuntime(9610): FATAL EXCEPTION: main
10-14 16:51:53.724: E/AndroidRuntime(9610): Process: br.com.android.sicsm, PID: 9610
10-14 16:51:53.724: E/AndroidRuntime(9610): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.android.sicsm/br.com.android.sicsm.view.CategoriaActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String)' on a null object reference
10-14 16:51:53.724: E/AndroidRuntime(9610):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at android.app.ActivityThread.access$800(ActivityThread.java:143)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at android.os.Looper.loop(Looper.java:135)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at android.app.ActivityThread.main(ActivityThread.java:5070)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at java.lang.reflect.Method.invoke(Native Method)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at java.lang.reflect.Method.invoke(Method.java:372)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
10-14 16:51:53.724: E/AndroidRuntime(9610): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String)' on a null object reference
10-14 16:51:53.724: E/AndroidRuntime(9610):     at br.com.android.sicsm.dao.CategoriaDAO.listarCategoria(CategoriaDAO.java:52)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at br.com.android.sicsm.view.CategoriaActivity.carregarLista(CategoriaActivity.java:50)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at br.com.android.sicsm.view.CategoriaActivity.onCreate(CategoriaActivity.java:28)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at android.app.Activity.performCreate(Activity.java:5720)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
10-14 16:51:53.724: E/AndroidRuntime(9610):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
10-14 16:51:53.724: E/AndroidRuntime(9610):     ... 10 more
10-14 16:51:59.463: I/art(9610): Heap transition to ProcessStateJankImperceptible took 50.478906ms saved at least 283KB

Below is the code for the connection.

private final String endereco = "jdbc:postgres://10.1.32.31:5432/sics";
private final String usuario = "postgres";
private final String senha = "postdba";

Class.forName("org.postgresql.Driver").newInstance();
conn = DriverManager.getConnection(endereco, usuario, senha);

New error:

10-14 17:13:29.244: E/CONEXAO(15297): NAO CONECTADO A tentativa de conexão falhou.
10-14 17:13:29.244: D/AndroidRuntime(15297): Shutting down VM
10-14 17:13:29.246: E/AndroidRuntime(15297): FATAL EXCEPTION: main
10-14 17:13:29.246: E/AndroidRuntime(15297): Process: br.com.android.sicsm, PID: 15297
10-14 17:13:29.246: E/AndroidRuntime(15297): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.android.sicsm/br.com.android.sicsm.view.CategoriaActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String)' on a null object reference
10-14 17:13:29.246: E/AndroidRuntime(15297):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at android.app.ActivityThread.access$800(ActivityThread.java:143)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at android.os.Looper.loop(Looper.java:135)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at android.app.ActivityThread.main(ActivityThread.java:5070)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at java.lang.reflect.Method.invoke(Native Method)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at java.lang.reflect.Method.invoke(Method.java:372)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
10-14 17:13:29.246: E/AndroidRuntime(15297): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String)' on a null object reference
10-14 17:13:29.246: E/AndroidRuntime(15297):    at br.com.android.sicsm.dao.CategoriaDAO.listarCategoria(CategoriaDAO.java:52)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at br.com.android.sicsm.view.CategoriaActivity.carregarLista(CategoriaActivity.java:50)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at br.com.android.sicsm.view.CategoriaActivity.onCreate(CategoriaActivity.java:28)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at android.app.Activity.performCreate(Activity.java:5720)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
10-14 17:13:29.246: E/AndroidRuntime(15297):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
10-14 17:13:29.246: E/AndroidRuntime(15297):    ... 10 more
10-14 17:13:30.875: I/Process(15297): Sending signal. PID: 15297 SIG: 9
    
asked by anonymous 14.10.2014 / 01:37

1 answer

1

Are you talking about PosgreSQL? On Android, does not have enough space so, we do not use MySQL the PosG, but sqLite. In case you need to communicate with a Web server (with Posgrsql or MySQL). It is very dangerous to try to communicate directly, because you will have to enter the password, id etc ... in the Android app. And, in many cases, the server will not accept: in most cases, only a code placed on the same server as the database can access (security!)

What you need to do and put a PHP type page on the server and, with the Android app, call this page. This PHP page will read the data from the BDD and when it will echo the results, your Android app will receive the data.

On Android, to do this, you need to do an Asyntask () extension and use HttpPost (). When you are going to send for example link PHP will make a GET to receive the information:

   $nome = $_GET['nome'];

After PHP will read the BDD and when it will echo the result, Android will receive on your part of the InBackground () class AsyncTask.

    
14.10.2014 / 02:19