Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing a jar file into a java class

Status
Not open for further replies.

jimwal0367

Programmer
Dec 3, 2005
24
GB
I need to connect to a secure postgresql database, i have the latest drivers downloaded from the postgresql website.

My main question is:-

Is there a way of importing the jar file into the java class.

So method in another java class can then call the DbConnect class file.

At present the only way I can run DbConnect.java is by manually typing in the jar file and associating it to the DbConnect class, as follows:-

java -cp pg73jdbc3.jar:. DbConnect


Can the jar file imported as a package?

Thanks

Jimwal0367
 
1) Read the JDBC tutorial : 3) Add your driver jar to the CLASSPATH
2) Find out what the postgres driver name is, and use that with the knowledge you gained from the above tutorial.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hi sedj,

Here is my code, I have set my code up as per the tutorial that you mentioned, but the only way to execute the java file is by using the following on a command line:-

java -cp pg73jdbc3.jar:. DbConnect

Is there anyway I get the java file to run as a standalone.


Java code see below

//package DbConnect.pg73jdbc3.jar;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class DbConnect {
public static void main(String[] argv) {
System.out.println("Checking if Driver is registered with DriverManager.");

try {
Class.forName("org.postgresql.Driver.DbConnect");
} catch (ClassNotFoundException cnfe) {
System.out.println("Couldn't find the driver!");
System.out.println("Let's print a stack trace, and exit.");
cnfe.printStackTrace();
System.exit(1);
}

System.out.println("Registered the driver ok, so let's make a connection.");

Connection c = null;

try {
// The second and third arguments are the username and password,
// respectively. They should be whatever is necessary to connect
// to the database.
c = DriverManager.getConnection("jdbc:postgresql:server name",
"username", "password");// you need to change these to match your own username & password
} catch (SQLException se) {
System.out.println("Couldn't connect: print out a stack trace and exit.");
se.printStackTrace();
System.exit(1);
}

if (c != null)
System.out.println("Hooray! We connected to the database!");
else
System.out.println("We should never get here.");
}
}
 
You just need to set your CLASSPATH to include the postgres dirver jar file.

Eg, on linux/unix :

export CLASSPATH=/path/to/driver.jar

Then you can just to :

java DbConnect

By the way, the "-cp" switch in your command is just setting the classpath for that one process.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top