Sample Code
Package ycq. connection;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. statement;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Public class connectionsqlactivity extends activity {
Static connection conn = NULL;
Static preparedstatement pstate = NULL;
Static statement state = NULL;
Static string dirve = "org. PostgreSQL. Driver ";
Static string URL;
Resultset rs = NULL;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Url = "JDBC: PostgreSQL: // 192.168.0.249: 5432/yangdb ";
Try {
If (conn = NULL | conn. isclosed ())
{
Class. forname (dirve );
Conn = drivermanager. getconnection (URL, "Yang", "888888 ");
}
Pstate = conn. preparestatement ("select * from Yang ");
Rsw.pstate.exe cutequery ();
} Catch (exception e ){
E. printstacktrace ();
}
// Conn. Close ();
// Pstate. Close ();
}
}
Note permission <uses-Permission Android: Name = "android. Permission. Internet"/>
JDBC is postgresql-9.0-801.jdbc4.jar;
Another problem is the SDK version. I used to set up a project using the SDK 4.0.3, but the connection was not established. Then I created another project with the SDK Version 2.2, and the result was successful, later, I used androidmanifest in the Project of SDK 4.0.3. in XML, you can change <uses-SDK Android: minsdkversion = "15"/> to <uses-SDK Android: minsdkversion = "8"/>,
I Baidu on the impact of minsdkversion on the application, did not find why, there are known Daniel, trouble to tell, other database connection can see http://blog.csdn.net/conowen/article/details/7435231