无法通过命令行从数据库中提取信息

时间:2013-12-24 20:39:01

标签: java mysql

Hello mysql和Java专家,我是mysql和jsp的新学习者,虽然我对Java和SQL加上mysql有基本的大学知识。
我在mysql中创建了一个数据库'EMP'和一个表'Employees',然后创建了一个'FirstExample.java'文件,并在没有任何错误报告的情况下编译它。问题是,当我通过命令行运行'FirstExample.class'以提取数据库表内容时,我收到一个我无法理解的异常错误。

数据库 - 'EMP'。
   - “员工”。
; ID;年龄;第一;最后,
数据; 100; 28;扎伊德; Khann。
ID - '用户名'
密码 - '密码'

然后我创建了FirstExample.java
`//步骤1.导入所需的包
import java.sql。*; //用于标准JDBC程序

public class FirstExample {  
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/EMP";

// Database credentials
static final String USER = "username";
static final String PASS = "password";

public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    try{
        //STEP 2: Register JDBC driver
        Class.forName ( "com.mysql.jdbc.Driver");

        //STEP 3: Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL,USER,PASS);

        //STEP 4: Execute a query
        System.out.println("Creating Statement...");
        stmt = conn.createStatement();
        String sql;
        sql = "SELECT id, first, last, age FROM Employees";
        ResultSet rs = stmt.executeQuery(sql);

        //STEP 5: Extract from result set
        while(rs.next()){
            //Retrieve by column name
            int id = rs.getInt("id");
            int age = rs.getInt("age");
            String first = rs.getString("first");
            String last = rs.getString("last");

            //Display values
            System.out.print("ID: " + id);
            System.out.print(", Age: " + age);
            System.out.print(", First: " + first);
            System.out.print(", Last: " + last);

        }
        //STEP 6: Clean-up enviroment
        rs.close();
        stmt.close();
        conn.close();
    } catch (SQLException se) {
        //Handle errors for JDBC
        se.printStackTrace();   
    }catch (Exception e) {
        //Handle errors for Class.forName
        e.printStackTrace();
    }finally{
        //finally block used to close resources
        try{
            if (stmt !=null)
            stmt.close();
        } catch(SQLException se2) {

        }// nothing we can do
        try{
            if(conn !=null)
                conn.close();
        } catch(SQLException se){
            se.printStackTrace();
        }//end finally try
        }//end try
        System.out.println("Goodbye!"); 
}//end main  
}//end FirstExample`  

我将文件保存为: C:\ Java \ FirstExample.java然后编译为FirstExample.class

CLASSPATH: C:\ Program Files(x86)\ MySQL \ MySQL Connector J \ mysql-connector-java-5.1.27-bin.jar
CATALINA: C:\ Program Files \ Apache Software Foundation \ Tomcat 7.0 \ lib \ jsp-api.jar
JAVA_HOME: C:\ glassfish3 \ jdk
路径: C:\ glassfish3 \ jdk \ bin; C:\ Program Files(x86)\ MySQL \ MySQL Server 5.0 \ bin
MySQL目录: C:\ Program Files(x86)\ MySQL \ MySQL Server 5.0

以下是命令行错误消息:
    Exception in thread 'main' java.lang.NoClassDefFoundError: FirstExample
Caused by: java.lang.ClassNotFoundException: FirstExample
at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoder.findClass(URLClassLoader.java:190)
at java.lang.URLClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: FirstExample. Program will exit.

0 个答案:

没有答案