views:

101

answers:

3

can some one please tell me how to connect java file to postgresql database (if posible wit code n explanation)

+5  A: 

Google is a good start

http://jdbc.postgresql.org/

skaffman
+3  A: 

Here is an example test.java

import java.sql.*;

class test
{
        public static void main(String[] args) {
                String hostname="", dbname="", username="", password="";
                try {
                        int argno = 0;
                        hostname = args[argno++]; 
                        dbname = args[argno++]; 
                        username = args[argno++]; 
                        password = args[argno++]; 
                } catch (Exception ex) {
                        System.err.println("Usage: java -cp driver.jar:. test [hostname] [dbname] [username] [password]");
                        System.exit(1);
                }
                try {
                        Class.forName("org.postgresql.Driver");
                        Connection connection =
                                DriverManager.getConnection(
                                        "jdbc:postgresql://"+hostname+"/"+dbname,
                                        username,
                                        password
                                );
                        ResultSet rs = connection.createStatement().executeQuery(
                                "select version() as version"  
                        );
                        while ( rs.next() ) {
                                System.out.println(rs.getString("version"));
                        }
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
        }
}

Download a current driver from JDBC download page, compile this and run like this on Unices:

java -cp [driver_file_name].jar:. test [hostname] [dbname] [username] [password]

On Windows:

java -cp [driver_file_name].jar;. test [hostname] [dbname] [username] [password]
Tometzky
Java classes begin with a capital letter, by convention.
Bastien Léonard
A: 

Just wanted to expound on Tometzky's answer for other beginners using the Netbeans IDE in UNIX like me.

I want the driver to be recognized as a library in the IDE. If you go to Tools->Libraries, you will see the current list. Hit "New Library" and type "PostgreSQL JDBC Driver" or whatever name you want to give it. Then in the Classpath tab, hit "Add JAR/Folder" and point to where you've saved your downloaded driver. I'm not sure if there is a "correct" place to store it, I think it rather depends on how you back up your system and if multiple users share it. Somewhere in your home directory is fine.

After that, make a new project of type "Java Application" and paste Tometzky's code into main. In your project tree, right click on Libraries and add the JDBC driver directly to the project. Now you don't have to worry about specifying the driver on the command line.

Build your project and head over to its "dist" folder. Now you can run it with the command

java -jar myprojectname.jar 127.0.0.1 [dbname] [user] [pw]

That of course assumes you are connect to the database server on your own machine. [user] and [pw] refer to your PostgreSQL username and pw.

Also, when you download the documentation it comes as a bunch of html files. Save them somewhere and point your browser to the index.html file (in Firefox it is File-->Open File).

Pete