3

I am new to UCanAccess and am using it in the place of reinstalling Microsoft Office from 32-bit to 64-bit or Netbeans 8.0.2 from 64-bit to 32-bit.

I am running a simple program to connect to a small database and display results from it; however I have run into an error that Google has only 5 results for when I search.

Here is my whole DB class:

package highscoresproj;

import java.io.File;
import java.sql.*;

public class DB
{

    private Connection connection;
    private PreparedStatement statement;
    private ResultSet resultSet;

    public DB()
    {
        //load driver

        try
        {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            System.out.println("Driver successfully loaded");
        } catch (ClassNotFoundException c)
        {
            System.out.println("Unable to load driver\n" + c);
        }

        //connect to database
        try
        {
            connection = DriverManager.getConnection("jdbc:ucanaccess://"+ getDatabaseLocation("HighScores.accdb")+ ";showschema=true");
            System.out.println("Connection successful");
        } catch (SQLException e)
        {
            System.out.println("Unable to connect\n" + e);
        }
    }

    public ResultSet query(String stmt) throws SQLException
    {
        statement = connection.prepareStatement(stmt);
        resultSet = statement.executeQuery();
        return resultSet;
    }

    public void update(String update) throws SQLException
    {
        statement = connection.prepareStatement(update);
        statement.executeUpdate();
        statement.close();
    }

    public static String getDatabaseLocation(String relativePath)
    {

        File dbFile = new File(relativePath);
        String path = dbFile.getAbsolutePath();
        path = path.replaceAll("\\\\", "/");
        System.out.println("DB Full path:\t" + path);
        return path;
    }
}

And the method that runs when I press the button to display the top scores:

private void btnDisplayMaxActionPerformed(java.awt.event.ActionEvent evt)                                              
{                                                  
    try
    {
        res = db.query("SELECT TOP Username, Score FROM tblUser INNER JOIN tblScores ON UserID = ID");
    } 
    catch (SQLException ex)
    {
        System.out.println("An error occured. Error Message:\n" + ex);
    }
}

When I run the program and press the button, I get the following output:

run:
Driver successfully loaded
DB Full path:   C:/Users/Aaron/Documents/Somerset College/IT/Java Book/Excersises/P148 Ex5/HighScoresProj/HighScores.accdb
Connection successful
An error occured. Error Message:
net.ucanaccess.jdbc.UcanaccessSQLException: incompatible data type in operation: ; in LIMIT, OFFSET or FETCH
BUILD SUCCESSFUL (total time: 11 seconds)

Please forgive me if this is a very simple error, but I could not find anywhere specifically mentioning it. Thank you!

Greatmar2
  • 89
  • 1
  • 11

1 Answers1

2

Your SQL syntax is invalid. SELECT TOP expects to be told the number of rows you want returned. If you only want one row returned then use

SELECT TOP 1 Username, ...
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • *facepalm* I feel so stupid for missing that. But now anyone who gets that error will know to check their SQL code. XD – Greatmar2 Apr 21 '15 at 05:20