我正在使用MySQL.5和Isql-linkor-java-5.1.15(两者都印有新)
I tried a JDBC Program by using Notepad and Command Prompt.
www.un.org/Depts/DGACM/index_spanish.htm My JDBC Program:
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();
}
}
}
我创建了一个名为“我方”的数据库。
I try to run this Program...
C:UsersInnodeaDesktop>javac MysqlConnect.java
C:UsersInnodeaDesktop>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:UsersInnodeaDesktop>set classpath=C:Program FilesMySQLmysql-connector-ja
va-5.1.15mysql-connector-java-5.1.15.jar
C:UsersInnodeaDesktop>javac MysqlConnect.java
C:UsersInnodeaDesktop>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.
Again also same error occurs please Help me to clear this error and Run JDBC Program....