代码在Eclipse中运行,但在CMD中提供了“无法解析符号”的编译错误

时间:2010-08-02 07:58:02

标签: java eclipse jdbc

这是我的班级:

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class DisplayAuthors {
static final String JDBC_DRIVER = "com.mysql.jdbc.driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/myfirstdb";
public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;
    try {
        Class.forName(JDBC_DRIVER);
        connection = DriverManager.getConnection(DATABASE_URL, "root", "1234");
        statement = connection.createStatement();
        ResultSet resultset = statement.executeQuery("SELECT S_ID, S_NAME, AGE, CLASS FROM MYOWN"); 
        ResultSetMetaData metaData = resultset.getMetaData();
        int numberOfColumns = metaData.getColumnCount();
        System.out.println("Table Content");
        for(int i = 1; i<+numberOfColumns; i++)
            System.out.printf("%-8s\t", metaData.getColumnName(i));
        System.out.println();
        ResultSet resultSet;
        while (resultSet.next())
        {
            for (int i = 1; i<+numberOfColumns; i++)
                System.out.printf("%-8s\t", resultSet.getObject(i));
                System.out.println();
        }

    }
    catch ( SQLException sqlException)
    {
        sqlException.printStackTrace();
        System.exit(1);
    }
    catch ( ClassNotFoundException classNotFound)
    {
        classNotFound.printStackTrace();
        System.exit(1);
    }
    finally
    {
        try
        {
            statement.close();
            connection.close();
        }
        catch ( Exception exception )
        {
            exception.printStackTrace();
            System.exit(1);
        }
    }
}
}

这在Eclipse中运行良好,输出如下:

testing oracle-character-set-1 against <abc>
PASSED LOSSY
testing oracle-character-set-1 against <ab?c>
PASSED LOSSY
testing oracle-character-set-1 against <XY

我也尝试在CMD中编译和运行,但它提供了以下编译错误:

 C:\My Java>javac DisplayAuthors.java
 DisplayAuthors.java:43: cannot resolve symbol
 symbol  : method printf (java.lang.String,java.lang.String)
 location: class java.io.PrintStream
 System.out.printf( "%-8s\t", metaData.getColumnName( i ) );
            ^
 DisplayAuthors.java:49: cannot resolve symbol
 symbol  : method printf (java.lang.String,java.lang.Object)
 location: class java.io.PrintStream
 System.out.printf( "%-8s\t", resultSet.getObject( i ) );
            ^
 2 errors

我该如何解决这个问题?

4 个答案:

答案 0 :(得分:4)

那些编译错误意味着无法找到方法PrintStream#printf()。根据链接的javadoc,它是在Java 1.5中引入的。

  

时间:

    1.5

这意味着您在CMD中使用的是Java 1.4或更早版本。检查您的PATHJAVA_HOME环境变量,它应指向Java 1.5或更高版本。


也就是说,JDBC代码中至少存在三个主要问题:

  1. 您应该从不使用System#exit();catch块中调用finally,因为这样finally永远不会调用。因此,你仍然在泄漏连接和声明。将System#exit()放在代码的末尾。

  2. 您也忘记了close() ResultSet中的finally

  3. 关闭连接,语句和结果集应该在它自己的 try-catch块中发生,因为关闭可以抛出异常。想象一下,关闭语句会抛出异常,然后连接将永远不会被关闭。所以,请改为:

    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }
    

答案 1 :(得分:0)

您有两个变量:resultsetresultSet。第二个未使用,应删除。在您的while块中,将resultSet替换为resultset

另外,替换

static final String JDBC_DRIVER = "com.mysql.jdbc.driver";

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

答案 2 :(得分:0)

使用一系列更改更新了您的代码。现在应该至少编译:)。

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class DisplayAuthors {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DATABASE_URL = "jdbc:mysql://localhost/myfirstdb";

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName(JDBC_DRIVER);
            connection = DriverManager.getConnection(DATABASE_URL, "root",
                    "1234");
            statement = connection.createStatement();
            ResultSet resultset = statement
                    .executeQuery("SELECT S_ID, S_NAME, AGE, CLASS FROM MYOWN");
            ResultSetMetaData metaData = resultset.getMetaData();
            int numberOfColumns = metaData.getColumnCount();
            System.out.println("Table Content");
            for (int i = 1; i < +numberOfColumns; i++)
                System.out.printf("%-8s\t", metaData.getColumnName(i));
            System.out.println();
            // ResultSet resultSet;
            while (resultset.next()) {
                for (int i = 1; i < +numberOfColumns; i++)
                    System.out.printf("%-8s\t", resultset.getObject(i));
                System.out.println();
            }

        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
            System.exit(1);
        } catch (ClassNotFoundException classNotFound) {
            classNotFound.printStackTrace();
            System.exit(1);
        } finally {
            try {
                statement.close();
                connection.close();
            } catch (Exception exception) {
                exception.printStackTrace();
                System.exit(1);
            }
        }
    }
}

答案 3 :(得分:-1)

您的代码路径未构建,请尝试创建新项目并添加外部库.Npw将源代码复制粘贴到新类中。

尝试!!它为我工作