Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Please help me in my jdbc-PostgreSQL

843854Jul 4 2002 — edited Jul 24 2003
Hi everybody, I'm new to java but we have a project at school and we need to
connect to a postgeSQL database on linux using java... via a jsp, servlet..
It's all on the same server... my linux is mandrake 8.2, postgreSQL 7.2,
my java is java2 1.4.0... I've had surf the web and what I have found it's
almost the same every where... I found some sample code and this extra jdbc
for postgres but we just can't make them work,

This is the sample code I'm using trying to connect to my postgresql server

--------------------------------------------------------------
import java.sql.*;

class PostgreSQLTest {
public static void main (String[] args) {
try {
Driver driver = (Driver)
Class.forName("postgresql.Driver").newInstance();
DriverManager.registerDriver(driver);

String url = "jdbc:postgresql:javatest";
Connection con = DriverManager.getConnection(url, "postgres", "");
Statement stm = con.createStatement();

stm.setQueryTimeout(10);
ResultSet rs = stm.executeQuery("select col1 from test");

rs.next();

System.out.println(rs.getString(1));

} catch (SQLException e) {

System.out.println("Exception!");
System.out.println(e.toString());
}
}

-----------------------------------

the rest of instruction it reads like the following:

-----------------------------------
Compile the program with the Java compiler.
javac PostgreSQLTest.java


If the compiler produces errors, double check the syntax and confirm your PATH and CLASSPATH.

Run the program with the JVM.
java PostgreSQLTest


If the JVM produces errors, confirm your PATH and CLASSPATH.

You should see the following output:
Hello, from PostgreSQL!


Congratulations, you have installed, set up an environment for, and tested a JDBC interface to PostgreSQL.
---------------------------------

I've just copy the code and made the changes to suit my local server settings.... but it
displays de next message:

---------------------------------
PostgeSQLTest.java:7: unreported exception java.lang.ClassNotFoundException; mus
st be caught or declared to be thrown
Class.forName("org.postgresql.Driver").newInstance();

PostgreSQLTest.java:7: unreported exception java.lang.InstantiationException; mu
st be cuaght or declared to be thrown
Class.forName("org.postgresql.Driver").newInstance();
2 errors
$.
---------------------------------

So as far as I can understand the code still needs some exception catch so I
type the next code

---------------------------------
import java.sql.*;

class PostgreSQLTest2 {
public static void main (String[] args) {
try {
Driver driver = (Driver)
Class.forName("org.postgresql.Driver").newInstance();
DriverManager.registerDriver(driver);

String url = "jdbc:postgresql:javatest";
Connection con = DriverManager.getConnection(url, "sa", "");
Statement stm = con.createStatement();

stm.setQueryTimeout(10);
ResultSet rs = stm.executeQuery("select col1 from test");

rs.next();

System.out.println(rs.getString(1));

} catch (ClassNotFoundException e) {

System.out.println("Imposible encontrar el driver: " + e.getMessage());
} catch (SQLException e) {
System.out.println("Imposible conectar a la base: " + e.getMessage());
} catch (InstantiationException e) {
System.out.println("Imposible algo de instantation: " + e.getMessage());
} catch (IllegalAccessException e) {
System.out.println("Acceso ilegal: " + e.getMessage());
}
}}
---------------------------------

Ok so this is suppose to handle all the exceptions....
Now when i try to compile with "javac PostgreSQLTest.java"
It's runs smootly... in verbose mode I can see that it writes the PostgreSQLTest.class file....
but then when I try to run it it displays the next message:

---------------------------------
Exception in thread "main" java.lang.BoClassDefFoundError: PostgreSQLTest
---------------------------------

I've been moving around my jdbc jar files... just to se if.... I don't know maybe
is has to be the path or dir .... by the way this is my .bashrc file

---------------------------------
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:/usr/java/j2sdk1.4.0/bin
JAVA_HOME=$JAVA_HOME/usr/java/j2sdk1.4.0/
CLASSPATH=$CLASSPATH:/usr/share/pgsql/jdbc7.2.dev-1.2.jar
TOMCAT_HOME=/var/tomcat4/
ENV=$HOME/.bashrc
USERNAME="root"
export USERNAME ENV PATH JAVA_HOME CLASSPATH
---------------------------------

Well... so please if someone can help me out here... I can only say I would really, really thank you...
,
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 21 2003
Added on Jul 4 2002
3 comments
469 views