Search code examples
javams-accessjdbcucanaccess

"feature not supported" error when executing a PreparedStatement in UCanAccess


I am trying to populate my combobox in a GUI using ResultSet (supported on UCanAccess)

package Vegan;

import java.sql.Connection;
import java.sql.DriverManager;


public class connectionString {

static Connection connection = null;

public static Connection getConnection()
{
    try
    {
        connection = DriverManager.getConnection("jdbc:ucanaccess://C:/Mo//MyDatabase1.accdb");
        System.out.println("---connection succesful---");
    }

    catch (Exception ex)
    {
        System.out.println("Connection Unsuccesful");
    }

    return connection;
}

 

package Vegan;

import static java.nio.file.Files.list;
import static java.rmi.Naming.list;
import java.sql.*;
import java.util.ArrayList;
import static java.util.Collections.list;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;


public class DB {


private static ResultSet rs = null;
private static PreparedStatement ps = null;
private static Connection connection = null;
private int StoreID;
private String userID;

public DB() {
    connection = connectionString.getConnection();
}


public void getCat(JComboBox box) throws SQLException {
    //String sql = "SELECT CategoryName FROM CategoryTbl GROUP BY CategoryName";


    String query = "SELECT CategoryName FROM CategoryTbl";
    ps = connection.prepareStatement(query);

    rs = ps.executeQuery(query);

    while (rs.next()) {
        String groupName = rs.getString("CategoryName");
        box.addItem(groupName);


    }

 

package Vegan;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import net.proteanit.sql.DbUtils;


public class UserMenu extends javax.swing.JFrame {

/**
 * Creates new form UserMenu
 */
public UserMenu() {
    try {
        initComponents();
        DB db = new DB();
        db.getCat(cmbCategory);


    } catch (SQLException ex) {
        Logger.getLogger(UserMenu.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Every time I try executing UserMenu() I get this error:

run:
---connection succesful---
Sep 11, 2016 11:04:47 PM Vegan.UserMenu <init>
SEVERE: null
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 feature not supported
at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:211)
at Vegan.DB.getCat(DB.java:203)
at Vegan.UserMenu.<init>(UserMenu.java:28)
at Vegan.UserMenu$4.run(UserMenu.java:179)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:719)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.sql.SQLFeatureNotSupportedException: feature not supported
at org.hsqldb.jdbc.JDBCUtil.notSupported(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.executeQuery(Unknown Source)
at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:208)
... 17 more

I have no idea what this is being caused by. I assume its an issue with ucanaccess? Link to access database for reference: http://www53.zippyshare.com/v/DMLjdpDw/file.html


Solution

  • The exception is caused by the statement

    rs = ps.executeQuery(query);
    

    It is a common error for people starting to work with PreparedStatement objects.

    You supply the SQL command text when you call .prepareStatement, e.g.

    ps = connection.prepareStatement(query);
    

    So, when the time comes to execute the PreparedStatement you simply call

    rs = ps.executeQuery();
    

    Since we never call an .execute... method of a PreparedStatement with a parameter, the UCanAccess JDBC driver throws a "feature not supported" exception.