代码:
private TextField tfTableName = new TextField();
private TextArea taResult = new TextArea();
private Button btShowContents = new Button("Show Contents");
private Label lblStatus = new Label();
// Statement for executing queries
private Statement stmt;
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
HBox hBox = new HBox(5);
hBox.getChildren().addAll(new Label("Table Name"), tfTableName,
btShowContents);
hBox.setAlignment(Pos.CENTER);
BorderPane pane = new BorderPane();
pane.setCenter(new ScrollPane(taResult));
pane.setTop(hBox);
pane.setBottom(lblStatus);
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 500, 200);
primaryStage.setTitle("Exercise32_05"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
initializeDB();
btShowContents.setOnAction(e -> showContents());
}
private void initializeDB() {
try {
// Load the JDBC driver
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("Driver loaded");
// Establish a connection
Connection connection = DriverManager.getConnection("jdbc:ucanaccess://C:/Data/exampleMDB.accdb");
// ("jdbc:oracle:thin:@liang.armstrong.edu:1521:ora9i",
// "scott", "tiger");
System.out.println("Database connected");
// Create a statement
stmt = connection.createStatement();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void showContents() {
String tableName = tfTableName.getText();
try {
String queryString = "select * from " + tableName;
ResultSet resultSet = stmt.executeQuery(queryString);
ResultSetMetaData rsMetaData = resultSet.getMetaData();
for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
taResult.appendText(rsMetaData.getColumnName(i) + " ");
}
taResult.appendText("\n");
// Iterate through the result and print the student names
while (resultSet.next()) {
for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
taResult.appendText(resultSet.getObject(i) + " ");
}
taResult.appendText("\n");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
/**
* The main method is only needed for the IDE with limited JavaFX support.
* Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
错误堆栈跟踪;
net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc ::: 4.0.1解码 不支持。请选择一个支持阅读的CodecProvider 当前的数据库编码。在 net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:264)
我用谷歌搜索了所有可能的解决方案,但是没有用...请帮助