How to add postgres library to eclipse/android

Asked

Viewed 586 times

0

I’m making an android app, which I will connect directly to a postgres bank on a server. However, I don’t know how to add the postgres jar, if I add it in Java Build Path in the project’s properties and it doesn’t run anymore. So as I add the jar, and which jar should I add, jdbc 3, 4 or 41. Thank you very much in advance.

I did what I was told to put in the lib folder and add in 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 follows the connection code.

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 mistake:

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
  • I believe you should create a folder called libs at the root of your project. Put the jar inside that folder and add them to the Build Path (may not need to). I believe this will work.

  • I did this, but it ta error. No suitable driver. Says the driver is not suitable :/

  • You need to see the correct version (the latest maybe) and load the Driver (using the Class.forName) before opening the connection.

  • Look what I added up there, it’s the bug and down the code

  • Your connection is void, check if the connection url is not: jdbc:postgresql://10.1.32.31:5432/sics

  • Solved the driver issue, but now it can not connect, the other error, I will add the error in the question.

Show 1 more comment

1 answer

1

You’re talking about Posgresql ? About Android, there is not enough space so we do not use Mysql Posg, but sqlite. In case you need to communicate with a web server (with Posgrsql or Mysql). And very dangerous try to communicate directly, because you will need to put the password, id etc... in the Android app. And in many cases, the server will not accept: in the majority of cases, only a code placed on the same server that the database can access (security!)

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

On Android, to do this, you need to extend Asyntask() and use Httppost(). When you will send for example http://dominio.com/call_bdd.php?nome=marcello PHP will make a GET to receive the information:

   $nome = $_GET['nome'];

After PHP will read the BDD and when it will do "echo" of the result, Android will receive in its part of the class Asynctask InBackground().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.