tags:

views:

76

answers:

3

Here is my class:

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);
        }
    }
}
}

This runs fine in Eclipse and outputs as follows:

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

I also tried to compile and run in CMD, but it gives the following compilation errors:

 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

How can I fix this?

A: 

You have 2 variables: resultset and resultSet. The second is not used, and should be deleted. And in your while block replace resultSet with resultset.

Also, replace the line

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

with

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
True Soft
A: 

Updated your code with Couple of changes. It should atleast compile now :).

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);
            }
        }
    }
}
YoK
+2  A: 

Those compilation errors means that the method PrintStream#printf() cannot be found. As per the linked javadoc it was introduced in Java 1.5.

Since:

    1.5

This means that you're using Java 1.4 or older in CMD. Check your PATH and JAVA_HOME environment variables, it should point to Java 1.5 or newer.


That said, there are at least three other major problems in your JDBC code:

  1. You should never call System#exit(); in a catch block with a finally, because this way the finally will never be invoked. Here you're thus still leaking the connection and statement. Put the System#exit() at end of the code instead.

  2. You forgot to close() the ResultSet in finally as well.

  3. Closing of connection, statement and resultset should each happen in its own try-catch block, because closing can throw an exception. Imagine that closing the statement throws an exception, then the connection will never be closed. So, do this instead:

    } 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) {}
    }
    
BalusC