在UCanAccess中执行PreparedStatement时出现“功能不受支持”错误

时间:2016-09-11 21:08:53

标签: java ms-access jdbc ucanaccess

我正在尝试使用ResultSet在GUI中填充我的组合框(在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);
    }
}

每当我尝试执行UserMenu()时,我都会收到此错误:

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

我不知道这是由什么引起的。我认为它与ucanaccess存在问题?链接到访问数据库以供参考:http://www53.zippyshare.com/v/DMLjdpDw/file.html

1 个答案:

答案 0 :(得分:3)

该异常是由声明

引起的
rs = ps.executeQuery(query);

对于开始使用PreparedStatement对象的人来说,这是一个常见的错误。

您在致电.prepareStatement时提供SQL命令文本,例如

ps = connection.prepareStatement(query);

因此,当执行PreparedStatement时,您只需调用

rs = ps.executeQuery();

由于我们从不使用参数调用PreparedStatement的.execute...方法,因此UCanAccess JDBC驱动程序会抛出“不支持的功能”异常。