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!