Guest User

Untitled

a guest
Jun 18th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. package sample;
  2. import java.sql.*;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7. import java.util.Vector;
  8. import model.City;
  9. import model.Airport;
  10. import java.util.ResourceBundle;
  11. import java.net.URL;
  12. import javafx.fxml.FXML;
  13. import javafx.scene.control.TextField;
  14. import javafx.scene.control.cell.PropertyValueFactory;
  15. import javafx.fxml.Initializable;
  16. import javafx.scene.control.*;import javafx.fxml.FXMLLoader;
  17. import javafx.scene.Node;
  18. import javafx.scene.Parent;
  19. import javafx.scene.Scene;
  20. import javafx.stage.Stage;
  21.  
  22. import java.awt.event.ActionEvent;
  23. import java.io.IOException;
  24.  
  25.  
  26. import javafx.collections.FXCollections;
  27. import javafx.collections.ObservableList;
  28.  
  29.  
  30. public class Controller implements Initializable {
  31.  
  32.     public Button Add, Add1;
  33.     public TableView<City> a;
  34.     public TableView<Airport> s;
  35.     public TextField CityID, CityName, Country, asd, asdd, asddd;
  36.    
  37.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  38.     static final String DB_URL = "jdbc:mysql://mysql.agh.edu.pl:3306/gaszlar1";
  39.     static final String USER = "gaszlar1";
  40.     static final String PASS = "G5C9WwjovojKkH0T";
  41.    
  42.     public TableColumn<City, String> colCityID;
  43.     public TableColumn<City, String> colCityName;
  44.     public TableColumn<City, String> colCountry;
  45.     public TableColumn<Airport, String> colAirportID;
  46.     public TableColumn<Airport, String> colAirportName;
  47.     public TableColumn<Airport, String> colCityid;
  48.    
  49.     @FXML
  50.     public void Add()
  51.     {
  52.        
  53.         City city = new City (CityID.getText(),CityName.getText(),Country.getText());
  54.         a.getItems().add(city);
  55.         String aa=CityID.getText();
  56.         String aaa=CityName.getText();
  57.         String aaaa=Country.getText();
  58.        
  59.  
  60.                 CityID.clear();
  61.                 CityName.clear();
  62.                 Country.clear();
  63.  
  64.    
  65.            Connection conn = null;
  66.            Statement stmt = null;
  67.            PreparedStatement stmt1 = null;
  68.            try{
  69.               Class.forName("com.mysql.jdbc.Driver");
  70.               conn = DriverManager.getConnection(DB_URL, USER, PASS);
  71.               stmt = conn.createStatement();
  72.                 stmt.executeUpdate("INSERT INTO `gaszlar1`.`City` (`CityID`, `CityName`, `Country`) VALUES ('"+aa+"', '"+aaa+"', '"+aaaa+"');");
  73.  
  74.               stmt.close();
  75.            }catch(SQLException se){
  76.  
  77.               se.printStackTrace();
  78.            }catch(Exception e){
  79.  
  80.               e.printStackTrace();
  81.            }finally{
  82.  
  83.               try{
  84.                  if(stmt!=null)
  85.                     stmt.close();
  86.               }catch(SQLException se2){
  87.               }// nothing we can do
  88.               try{
  89.                  if(conn!=null)
  90.                     conn.close();
  91.               }catch(SQLException se){
  92.                  se.printStackTrace();
  93. }}}
  94.    
  95.    
  96. @FXML
  97.     public void Add1()
  98.     {
  99.        
  100.         Airport airport = new Airport (asd.getText(),asdd.getText(),asddd.getText());
  101.         s.getItems().add(airport);
  102.         String aa=asd.getText();
  103.         String aaa=asdd.getText();
  104.         String aaaa=asddd.getText();
  105.        
  106.  
  107.         asd.clear();
  108.         asdd.clear();
  109.         asddd.clear();
  110.  
  111.    
  112.            Connection conn = null;
  113.            Statement stmt = null;
  114.            PreparedStatement stmt1 = null;
  115.            try{
  116.               Class.forName("com.mysql.jdbc.Driver");
  117.               conn = DriverManager.getConnection(DB_URL, USER, PASS);
  118.               stmt = conn.createStatement();
  119.                 stmt.executeUpdate("INSERT INTO `gaszlar1`.`Airport` (`AirportID`, `AirportName`, `CityID`) VALUES ('"+aa+"', '"+aaa+"', '"+aaaa+"');");
  120.  
  121.               stmt.close();
  122.            }catch(SQLException se){
  123.  
  124.               se.printStackTrace();
  125.            }catch(Exception e){
  126.  
  127.               e.printStackTrace();
  128.            }finally{
  129.  
  130.               try{
  131.                  if(stmt!=null)
  132.                     stmt.close();
  133.               }catch(SQLException se2){
  134.               }// nothing we can do
  135.               try{
  136.                  if(conn!=null)
  137.                     conn.close();
  138.               }catch(SQLException se){
  139.                  se.printStackTrace();
  140. }}}
  141.    
  142.    
  143.    
  144.     @Override
  145.     public void initialize(URL url, ResourceBundle rb)
  146.     {   ObservableList<City> o = FXCollections.observableArrayList();      
  147.         colCityID.setCellValueFactory(new PropertyValueFactory<>("CityID"));
  148.         colCityName.setCellValueFactory(new PropertyValueFactory<>("CityName"));
  149.         colCountry.setCellValueFactory(new PropertyValueFactory<>("Country"));
  150.         a.setItems(o);
  151.        
  152.         ObservableList<Airport> z = FXCollections.observableArrayList();      
  153.        // colAirportID.setCellValueFactory(new PropertyValueFactory<>("AirportID"));
  154.       //  colAirportName.setCellValueFactory(new PropertyValueFactory<>("AirportName"));
  155.       //  colCityid.setCellValueFactory(new PropertyValueFactory<>("Cityid"));
  156.         s.setItems(z);
  157.  
  158.     }
  159.  
  160.  
  161. }
Add Comment
Please, Sign In to add comment