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)