Skip to Main Content

New to Java

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!

Importing a JDBC driver

807598Jan 19 2006 — edited Jan 21 2006
Hi all,

I'm currently running a piece of code on my student server to test Postgres database connectivity. I cun run it from my PC but eventually I need to run the queries etc from the student server itself.

I have been told that I need to import the jar file in the code. The problem is i'm not sure exactly how to do it. I've uploaded the jar file (pg74.214.jdbc3.jar) to the server and it is in the same directory as my program. I've tried a few different import commands but they don't compile.

Do you have any ideas?
import java.sql.*;
import javax.swing.*;

class DatabaseConnection {
	
	Connection       db;        
  	Statement        sql;       
 	DatabaseMetaData dbmd;  
	
	public DatabaseConnection() {
		
		try{
			String sqlText;
			String url = "jdbc:postgresql://students.odl.qmul.ac.uk:5432/scratch?ssl";
			String username = "Chris";
			String password = "Hello"; 
			String driverName = "org.postgresql.Driver";
			
			// Load the JDBC driver. Add jar file to your SDK jre\lib\ext folder.
        	Class.forName(driverName);
        	
        	//Try connecting to the server
			db = DriverManager.getConnection(url, username, password);
			
			//get MetaData to confirm connection
			dbmd = db.getMetaData(); 
			System.out.println("Connection to "+dbmd.getDatabaseProductName()+" "+
            dbmd.getDatabaseProductVersion()+" successful.\n");
            
            //create a statement that we can use later
            sql = db.createStatement(); 
            
            //carry out sql commands on the database
           	sqlText = "create table jdbc_demo (code int, text varchar(20))";
    		System.out.println("Executing this command: "+sqlText+"\n");
    		sql.executeUpdate(sqlText);
    		
    		/*sqlText = "insert into jdbc_demo values (1,'One')";
		    System.out.println("Executing this command: "+sqlText+"\n");
		    sql.executeUpdate(sqlText);
		
		 
		    sqlText = "insert into jdbc_demo values (3,'Four')";
		    System.out.println("Executing this command twice: "+sqlText+"\n");
		    sql.executeUpdate(sqlText);
		    sql.executeUpdate(sqlText);
		
		
		    sqlText = "update jdbc_demo set text = 'Three' where code = 3";
		    System.out.println("Executing this command: "+sqlText+"\n");
		    sql.executeUpdate(sqlText);
		    System.out.println (sql.getUpdateCount()+
		                        " rows were update by this statement\n");
		
		
		    System.out.println("\n\nNow demostrating a prepared statement...");
		    sqlText = "insert into jdbc_demo values (?,?)";
		    System.out.println("The Statement looks like this: "+sqlText+"\n");
		    System.out.println("Looping three times filling in the fields...\n");
		    PreparedStatement ps = db.prepareStatement(sqlText);
		    for (int i=10;i<13;i++) {
		      System.out.println(i+"...\n");
		      ps.setInt(1,i);         //set column one (code) to i
		      ps.setString(2,"HiHo"); //Column two gets a string
		      ps.executeUpdate();
   			}
    		ps.close();

 
    		System.out.println("Now executing the command: "+
                       "select * from jdbc_demo");
    		ResultSet results = sql.executeQuery("select * from jdbc_demo");
    		if (results != null) {
      			while (results.next()) {
        			System.out.println("code = "+results.getInt("code")+
                    "; text = "+results.getString(2)+"\n");
      			}
    		}
    		results.close();*/


    		sqlText = "drop table jdbc_demo";
    		System.out.println("Executing this command: "+sqlText+"\n");
    		sql.executeUpdate(sqlText);


    		db.close();
  		}
		catch (ClassNotFoundException e) {
			System.out.println(e);
		}
		catch (SQLException e) {
        System.out.println("Could not connect to the database");// Could not connect to the database
    	}


	}
	
	public static void correctUsage() {
    System.out.println("\nIncorrect number of arguments.\nUsage:\n "+
                       "java   \n");
    System.exit(1);
  	}
	
	public static void main (String [] args) {
		DatabaseConnection db = new DatabaseConnection();
	}
}
Kind regards,

Chris
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 18 2006
Added on Jan 19 2006
10 comments
345 views