Вопрос:

Java Connection With Access

java compiler-errors

70 просмотра

1 ответ

1 Репутация автора

public class BDconnection {

public static void main(String[] args) {

    // variables
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    // Step 1: Loading or registering Oracle JDBC driver class
    try {

        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    }
    catch(ClassNotFoundException cnfex) {

        System.out.println("Problem in loading or registering MS Access JDBC driver");
        cnfex.printStackTrace();
    }

    // Step 2: Opening database connection
    try {

        String msAccessDBName = "C://users//ELMAAYAR//Desktop//Player.accdb";
        String dbURL = "jdbc:ucanaccess://" + msAccessDBName; 

        // Step 2.A: Create and get connection using DriverManager class
        connection = DriverManager.getConnection(dbURL);
        statement = connection.createStatement();

        // Step 2.C: Executing SQL & retrieve data into ResultSet
        resultSet = statement.executeQuery("SELECT * FROM PLAYER");

        System.out.println("ID\tName\t\t\tAge\tMatches");
        System.out.println("==\t================\t===\t=======");

        // processing returned data and printing into console
        while(resultSet.next()) {
            System.out.println(resultSet.getInt(1) + "\t" + 
                    resultSet.getString(2) + "\t" + 
                    resultSet.getString(3) + "\t" +
                    resultSet.getString(4));
        }
    }
    catch(SQLException sqlex){
        sqlex.printStackTrace();
    }
    finally {

        // Step 3: Closing database connection
        try {
            if(null != connection) {

                // cleanup resources, once after processing
                resultSet.close();
                statement.close();

                // and then finally close connection
                connection.close();
            }
        }
        catch (SQLException sqlex) {
            sqlex.printStackTrace();
        }
    }
}

}

Exception:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token(s), misplaced construct(s)
    Syntax error on tokens, Statement expected instead
    String literal is not properly closed by a double-quote
    String literal is not properly closed by a double-quote

    at model.BDconnection.main(BDconnection.java:33)
Автор: Youssef Источник Размещён: 08.11.2017 11:20

Ответы (1)


0 плюса

616 Репутация автора

Seems like you are not able to create connection because of the path. So Modify the path like below and try. Make sure you clean/build your project before running it to get rid of Unresolved compilation problems.

String msAccessDBName = "C:/users/ELMAAYAR/Desktop/Player.accdb";
String dbURL = "jdbc:ucanaccess://" + msAccessDBName; 
Автор: utpal416 Размещён: 09.11.2017 12:44
Вопросы из категории :
32x32