About android direct access to postgresql database through jdbc

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.