-1

I have an error using uCanaccess to retrieve my database which I have saved in my desktop.

My code is below:

package Week11Package;

import java.util.Scanner; import java.sql.*;

public class dbTest1 {

static Scanner input = new Scanner (System.in);
static String url;
static Connection aConnection;
static Statement aStatement;
static boolean gotIt = false;

public static void main(String[] args) {

    dbTest1.initialize();
}
public static void initialize() {
    //establish the DB connection.
    url = "jdbc:odbc:MS Access Database;DBQ=.//Teams.accdb";
    try {
        //load the jdbc - odbc bridge for Windows
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        //create a connection instance
        aConnection = DriverManager.getConnection("jdbc:ucanaccess:///Users/Vince/Desktop/Teams.accdb");
        //create statement object instance for this connection
        aStatement = aConnection.createStatement();
        String sqlQuery = "SELECT PlayerID, PlayerName, TeamID " +
                    "FROM PLAYER"+"'";
        ResultSet rs = aStatement.executeQuery (sqlQuery);
        gotIt = rs.next();
        if (gotIt) {
            System.out.println("Connected to DB & found Data!!!");
            System.out.println("Which Player ID are you looking for?");
            String stringpID = input.next();
            boolean found = false;
            while(gotIt){
                //extract the data
                String pID = rs.getString (1);
                if (pID.equals(stringpID))
                    found = true;
                String pName = rs.getString (2);
                String tID = rs.getString (3);
                System.out.println(pID+" " +pName+ " "+tID);
                gotIt = rs.next();
            }
            if (found)
                System.out.println("The player ws found");
        }
        aStatement.close();
        aConnection.close();
    }
catch (ClassNotFoundException e) {
        System.err.println(e);
    }
    catch (SQLException e) {
        System.err.println(e);
    } 
}

}

I would appreciate any advice to fix this error.

Thanks, Vince

Vince
  • 73
  • 1
  • 2
  • 4
  • 1
    Please post the complete stack trace. You also have a quote (') at the end of your `sqlQuery` string, isn't that the problem? – Cédric Couralet Oct 04 '14 at 10:23
  • 1
    As Cédric said, the quote at the end of your sqlQuery is uncorrect and as result you get a the message "malformed string '." – jamadei Oct 04 '14 at 15:04

1 Answers1

3

Well, first, import your libraries(the jar files in lib folder). http://sourceforge.net/projects/ucanaccess/files/UCanAccess-2.0.9.1-bin.zip/download

Then check this out. This might help.

Connection a = DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\Vince\\Desktop\\Teams.accdb");
Statement s = a.createStatement();
String sqlQuery = "SELECT PlayerID, PlayerName, TeamID " + "FROM PLAYER"+"'";
ResultSet rs = s.executeQuery(sqlQuery);
while(rs.next()){
System.out.println("Connected to DB & found Data!!!");
}