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 thejar
inside that folder and add them to theBuild Path
(may not need to). I believe this will work.– Wakim
I did this, but it ta error. No suitable driver. Says the driver is not suitable :/
– meisterx7
You need to see the correct version (the latest maybe) and load the Driver (using the
Class.forName
) before opening the connection.– Wakim
Look what I added up there, it’s the bug and down the code
– meisterx7
Your connection is void, check if the connection url is not:
jdbc:postgresql://10.1.32.31:5432/sics
– Wakim
Solved the driver issue, but now it can not connect, the other error, I will add the error in the question.
– meisterx7