skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, September 19, 2012

Establishing postgresql jdbc connection

Posted by Admin at 9:36 AM – 0 comments
 

The java code establishes postgresql database connection .



import java.sql.*;
import java.text.*;
import java.io.*;
//class
public class BioappAPI
{

   Connection       db;
   DatabaseMetaData dbmd;

   /** 

* The constructor of BioApp API class It establishes connection with the database * and intitializes the class variables * @param argv a String array that contains database name,username and password */ //Constructor public BioappAPI(String argv[]) throws ClassNotFoundException, SQLException { String database = argv[0]; String username = argv[1]; String password = argv[2]; try { Class.forName("org.postgresql.Driver"); db = DriverManager.getConnection("jdbc:postgresql:"+database,username,password); dbmd = db.getMetaData(); System.out.println("Connection to "+dbmd.getDatabaseProductName()+" "+ dbmd.getDatabaseProductVersion()+" successful. "); }//end of try catch(Exception e) { e.printStackTrace() ; System.out.println(" Connection could not be established!"); } }//end of constructor }//end of class


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger