开发者

Error while Run a JDBC Program

I am using MySQL 5.5 and mysql-connector-java-5.1.15.(both are Installed new)

I tried a JDBC Program by using Notepad and Command Prompt.

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 have created a database named employement in mysql....

I try to run this Program...

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.loadCla开发者_运维问答ss(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.

How to clear this error?

Also I have tried as follows,

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.

Again also same error occurs please Help me to clear this error and Run JDBC Program....


java -cp .;C:\Program Files\MySQL\mysql-connector-java-5.1.15\mysql-connector-java-5.1.15.jar MysqlConnect


try using this -

java -cp %CLASSPATH%;.; MysqlConnect.java

Or add . to your classpath to read it like

set classpath=C:\Program Files\MySQL\mysql-connector-ja va-5.1.15\mysql-connector-java-5.1.15.jar;.;

Your current class also needs to be on classpath and hence the dot - current directory.


Using the -cp switch should work

java -cp=.;C:\Program Files\MySQL\mysql-connector-java-5.1.15\mysql-connector-java-5.1.15.jar MysqlConnect

It is almost never needed to set the CLASSPATH environment variable

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜