0

I would like to use JDBC. My code:

import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class test {

Connection conn;

public test() throws ClassNotFoundException, SQLException{
    Class.forName("org.postgresql.Driver");
    String url = "jdbc:postgresql://localhost/tb";
    Properties props = new Properties();
    props.setProperty("user","user");
    props.setProperty("password","passwd");
    conn = DriverManager.getConnection(url, props);
    System.out.println(conn.isClosed());
}

public static void main(String[] args) {
    try{
        test t = new test();
    } catch (Exception e){
        e.printStackTrace();        
    }
}
}

My soft: postgres 9.1, java 1.7

My try:

ziel@gad ~/java/test $ ls
postgresql-9.1-902.jdbc4.jar  test.java
ziel@gad ~/java/test $ javac test.java 
ziel@gad ~/java/test $ java -cp postgresql-9.1-902.jdbc4.jar test
Error: Could not find or load main class test

According to people from google this should load the driver. What am I doing wrong?

4 Answers 4

3

Use java -cp postgresql-9.1-902.jdbc4.jar:. test under Linux / Unix / OS X

Sign up to request clarification or add additional context in comments.

Comments

3

Thats because when you did -cp postgresql-9.1-902.jdbc4.jar, you defined the jar postgresql-9.1-902.jdbc4.jar as the only location of your class files and the class test isn't in this jar. To resolve the problem you have to execute this cmd java -cp postgresql-9.1-902.jdbc4.jar;. test

2 Comments

If I do java -cp 'postgresql-9.1-902.jdbc4.jar;.' test I get the same: Error: Could not find or load main class test.
do you have the file test.class in ~/java/test? why did you use simple quotes ' in your cmd?
1

i guess you are using linux os, so try this java -cp postgresql-9.1-902.jdbc4.jar:. test. In windows java -cp postgresql-9.1-902.jdbc4.jar;. test

Comments

0

Execute following on Linux machine

javac -cp '.:postgresql-9.1-901.jdbc4.jar' postgresjavatest.java

java -cp '.:postgresql-9.1-901.jdbc4.jar' postgresjavatest

make sure jar file is on same location where command is fired.

1 Comment

the OP has postgresql-9.1-902.jdbc4.jar. Why are you posting this answer everywhere?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.