Jump to content
  • Advertisement
Sign in to follow this  
Sparhawk42

[java] Problem with MySql and java under Windows

This topic is 4628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Obviously launcher can't find a class ConnectorMXJTestExamp
le.class because you forgot to type ;. into the classpath.

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
That last eror means that it can't find the class with the given name.
Here is what I found using google. Hope it helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!