JDBC

Started by Armin Preisabout 25 years ago3 messagesgeneral
Jump to latest
#1Armin Preis
preisa@sbox.tugraz.at

I'd like to connect to a Postgresql Database(Ver. 6.5) on my localhost
with JDBC, but it doesnt work.

Is the following code ok?

//[...]

Class.forName("postgresql.Driver");
con = DriverManager.getConnection(jdbc:postgresql:databasename,
username, password); //where con is a Connection

//[...]

My JDK directory is:
/usr/java/jdk1.3/

My directory with my java-program is:
/home/me/database/

The Directory with de JDBC driver for Postgresql is:
/usr/lib/pgsql/jdbc6.5-1.2.jar

And how do i set the correct CLASSPATH in Linux so that my program
works?

Thanks in advance,
Armin

#2Armin Preis
preisa@sbox.tugraz.at
In reply to: Armin Preis (#1)
Re: JDBC

Armin Preis wrote:

I'd like to connect to a Postgresql Database(Ver. 6.5) on my localhost
with JDBC, but it doesnt work.

Thanks, the problem is already solved.

Armin

#3Tim Freund
tim@technofreund.com
In reply to: Armin Preis (#1)
Re: JDBC

I'll answer the code question last since you need a working classpath
first... ;-)

I pondered the class path issue for a long time. I will assume you are
using bash or something similiar, since that's what I know -- make appropriate
substitutions.

To see your current CLASSPATH:
echo $CLASSPATH

To set your classpath:
export CLASSPATH=/javadirectory/jarname.jar:/javadirectory/jar2.jar:

To add your postgres jar onto your current classpath, do this:
export CLASSPATH=$CLASSPATH:/usr/lib/pgsql/jdbc6.5-1.2.jar

Alternatively, if all of your programs are going to use JDBC, you can
copy jdbc6.5-1.2.jar to /usr/java/jdk1.3/jre/lib/ext -- that will automatically
load the jdbc action on JVM start up.

Let me know if you run into problems....

Tim
--
/* Tim Freund -- tim@technofreund.com */

Is the following code ok?

Your code was close, but you were missing an org. in front of
postgresql.Driver -- try this:

import java.io.*;
import java.lang.*;
import java.sql.*;

public class jdbcTest {
public static void main(String args[]){
Connection con = null;
String url = "jdbc:postgresql:dbname";
String username = "username";
String password = "password";

try{
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection(url, username, password);

} catch(SQLException e){
e.printStackTrace(System.out);
} catch(ClassNotFoundException e){
e.printStackTrace(System.out);
}
}
}