Sign in to follow this  
Sparhawk42

[java] Problem with MySql and java under Windows

Recommended Posts

Hello, i just tried to get the first example to work:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.mysql.management.driverlaunched.ServerLauncherSocketFactory;

public class ConnectorMXJTestExample {
    public static void main(String[] args) throws Exception {
        String hostColonPort = "localhost:3306";
        
        String driver = com.mysql.jdbc.Driver.class.getName();
        String url = "jdbc:mysql://" + hostColonPort + "/" + "?"
                + "socketFactory="
                + ServerLauncherSocketFactory.class.getName();
        String userName = "root";
        String password = "";

        Class.forName(driver);
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, userName, password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT VERSION()");
            rs.next();
            String version = rs.getString(1);
            rs.close();
            stmt.close();

            System.out.println("------------------------");
            System.out.println(version);
            System.out.println("------------------------");
        } finally {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            ServerLauncherSocketFactory.shutdown(hostColonPort);
        }
    }
}

With the following compile statement and execute statement: H:\MySQL\connector-mxj>javac ConnectorMXJTestExample.java -cp H:\MySQL\jar\conne ctor-mxj.jar;H:\MySQL\jar\mysql-connector-java-3.1.11-bin.jar H:\MySQL\connector-mxj>java ConnectorMXJTestExample -cp H:\MySQL\jar\connector-m xj.jar;H:\MySQL\jar\mysql-connector-java-3.1.11-bin.jar and get only the following error Exception in thread "main" java.lang.NoClassDefFoundError: ConnectorMXJTestExamp le The error seems to be caused by String driver = com.mysql.jdbc.Driver.class.getName() But if i reduce the code to the following: Class.forName("com.mysql.jdbc.Driver").newInstance(); I still get the same error. can any body help me? Thanks a lot!

Share this post


Link to post
Share on other sites

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.mysql.management.driverlaunched.ServerLauncherSocketFactory;

public class ConnectorMXJTestExample {
public static void main(String[] args) throws Exception {

String driver = com.mysql.jdbc.Driver.class.getName();


Class.forName(driver);
}
}


This waht i get with one ; more:
H:\MySQL\connector-mxj>javac ConnectorMXJTestExample.java -cp H:\MySQL\jar\conne
ctor-mxj.jar;H:\MySQL\jar\mysql-connector-java-3.1.11-bin.jar;

H:\MySQL\connector-mxj>java ConnectorMXJTestExample.java -cp H:\MySQL\jar\connec
tor-mxj.jar;H:\MySQL\jar\mysql-connector-java-3.1.11-bin.jar;
Exception in thread "main" java.lang.NoClassDefFoundError: ConnectorMXJTestExamp
le/java

And for
Class.forName("com.mysql.jdbc.Driver").newInstance();
This
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driv
er
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at ConnectorMXJTestExample.main(ConnectorMXJTestExample.java:10)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this