在 JavaFX 中使用数据库填充 tableview [英] Populate a tableview using database in JavaFX
问题描述
我开始学习 javaFX,我需要用我的数据库中的数据填充一个表.我在网上阅读了很多代码,但我还没有找到我想要的.我读了 this 但我不知道如何实现最后一个功能.我阅读了一些其他代码来做到这一点,到目前为止这是我的一些代码:
I'm starting to learn javaFX and I need to populate a table with data from my database. I've read a lot of code online, but I haven't found what I was looking for. I read this but I don't know how to implement that last function. I read some other code to do that and so far this is some of my code:
@FXML private TableView<User> table;
@FXML private TableColumn<User, String> nameCol;
@FXML private TableColumn<User, String> emailCol;
private ObservableList<User> data;
public void initialize(URL location, ResourceBundle resources) {
nameCol.setCellValueFactory(new PropertyValueFactory("name"));
emailCol.setCellValueFactory(new PropertyValueFactory("email"));
buildData();
}
public void buildData() {
Connection connect = new Connection();
Statement st = connect.Connect();
data = FXCollections.observableArrayList();
try {
ResultSet rs = st.executeQuery("SELECT * FROM USER");
while (rs.next()) {
ObservableList<User> row = FXCollections.observableArrayList();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
row.add(rs.getString(i));
System.out.println(row);
}
data.add(pol);
}
tabla.setItems(data);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
希望你能帮到我
推荐答案
我相信这会对您有所帮助:
I'm sure this will help you:
public class DBClass{
public Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://192.168.0.1:3306/dbname","mysqluser","mysqluserpwd");
}
}
在控制器类中执行以下操作:
In Controller Class do the following :
@FXML
void initialize(){
assert tableview != null : "fx:id="tableview" was not injected: check your FXML file 'UserMaster.fxml'.";
colUserName.setCellValueFactory(
new PropertyValueFactory<Usermaster,String>("userName"));
colPassword.setCellValueFactory(
new PropertyValueFactory<Usermaster,String>("userPassword"));
colUserType.setCellValueFactory(
new PropertyValueFactory<Usermaster,String>("userType"));
colPhoto.setCellValueFactory(
new PropertyValueFactory<Object,ImageView>("userPhoto"));
objDbClass = new DBClass();
try{
con = objDbClass.getConnection();
buildData();
}
catch(ClassNotFoundException ce){
logger.info(ce.toString());
}
catch(SQLException ce){
logger.info(ce.toString());
}
}
private ObservableList<Usermaster> data;
public void buildData(){
data = FXCollections.observableArrayList();
try{
String SQL = "Select * from usermaster Order By UserName";
ResultSet rs = con.createStatement().executeQuery(SQL);
while(rs.next()){
Usermaster cm = new Usermaster();
cm.userId.set(rs.getInt("UserId"));
Image img = new Image("tailoring/UserPhoto/User"+cm.getUserId().toString()+".jpg");
ImageView mv = new ImageView();
mv.setImage(img);
mv.setFitWidth(70);
mv.setFitHeight(80);
cm.userPhoto.set(mv);
cm.userName.set(rs.getString("UserName"));
cm.userPassword.set(rs.getString("UserPassword"));
cm.userType.set(rs.getString("UserType"));
data.add(cm);
}
tableview.setItems(data);
}
catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
并为每个要使用 TableView 操作的实体(表)创建一个 POJO 一个单独的 java 文件
and Create a POJO a seperate java file for every entity(table) to want to manipulate using TableView
public class Usermaster{
public SimpleIntegerProperty userId = new SimpleIntegerProperty();
public ObjectProperty userPhoto = new SimpleObjectProperty();
public SimpleStringProperty userName = new SimpleStringProperty();
public SimpleStringProperty userPassword = new SimpleStringProperty();
public SimpleStringProperty userType = new SimpleStringProperty();
public SimpleStringProperty encPass = new SimpleStringProperty();
public SimpleStringProperty updt = new SimpleStringProperty();
public SimpleStringProperty uptm = new SimpleStringProperty();
public Integer getUserId() {
return userId.get();
}
public Object getUserPhoto() {
return userPhoto.get();
}
public String getUserName() {
return userName.get();
}
public String getUserPassword() {
return userPassword.get();
}
public String getUserType() {
return userType.get();
}
public String getEncPass() {
return encPass.get();
}
public String getUpdt() {
return updt.get();
}
public String getUptm() {
return uptm.get();
}
}
我已经测试过这个程序运行良好.
I have tested this program it working perfectly.
这篇关于在 JavaFX 中使用数据库填充 tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!