我使用MySQL 5.5和MySQL連接器的Java-5.1.15。(兩者都安裝了新)錯誤而運行JDBC程序
我嘗試了JDBC程序通過使用記事本和命令提示符。
我的JDBC程序:
import java.sql.*;
import java.io.*;
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "employement";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "12345";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
我創建了一個名爲到工作在MySQL數據庫....
我嘗試運行此程序...
C:\Users\Innodea\Desktop>javac MysqlConnect.java
C:\Users\Innodea\Desktop>java MysqlConnect
Exception in thread "main" java.lang.NoClassDefFoundError: MysqlConnect
Caused by: java.lang.ClassNotFoundException: MysqlConnect
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: MysqlConnect. Program will exit.
如何清除這個錯誤?
而且我已經試過如下,
C:\Users\Innodea\Desktop>set classpath=C:\Program Files\MySQL\mysql-connector-ja
va-5.1.15\mysql-connector-java-5.1.15.jar
C:\Users\Innodea\Desktop>javac MysqlConnect.java
C:\Users\Innodea\Desktop>java MysqlConnect
Exception in thread "main" java.lang.NoClassDefFoundError: MysqlConnect
Caused by: java.lang.ClassNotFoundException: MysqlConnect
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: MysqlConnect. Program will exit.
同樣也同樣發生錯誤,請幫我清除此錯誤和運行JDBC程序....