Campbell Ritchie wrote:Did that class compile normally? I so, where is the .class file? Did you give your class a module/package name?
Campbell Ritchie wrote:So, it appears the class compiled correctly, otherwise you would have had error messages. Please confirm you have that .class file in this location with the dir command (Windows®) or ls (Unix/OSX/Linux).
You probably should have given that class a package name, which you can read about in the Java™ Tutorials, but stick to one problem at a time and don't change anything else until you get your code to run. I presume you have altered the arguments for the connection to keep your details private.
Yes, that's what I expected; you wouldn't want to publish your private folder details.
Christian Antfeld wrote:. . . I changed the details to remain private. . . . it is good practice.
Confirming that you have JDBCTestConnection3.class in the . directory. I presume your connector/J is in the classpath quoted, and you didn't include the slash before the last J. You would appear to be on a Unix‑like system, which would interpret a slash in “Connector/J” as a different directory. Please also check that you don't have any files with a name similar to JDBCTestConnection3 anywhere else in the classpath.
. . . the .class file has saved to the same folder as the .java file.
No. That is why I said not to declare a package name at present.
Would a missing package name stop the .class file from running properly?
If you look at the Java™ Tutorials, you won't find any mistakes, assuming what you have obscured was correct.
. . . I'm not seeing anyone say the source code is wrong . . .
Knute Snortum wrote:I just tried to compile and execute your code, and I was able to get it to run. So your code is not the problem.
* Are you CD'd into the directory with the JDBCTestConnection3.class file?
* While in the directory, do you issue the command java JDBCTestConnection3 ?
That's a pleasure
Christian Antfeld wrote:Thanks . . .
I am never quite sure about this, but does your classpath entry (as set with -cp) end “jar”? If you expand $HOME, wouldn't you need a classpath like /home/cantfield/jdbc/resources/mysql-connector-java-8.0.18.jar? That would appear to be what the Connector J manual says. Start with $HOME or /home and end with .jar. You are correct to include .: in the classpath.
The name of the Connector/J is: mysql-connector-java-8.0.18.jar.
Look at the DriverManager#getConnection() documentation. The first parameter is called url. This connector J manual page shows an example where the url appears in green on line 9. I presume the ? is part of their database name and you probably don't need it yourself. Don't follow the example with Class.forName(...) in.
I'm not sure what is meant by URL text in the code.
I am not sure it is a good idea to move that .jar file about because any changes can cause more confusion. You would now need a classpath something like .:./mysql-connector-java-8.0.18.jar
. . . The compiled Java class file is in the same folder as the Connector/J.
Afraid you have sort of got 2 and 3 the wrong way round. The main method in the named Java® class is invoked first. That has a dependency on a .jar file which you name in the -cp option so the JVM can find the path to that .jar. The main method uses the connector, not the other way round.
. . .
1. Java class file written and compiled
2. Connector/J invoked with the -cp argument to java runtime tool
3. Java class file invoked and driven by the Connector/J identified in the Terminal argument
4. Output of SQL query or Exception thrown and error message displayed
. . .
We try our hardest to help, and it is nice to know we are appreciated. Please note the other links I gave you; they may be helpful too. Hope to see you back with better news
Christian Antfeld wrote:. . . helpful attention from you and all the others. . . .