Java dasturlash tilining Javafx moduli yordamida Ma'lumotar bazasi bilan ishlash
Download 1.94 Mb.
|
BEK dasturlash 5
- Bu sahifa navigatsiya:
- HelloController.java
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 {
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;
@FXML
@FXML private TableColumn @FXML private TableColumn @FXML
@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 ObservableList Connection conn = getConnection(); String query = "SELECT * FROM xonadonlar"; Statement st; ResultSet rs; try {
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 id.setCellValueFactory(new PropertyValueFactory fullName.setCellValueFactory(new PropertyValueFactory address.setCellValueFactory(new PropertyValueFactory tableView.setItems(list); } @FXML
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
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
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
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: |
ma'muriyatiga murojaat qiling