我正在尝试将我使用Microsoft Access创建的数据库数据加载到NetBeans JavaFX程序中的表视图,但它有错误。所以如果这里有人可以帮忙解决这个问题。
TableViewController.java
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
/**
*
* @author pc
*/
public class TableViewController implements Initializable{
@FXML
TableView<InvoiceEntry> tblViewer;
@FXML
TableColumn colID;
@FXML
TableColumn colName;
@FXML
TableColumn colAuthor;
@FXML
TableColumn colPublisher;
@FXML
TableColumn colPrice;
@FXML
Button cmdTest;
public Statement st;
@Override
public void initialize(URL location, ResourceBundle resources) {
colID.setCellValueFactory(new PropertyValueFactory<InvoiceEntry, Integer>("id"));
colName.setCellValueFactory(new PropertyValueFactory<InvoiceEntry, String>("name"));
colAuthor.setCellValueFactory(new PropertyValueFactory<InvoiceEntry, String>("author"));
colPublisher.setCellValueFactory(new PropertyValueFactory<InvoiceEntry, String>("publisher"));
colPrice.setCellValueFactory(new PropertyValueFactory<InvoiceEntry, Integer>("price"));
//tblViewer.getItems().setAll(getAllBookInfo());
tblViewer.getItems().setAll(getAllBookInfo());
}
public class InvoiceEntry {
private final SimpleIntegerProperty bookId ;
private final SimpleStringProperty bookName;
private final SimpleStringProperty Bookauthor;
private final SimpleStringProperty Bookpublisher;
public SimpleStringProperty Bookprice;
public InvoiceEntry(Integer id, String name, String auth, String pub){
this.bookId = new SimpleIntegerProperty(id);
this.bookName = new SimpleStringProperty(name);
this.Bookauthor = new SimpleStringProperty(auth);
this.Bookpublisher = new SimpleStringProperty(pub);
}
public int invoiceId;
public Integer getBookId() {
return bookId.get();
}
public void setBookId(Integer id){
bookId.set(id);
}
public String getBookName() {
return bookName.get();
}
public void setBookName(String name){
bookName.set(name);
}
public String getAuthor() {
return Bookauthor.get();
}
public void setAuthor(String auth){
Bookauthor.set(auth);
}
public String getPublisher() {
return Bookpublisher.get();
}
public void setPublisher(String pub){
Bookpublisher.set(pub);
}
}
public List<String> getAllBookInfo(){
List<String> myList = new ArrayList<String>();
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection con = DriverManager.getConnection("jdbc:ucanaccess://D:\\GUI\\Library.accdb","","");
System.out.println("connected...");
st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String recordQuery = ("Select * from BookDB");
ResultSet rs = st.executeQuery(recordQuery);
while(rs.next()){
Integer id = rs.getInt("bid");
String name = rs.getString("bname");
String auth = rs.getString("author");
String pub = rs.getString("publisher");
//myList.add(new InvoiceEntry(id, name, auth, pub));
myList.add(new InvoiceEntry(id, name, auth, pub));
}
}
catch (Exception ex)
{
}
return myList;
}
}
错误讯息:
D:\GUI\JavaFXApplication24\src\javafxapplication24\TableViewController.java:61: error: no suitable method found for setAll(List<String>)
tblViewer.getItems().setAll(getAllBookInfo());
method ObservableList.setAll(TableViewController.InvoiceEntry...) is not applicable
(varargs mismatch; List<String> cannot be converted to TableViewController.InvoiceEntry)
method ObservableList.setAll(Collection<? extends TableViewController.InvoiceEntry>) is not applicable
(argument mismatch; List<String> cannot be converted to Collection<? extends TableViewController.InvoiceEntry>)
D:\GUI\JavaFXApplication24\src\javafxapplication24\TableViewController.java:130: error: no suitable method found for add(TableViewController.InvoiceEntry)
myList.add(new InvoiceEntry(id, name, auth, pub));
method Collection.add(String) is not applicable
(argument mismatch; TableViewController.InvoiceEntry cannot be converted to String)
method List.add(String) is not applicable
(argument mismatch; TableViewController.InvoiceEntry cannot be converted to String)
Note: D:\GUI\JavaFXApplication24\src\javafxapplication24\TableViewController.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors
D:\GUI\JavaFXApplication24\nbproject\build-impl.xml:931: The following error occurred while executing this line:
D:\GUI\JavaFXApplication24\nbproject\build-impl.xml:271: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)
答案 0 :(得分:0)
您有List<String> myList = new ArrayList<String>()
必须包含String
,但您正在尝试添加InvoiceEntry
,因此您会收到编译错误
myList.add(new InvoiceEntry(id, name, auth, pub));
因为它无法转换为String。
我不知道您使用的是什么IDE,但我确信IntelliJ,Eclipse和Netbeans会通知您编译错误的位置和原因。