1

This is a constructor for a class which I hoped would manage jdbc connection to Postgrsql database 'clients':

package niff.com;
import java.sql.*;
import java.util.*;
public class Connector
{
 Connection conn;
public Connector()
{
try
{
    String url = "jdbc:postgresql://localhost/clients?user=postgres&password=admin";
    Class.forName("org.postgresql.Driver");
    conn = DriverManager.getConnection(url);
}
catch (Throwable e)  
{   
        System.out.println(e.getMessage());
}}}

I am an amateur and haven't done any java programming for a few years. This worked ok on Ubuntu, using the postgresql driver 'postgresql-42.4.0.jar', specifying the classpath option for 'java' command, and using full package name for my 'Starter' class. I have been trying for two days to get it to work on Windows 10. I have OpenJDK 17. I have tried with 'postgresql-42.4.2.jar' from https://jdbc.postgresql.org, as well as with 'postgresql-42.2.18.jar' installed thru Postgresql StackBuilder. I tried numerous combinations using the -cp option for 'java', in the end added an environment variable thru the dialog in Settings. Its value is:

'.;C:\Users\vent_\OneDrive\Documents1\java_progs\base_de_donnees\niff\com\postgresql-42.4.2;'

(I tried other locations for the driver but it didn't help.) In the past I used the default package but I am trying out naming a package so my classes are in a folder 'com' which is in a folder 'niff' in the folder 'base_de_donnees'. To be honest, I am not even sure I am launching from the correct folder.
I have been typing:

'javac *.java' 
'java niff.com.Starter' or just 
'java Starter'

If I use the saved classpath I get ClassNotFoundException, although, and I cannot remember the combination that produced this, I have seen many times 'No suitable driver found'. If I type

java -cp C:\Users\vent_\OneDrive\Documents1\java_progs\base_de_donnees niff.com.Starter

(leaving the driver off the classpath), my GUI class displays correctly, and I either get 'No suitable driver' or ClassNotFoundException ("org.postgresql.Driver"). I have tried using jdb to debug and the problem seems to be once the code hits DriverManager. I don't know whether my problem is purely classpath related, or my inexperience with package names, or with java in general. I hope this is not too rambling but I am so frustrated I don't know where I am with it anymore!

Tunny
  • 35
  • 1
  • 7
  • Your `-cp` command lists the full path to the jdbc driver... but omits the `.jar` part. – rzwitserloot Aug 21 '22 at 21:00
  • Sorry, that was deliberate, I just wanted to show that my GUI class works, the problem is confined to the Connector class. The only way the project works at all is if I leave the driver off the classpath, obviously I want to include it but not sure how. – Tunny Aug 21 '22 at 22:31
  • You've put a driver class into your own package naming scheme, that's.. not at all how java works. perhaps you're best off accepting that whatever you remember from the past about java is all scrambled up, and start over with a first-steps java tutorial. – rzwitserloot Aug 22 '22 at 02:22
  • And perhaps you should explain what you mean by 'put a driver class into your own package naming scheme', especially after you mis-read the -cp flag as being the path to the driver, when it was in fact the path to the java classes (as explained to you). – Tunny Aug 22 '22 at 18:00
  • actually https://stackoverflow.com/questions/10546720/how-to-compile-packages-in-java solved my problem – Tunny Aug 22 '22 at 19:11

0 Answers0