English 中文(简体)
Error while Rum a JDBC Program
原标题:Error while Run a JDBC Program
  • 时间:2011-04-14 13:20:03
  •  标签:
  • jdbc

我正在使用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....

问题回答
java -cp .;C:Program FilesMySQLmysql-connector-java-5.1.15mysql-connector-java-5.1.15.jar MysqlConnect

尝试使用:

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

或者说,在你的班次中,添加这样的内容。

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

你目前的班子也需要上班,因此,现在的班子。

采用-cp 交换机应当工作

java -cp=.;C:Program FilesMySQLmysql-connector-java-5.1.15mysql-connector-java-5.1.15.jar MysqlConnect

It is almost never needed to set the CLASSPATH environment variable





相关问题
MaxPooledStatements setting in JDBC oracle

I can t figure out how to set MaxPooledStatements in Oracle using the Oracle thin JDBC driver. Could someone point me in the right direction?

pass ResultSet from servlet to JSP

I am doing the following in my SampleServlet.java //Fill resultset from db .... try { ArrayList Rows = new ArrayList(); while (resultSet.next()){ ArrayList row = new ArrayList(); ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

Mysql session variable in JDBC string

am using this connection string to connect to mysql from java: jdbc:mysql://localhost:3306/db?noDatetimeStringSync=true&useUnicode=yes&characterEncoding=UTF-8 is it possible to set the ...

热门标签