Re: java connector/J5.1 mysql
Posted by: Filipe Silva
Date: February 19, 2018 01:16PM

You don't need the class LoadDriver at all. JDK's DriverManager is able to load the Connector/J driver automatically if it is in the classpath.

I also don't understand why you are messing around with the java and javac executables. Those should be inside the JDK's bin directory and, if you want, accessible via system path. Otherwise just write full paths when running them.

To start with a simple and quick test you just need this (using a linux environment, it is pretty much the same in all other systems):

1. Create a project directory "test-cj"
2. Create the source file Test.java with the following contents:
----
import java.sql.*;

public class Test {
public static void main(String[] args) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "shorty", "asdfgh456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT VERSION()");
rs.next();
System.out.println(rs.getString(1));
conn.close();
}
}
----
3. Copy a Connector/J jar file (mysql-connector-java-5.1.45-bin.jar) into current directory.
4. Compile the code: javac -classpath mysql-connector-java-5.1.45-bin.jar *.java
5. Start the MySQL server (if not running yet).
6. Run your code: java -classpath mysql-connector-java-5.1.45-bin.jar:. Test

Notes:
- This example requires a database named "test" in the running MySQL Server.
- If running on Windows, the classpath in step 6 must be -classpath mysql-connector-java-5.1.45-bin.jar;. (mind the colon vs the semi-colon)

IHTH

Options: ReplyQuote


Subject
Written By
Posted
February 10, 2018 03:08PM
February 12, 2018 12:14PM
Re: java connector/J5.1 mysql
February 19, 2018 01:16PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.