Registering Connector/J driver  
Author Message
harri.saarikoski





PostPosted: 2004-11-11 2:58:00 Top

java-programmer, Registering Connector/J driver Hi. I have
- mySQL 1.4
- Connector/J 3.0.15

I want to use Java to query mySQL database via Connector/J.

Connector/J documentation at
http://dev.mysql.com/doc/connector/j/en/index.html instructs to
compile and run the following Java code to register the Connector/J
driver:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// Notice, do not import com.mysql.jdbc.*
// or you will have problems!

public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations

Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
}
}
// Error:
// SQLJConnectorRegister1failed.java:8:
// class LoadDriver is public, should be declared in a file named
// LoadDriver.java

Where should I have this LoadDriver class/java file? It doesn't come
with any of the packages I have (java.sql.* or connector/J driver
classes). Hence I can't "supply a class that implements Driver
interface" which would I gather register the driver. I have set the
classpath to the driver's jar, so it should find that class.

*
And then SQL queries would be possible. That would be an achievement
as such, never loaded/registered a driver in my life, or programmed
much anyway.

Appreciate any help over this.
Best, Harri S
Finland
language technologist
 
Andy Flowers





PostPosted: 2004-11-11 3:07:00 Top

java-programmer >> Registering Connector/J driver The public class in a .java source file MUST be names the same as the file
it is contained within.

Thus your LoadDriver class should be in a file names LoadDriver.java.

"HarriS" <email***@***.com> wrote in message
news:email***@***.com...
> Hi. I have
> - mySQL 1.4
> - Connector/J 3.0.15
>
> I want to use Java to query mySQL database via Connector/J.
>
> Connector/J documentation at
> http://dev.mysql.com/doc/connector/j/en/index.html instructs to
> compile and run the following Java code to register the Connector/J
> driver:
>
> import java.sql.Connection;
> import java.sql.DriverManager;
> import java.sql.SQLException;
>
> // Notice, do not import com.mysql.jdbc.*
> // or you will have problems!
>
> public class LoadDriver {
> public static void main(String[] args) {
> try {
> // The newInstance() call is a work around for some
> // broken Java implementations
>
> Class.forName("com.mysql.jdbc.Driver").newInstance();
> } catch (Exception ex) {
> // handle the error
> }
> }
> }
> // Error:
> // SQLJConnectorRegister1failed.java:8:
> // class LoadDriver is public, should be declared in a file named
> // LoadDriver.java
>
> Where should I have this LoadDriver class/java file? It doesn't come
> with any of the packages I have (java.sql.* or connector/J driver
> classes). Hence I can't "supply a class that implements Driver
> interface" which would I gather register the driver. I have set the
> classpath to the driver's jar, so it should find that class.
>
> *
> And then SQL queries would be possible. That would be an achievement
> as such, never loaded/registered a driver in my life, or programmed
> much anyway.
>
> Appreciate any help over this.
> Best, Harri S
> Finland
> language technologist


 
Steve W. Jackson





PostPosted: 2004-11-11 4:08:00 Top

java-programmer >> Registering Connector/J driver In article <email***@***.com>,
email***@***.com (HarriS) wrote:

>:Hi. I have
>:- mySQL 1.4
>:- Connector/J 3.0.15
>:
>:I want to use Java to query mySQL database via Connector/J.
>:
>:Connector/J documentation at
>:http://dev.mysql.com/doc/connector/j/en/index.html instructs to
>:compile and run the following Java code to register the Connector/J
>:driver:
>:
>:import java.sql.Connection;
>:import java.sql.DriverManager;
>:import java.sql.SQLException;
>:
>:// Notice, do not import com.mysql.jdbc.*
>:// or you will have problems!
>:
>:public class LoadDriver {
>: public static void main(String[] args) {
>: try {
>: // The newInstance() call is a work around for some
>: // broken Java implementations
>:
>: Class.forName("com.mysql.jdbc.Driver").newInstance();
>: } catch (Exception ex) {
>: // handle the error
>: }
>:}
>:}
>:// Error:
>:// SQLJConnectorRegister1failed.java:8:
>:// class LoadDriver is public, should be declared in a file named
>:// LoadDriver.java
>:
>:Where should I have this LoadDriver class/java file? It doesn't come
>:with any of the packages I have (java.sql.* or connector/J driver
>:classes). Hence I can't "supply a class that implements Driver
>:interface" which would I gather register the driver. I have set the
>:classpath to the driver's jar, so it should find that class.
>:
>:*
>:And then SQL queries would be possible. That would be an achievement
>:as such, never loaded/registered a driver in my life, or programmed
>:much anyway.
>:
>:Appreciate any help over this.
>:Best, Harri S
>:Finland
>:language technologist

As another poster has pointed out (and the error messages above tell),
the LoadDriver class must be in a file named LoadDriver.java if it's
declared public. In addition, the class you've coded above isn't going
to do much good if this is all there is to it -- simply creating an
instance of the driver and presumably registering it. The act of
registering the driver isn't valid after the program ends.

There's not really any requirement to register the driver anyway. But
it can be done by passing an instance to the DriverManager's static
registerDriver() method.

All that's actually required is that you get a Connection object. You
can do that by calling one of the static DriverManager.getConnection()
methods. In our application, we use the one that takes a "url" String
and the user/password strings. The url portion we construct from our
properties file, which contains everything needed. One of the
properties we have is jdbc.drivers, which is set to
com.mysql.jdbc.Driver. Another is jdbc.url, which begins with
"jdbc:mysql:" and an appropriate string pointing to the system and the
database name. The final two properties in our file are jdbc.username
and jdbc.password, the purposes of which are obvious.

HTH.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
 
Mark Matthews





PostPosted: 2004-11-13 1:46:00 Top

java-programmer >> Registering Connector/J driver HarriS wrote:
> Hi. I have
> - mySQL 1.4
> - Connector/J 3.0.15
>
> I want to use Java to query mySQL database via Connector/J.
>
> Connector/J documentation at
> http://dev.mysql.com/doc/connector/j/en/index.html instructs to
> compile and run the following Java code to register the Connector/J
> driver:
[snip]

Harri,

The text says "The following section of Java code shows how you might"
(maybe I need to emphasize _might_) :)

It's just an example. It's not something you need to do. Connector/J
works just like any other JDBC driver for registering/connecting. If
you've never used JDBC before, you might be better off starting with a
generic JDBC tutorial, such as the one from Sun, or any of the fine JDBC
books out there.

-Mark