我正在尝试使用ResultSet(在UCanAccess上支持)在GUI中填充我的组合框
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
最佳答案
异常是由以下语句引起的
rs = ps.executeQuery(query);
对于开始使用PreparedStatement对象的人来说,这是一个常见错误。
您在调用
.prepareStatement
时提供SQL命令文本,例如ps = connection.prepareStatement(query);
因此,当需要执行PreparedStatement时,您只需调用
rs = ps.executeQuery();
由于我们从不使用参数调用PreparedStatement的
.execute...
方法,因此UCanAccess JDBC驱动程序将引发“功能不受支持”异常。关于java - 在UCanAccess中执行PreparedStatement时出现“功能不受支持”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39440680/