Java dasturlash tilining Javafx moduli yordamida Ma'lumotar bazasi bilan ishlash


Download 1.94 Mb.
bet2/3
Sana18.06.2023
Hajmi1.94 Mb.
#1593429
1   2   3
Bog'liq
BEK dasturlash 5

Amaliy qism:
HelloApplication.java
package com.example.gasauthority;

import javafx.application.Application;


import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Ma'lumotlar");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {


launch();
}
}

HelloController.java
package com.example.gasauthority;

import javafx.collections.FXCollections;


import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

import javafx.scene.control.TableColumn;


import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

import javax.swing.*;


import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;

public class HelloController implements Initializable {

Connection con;
PreparedStatement pst;
@FXML
private TextField tfId;
@FXML
private TextField tfFullName;
@FXML
private TextField tfAddress;

@FXML
private TableColumn id;


@FXML
private TableColumn fullName;
@FXML
private TableColumn address;

@FXML
private TableView tableView;


@Override


public void initialize(URL url, ResourceBundle resourceBundle) {
showAdapter();
}

public Connection getConnection() {


Connection conn;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/address_database?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "");
return conn;
} catch (Exception ex) {
System.out.println("Error" + ex.getMessage());
return null;
}
}

public ObservableList getAdapter() {


ObservableList bookList = FXCollections.observableArrayList();
Connection conn = getConnection();
String query = "SELECT * FROM xonadonlar";
Statement st;
ResultSet rs;

try {
st = conn.createStatement();


rs = st.executeQuery(query);
Adapter adapter;
while (rs.next()) {
adapter = new Adapter(rs.getInt(1),
rs.getString(2),
rs.getString(3));
bookList.add(adapter);
}

} catch (Exception ex) {


ex.printStackTrace();
}
return bookList;
}

public void showAdapter() {


ObservableList list = getAdapter();
id.setCellValueFactory(new PropertyValueFactory("id"));
fullName.setCellValueFactory(new PropertyValueFactory("fullName"));
address.setCellValueFactory(new PropertyValueFactory("address"));

tableView.setItems(list);


}

@FXML
protected void onBtnSave() {


try {
String query = "INSERT INTO `xonadonlar`(`id`, `fullName`, `address`) VALUES (?,?,?)";

con = DriverManager.getConnection("jdbc:mysql://localhost/address_database?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "");


pst = con.prepareStatement(query);
pst.setString(1, null);
pst.setString(2, tfFullName.getText());
pst.setString(3, tfAddress.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Information saved!");
tfId.setText("");
tfFullName.setText("");
tfAddress.setText("");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
System.out.println(ex.getMessage());
}
showAdapter();
}

@FXML
protected void onBtnUpdate() {


try {
update();
JOptionPane.showMessageDialog(null, "Information changed!");
tfId.setText("");
tfFullName.setText("");
tfAddress.setText("");

} catch (Exception ex) {


JOptionPane.showMessageDialog(null, " Possible error when connecting to the base!\n" + ex);
}
showAdapter();
}

public void update() {


String query = "UPDATE xonadonlar SET fullName='" + tfFullName.getText() + "',address='" + tfAddress.getText()
+ "' WHERE id=" + tfId.getText() + "";
System.out.println(query);
updateMethod(query);
showAdapter();
}

public void updateMethod(String query) {


Connection con = getConnection();
Statement st;
try {
st = con.createStatement();
st.executeUpdate(query);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}

@FXML
protected void onBtnDelete() {


String query = "DELETE FROM xonadonlar WHERE ID=?";
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/address_database?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "");
pst = con.prepareStatement(query);
pst.setString(1, tfId.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Information deleted!");
tfId.setText("");
tfFullName.setText("");
tfAddress.setText("");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
showAdapter();
}

@FXML
protected void onBtnSearch() {


try {
String query = "SELECT * FROM xonadonlar WHERE id=?";

con = DriverManager.getConnection("jdbc:mysql://localhost/address_database?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "(13ekoder");


pst = con.prepareStatement(query);
pst.setString(1, tfId.getText());
var res = pst.executeQuery();

while (res.next()) {


if (res.getString(2) == null) {
JOptionPane.showMessageDialog(null, tfId.getText() + " ID does not have a user");
} else {
tfId.setText(res.getString("id"));
tfFullName.setText(res.getString(2));
tfAddress.setText(res.getString(3));
JOptionPane.showMessageDialog(null, "User found!");
}
}

} catch (Exception ex) {


System.out.println(ex);
JOptionPane.showMessageDialog(null, ex);
}
showAdapter();
}
}


Download 1.94 Mb.

Do'stlaringiz bilan baham:
1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling