Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dbtotableview;
- import java.net.URL;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ResourceBundle;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.PropertyValueFactory;
- public class FXMLDocumentController implements Initializable {
- @FXML private TableView<Product> tableView;
- @FXML private TableColumn<Product, Integer> c1;
- @FXML private TableColumn<Product, String> c2;
- @FXML private TableColumn<Product, String> c3;
- @FXML
- private void handleButtonAction(ActionEvent event) throws SQLException {}
- @Override
- public void initialize(URL location, ResourceBundle resources) {
- c1.setCellValueFactory(new PropertyValueFactory<>("id"));
- c2.setCellValueFactory(new PropertyValueFactory<>("cell1"));
- c3.setCellValueFactory(new PropertyValueFactory<>("cell2"));
- tableView.setItems(parseUserList());
- }
- private ObservableList<Product> parseUserList(){
- ObservableList<Product> products = FXCollections.observableArrayList();
- try {
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database1?serverTimezone=UTC","root","");
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM modules");
- while(rs.next()){
- int i = rs.getInt(1);
- String c = rs.getString(2);
- String cs = rs.getString(3);
- products.add(new Product(i, c, cs));
- }
- } catch (SQLException ex) {
- System.out.println(ex.getMessage());
- }
- return products;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement