栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用JavaFX中的数据库填充表格视图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用JavaFX中的数据库填充表格视图

我相信这会为您提供帮助:

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");      }}

在控制器类中执行以下操作:

@FXMLvoid 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操纵的实体(表)创建一个单独的Java文件POJO

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();   }}

我已经测试了该程序,它可以完美运行。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507176.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号