2

I am trying to connect postgreSQL, using JBDC, to my Android app.

I have written a small program which will try to connect to the database and then the textView will change to connected.

I know for sure that the url is not correct but I followed this website: http://jdbc.postgresql.org/documentation/80/connect.html and used the first one.

Here is my code:

 import android.app.Activity;
 import android.content.Context;
 import android.util.Log;
 import android.view.Menu;
 import android.view.View;
 import java.sql.*;
 import java.util.Properties;

 public class MainActivity extends Activity {

private static final String dbname = "postgres";
public static final String USER = " yOUR_DATABASE_USERNAME";
public static final String PASSWORD = "DATABASE_PASSWORD";
TextView text;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.tvConnect);

    try {
          Class.forName("org.postgresql.Driver");

    } catch (ClassNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
    String url = "jdbc:postgresql:localhost:5432/postgres";
      Connection conn;
    try {
        //conn = DriverManager.getConnection(url, USER, PASSWORD);

      conn =  DriverManager.getConnection(url);
      text.setText("Connected");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        Log.e("Tag", "Description", e); 
    }

    }

}

These are the errors I am getting:

    07-19 19:23:46.023: E/Tag(1246): Description
    07-19 19:23:46.023: E/Tag(1246): org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception.
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.Driver.connect(Driver.java:300)
    07-19 19:23:46.023: E/Tag(1246):    at java.sql.DriverManager.getConnection(DriverManager.java:179)
    07-19 19:23:46.023: E/Tag(1246):    at java.sql.DriverManager.getConnection(DriverManager.java:144)
    07-19 19:23:46.023: E/Tag(1246):    at com.sql.postgre.MainActivity.onCreate(MainActivity.java:46)
    07-19 19:23:46.023: E/Tag(1246):    at android.app.Activity.performCreate(Activity.java:5231)
    07-19 19:23:46.023: E/Tag(1246):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    07-19 19:23:46.023: E/Tag(1246):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    07-19 19:23:46.023: E/Tag(1246):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    07-19 19:23:46.023: E/Tag(1246):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    07-19 19:23:46.023: E/Tag(1246):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    07-19 19:23:46.023: E/Tag(1246):    at android.os.Handler.dispatchMessage(Handler.java:102)
    07-19 19:23:46.023: E/Tag(1246):    at android.os.Looper.loop(Looper.java:136)
    07-19 19:23:46.023: E/Tag(1246):    at android.app.ActivityThread.main(ActivityThread.java:5017)
    07-19 19:23:46.023: E/Tag(1246):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-19 19:23:46.023: E/Tag(1246):    at java.lang.reflect.Method.invoke(Method.java:515)
    07-19 19:23:46.023: E/Tag(1246):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    07-19 19:23:46.023: E/Tag(1246):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    07-19 19:23:46.023: E/Tag(1246):    at dalvik.system.NativeStart.main(Native Method)
    07-19 19:23:46.023: E/Tag(1246): Caused by: android.os.NetworkOnMainThreadException
    07-19 19:23:46.023: E/Tag(1246):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
    07-19 19:23:46.023: E/Tag(1246):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    07-19 19:23:46.023: E/Tag(1246):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    07-19 19:23:46.023: E/Tag(1246):    at java.net.InetAddress.getByName(InetAddress.java:289)
    07-19 19:23:46.023: E/Tag(1246):    at java.net.InetSocketAddress.<init>(InetSocketAddress.java:105)
    07-19 19:23:46.023: E/Tag(1246):    at java.net.InetSocketAddress.<init>(InetSocketAddress.java:90)
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.core.PGStream.<init>(PGStream.java:60)
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:101)
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.Driver.makeConnection(Driver.java:410)
    07-19 19:23:46.023: E/Tag(1246):    at org.postgresql.Driver.connect(Driver.java:280)

2 Answers 2

2

This might be a late answer but after reading about jdbc drivers for android many people have said that the jdbc jar does not work well at all for android and only one or two versions of the jar actually worked. Otherwise the best way of connecting your database with android is via a web service and using either Volley or Retrofit to talk to the database.

Sign up to request clarification or add additional context in comments.

Comments

0

Android has built-in support for SQLite database. Please refer to android documentation or you may download this code demo to quickly check how to code it.

Still if it is required to access any other database server, that server must be running on a machine as android does not host any SQL server so localhost should not work, if you are testing your code with emulator use 10.0.2.2 instead of localhost to access your postgres server on host development machine.

Another important issue to take care of while accessing a database server involves network, executing network related code is not allowed to be executed as part of an activity class hence you must write your code in AsyncTask android API.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.