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:/&#<span class="java-number">47</span>;"</span> + hostColonPort + <span class="java-literal">"&#<span class="java-number">47</span>;"</span> + <span class="java-literal">"?"</span>
+ <span class="java-literal">"socketFactory="</span>
+ ServerLauncherSocketFactory.<span class="java-keyword">class</span>.getName();
String userName = <span class="java-literal">"root"</span>;
String password = <span class="java-literal">""</span>;
<span class="java-keyword">Class</span>.forName(driver);
Connection conn = <span class="java-keyword">null</span>;
<span class="java-keyword">try</span> {
conn = DriverManager.getConnection(url, userName, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(<span class="java-literal">"SELECT VERSION()"</span>);
rs.next();
String version = rs.getString(<span class="java-number">1</span>);
rs.close();
stmt.close();
System.out.println(<span class="java-literal">"------------------------"</span>);
System.out.println(version);
System.out.println(<span class="java-literal">"------------------------"</span>);
} <span class="java-keyword">finally</span> {
<span class="java-keyword">try</span> {
conn.close();
} <span class="java-keyword">catch</span> (Exception e) {
e.printStackTrace();
}
ServerLauncherSocketFactory.shutdown(hostColonPort);
}
}
}
</pre></div><!--ENDSCRIPT-->
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!
[java] Problem with MySql and java under Windows
Hello, i just tried to get the first example to work:
Obviously launcher can't find a class ConnectorMXJTestExamp
le.class because you forgot to type ;. into the classpath.
le.class because you forgot to type ;. into the classpath.
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)
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.
Here is what I found using google. Hope it helps.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement