Class FXMLDocumentControlle.java package tableviewusers; import java.net.URL; import java.sql.Connection; import java.sql.PreparedState...
***********************************************************************************************
Class mysqlconnect.java
package tableviewusers; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javax.swing.JOptionPane; /** * * @author amir */ public class mysqlconnect { Connection conn = null; public static Connection ConnectDb(){ try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/market","root",""); // JOptionPane.showMessageDialog(null, "Connection Established"); return conn; } catch (Exception e) { JOptionPane.showMessageDialog(null, e); return null; } } public static ObservableList<users> getDatausers(){ Connection conn = ConnectDb(); ObservableList<users> list = FXCollections.observableArrayList(); try { PreparedStatement ps = conn.prepareStatement("select * from users"); ResultSet rs = ps.executeQuery(); while (rs.next()){ list.add(new users(Integer.parseInt(rs.getString("user_id")), rs.getString("username"), rs.getString("password"), rs.getString("email"), rs.getString("type"))); } } catch (Exception e) { } return list; } }
***********************************************************************************************
Class users.java
package tableviewusers;
/**
*
* @author amir
*/
public class users {
int id ;
String username, password, email , type;
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setEmail(String email) {
this.email = email;
}
public void setType(String type) {
this.type = type;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
public String getType() {
return type;
}
public users(int id, String username, String password, String email, String type) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.type = type;
}
}
***********************************************************************************************
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tableviewusers.FXMLDocumentController">
<children>
<AnchorPane layoutY="6.0" prefHeight="543.0" prefWidth="228.0" style="-fx-background-color: #28252e;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Button layoutX="24.0" layoutY="320.0" mnemonicParsing="false" onAction="#Add_users" prefHeight="40.0" prefWidth="180.0" style="-fx-background-color: green;" text="Add" />
<Button layoutX="24.0" layoutY="376.0" mnemonicParsing="false" onAction="#Edit" prefHeight="40.0" prefWidth="180.0" style="-fx-background-color: Orange;" text="Update" />
<Button layoutX="24.0" layoutY="430.0" mnemonicParsing="false" onAction="#Delete" prefHeight="40.0" prefWidth="180.0" style="-fx-background-color: red;" text="Delete" />
<TextField fx:id="txt_username" layoutX="24.0" layoutY="60.0" onKeyReleased="#search_user" prefHeight="30.0" prefWidth="180.0" promptText="Username" />
<TextField fx:id="txt_password" layoutX="24.0" layoutY="110.0" prefHeight="30.0" prefWidth="180.0" promptText="Password" />
<TextField fx:id="txt_email" layoutX="24.0" layoutY="159.0" prefHeight="30.0" prefWidth="180.0" promptText="email" />
<TextField fx:id="txt_type" layoutX="24.0" layoutY="207.0" prefHeight="30.0" prefWidth="180.0" promptText="type" />
<TextField fx:id="txt_id" layoutX="24.0" layoutY="21.0" prefHeight="23.0" prefWidth="180.0" promptText="ID" />
</children>
</AnchorPane>
<TableView fx:id="table_users" layoutX="228.0" layoutY="76.0" onMouseClicked="#getSelected" prefHeight="474.0" prefWidth="469.0">
<columns>
<TableColumn fx:id="col_id" prefWidth="75.0" text="ID" />
<TableColumn fx:id="col_username" prefWidth="75.0" text="Username" />
<TableColumn fx:id="col_password" prefWidth="92.0" text="Password" />
<TableColumn fx:id="col_email" prefWidth="97.0" text="Email" />
<TableColumn fx:id="col_type" prefWidth="115.0" text="Type" />
</columns>
</TableView>
<TextField fx:id="filterField" layoutX="350.0" layoutY="30.0" promptText="Search" />
</children>
</AnchorPane>
***********************************************************************************************
Class TableViewUsers.java
package tableviewusers;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author amir
*/
public class TableViewUsers extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
COMMENTS