Java connection to PostgreSQL database for simple use

Source: Internet
Author: User

To
Http://wwwmaster.postgresql.org/download/mirrors-ftp? File?binary=2fv8.1.4%2fwin32%2f%sql-8.1.4-1.zip
Select a country with fast download speeds to download the PostgreSQL database

To

Http://jdbc.postgresql.org/download.html download JDBC driver

Install PostgreSQL and create tables

-- Table: testtable

-- Drop table testtable;

Create Table testtable
(
Id bigserial not null,
Name varchar (30 ),
Constraint testtable_pkey primary key (ID)
)
Without oids;
Alter table testtable owner to ipvs;

 

Create sequence testtable_id_seq
Increment 1
Minvalue 1
Max value 9223372036854775807
Start 3
Cache 1;
Alter table testtable_id_seq owner to ipvs;

Connection test...

Package com. test;
Import org. PostgreSQL .*;
Import java. SQL .*;

Public class testdb {
Public testdb (){
}
Public static void main (string ARGs [])
{
System. Out. Print ("this is a test ");
Try
{
Class. forname ("org. PostgreSQL. Driver"). newinstance ();
String url = "JDBC: PostgreSQL: // localhost: 5432/Postgres ";
Connection con = drivermanager. getconnection (URL, "s", "1234 ");
Statement ST = con. createstatement ();
String SQL = "select * From testtable ";
Resultset rs = st.exe cutequery (SQL );
While (Rs. Next ())
{
System. Out. Print (Rs. getint (1 ));
System. Out. println (Rs. getstring (2 ));
}
Rs. Close ();
St. Close ();
Con. Close ();


}
Catch (exception ee)
{
System. Out. Print (EE. getmessage ());
}
}
}

 

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.