Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. package sample;
  2.  
  3. import java.net.URL;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ResourceBundle;
  11. import java.util.Vector;
  12.  
  13. import javafx.collections.FXCollections;
  14. import javafx.collections.ObservableList;
  15. import javafx.fxml.FXML;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.TableColumn;
  18. import javafx.scene.control.TableView;
  19. import javafx.scene.control.TextField;
  20. import javafx.scene.control.cell.PropertyValueFactory;
  21. import model.City;
  22.  
  23. public class CityController {
  24.    
  25.    
  26.      static Vector<StringBuffer> searching= new Vector<StringBuffer>(1);
  27.      
  28.      Vector<Integer> Cityy= new Vector<Integer>(1);
  29.       Vector<Integer> Countryy= new Vector<Integer>(1);
  30.      
  31.        StringBuffer  abc=null ;
  32.      
  33.      public Button Add;
  34.      
  35.         public TableView<City> a;
  36.        
  37.         public TextField CityID, CityName, Country;
  38.  
  39.         static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  40.         static final String DB_URL = "jdbc:mysql://mysql.agh.edu.pl:3306/gaszlar1";
  41.         static final String USER = "gaszlar1";
  42.         static final String PASS = "G5C9WwjovojKkH0T";
  43.        
  44.          public TableColumn<City, String> colCityID;
  45.             public TableColumn<City, String> colCityName;
  46.             public TableColumn<City, String> colCountry;
  47.            
  48.            
  49.            
  50.            
  51.             @FXML
  52.             public void Add()
  53.             {
  54.                
  55.                 City city = new City (CityID.getText(),CityName.getText(),Country.getText());
  56.                 a.getItems().add(city);
  57.                 String aa=CityID.getText();
  58.                 String aaa=CityName.getText();
  59.                 String aaaa=Country.getText();
  60.                
  61.  
  62.                         CityID.clear();
  63.                         CityName.clear();
  64.                         Country.clear();
  65.  
  66.                    Connection conn = null;
  67.                    Statement stmt = null;
  68.                    PreparedStatement stmt1 = null;
  69.                    try{
  70.                       Class.forName("com.mysql.jdbc.Driver");
  71.                       conn = DriverManager.getConnection(DB_URL, USER, PASS);
  72.                       stmt = conn.createStatement();
  73.                         stmt.executeUpdate("INSERT INTO `gaszlar1`.`City` (`CityID`, `CityName`, `Country`) VALUES ('"+aa+"', '"+aaa+"', '"+aaaa+"');");
  74.  
  75.                       stmt.close();
  76.                    }catch(SQLException se){
  77.  
  78.                       se.printStackTrace();
  79.                    }catch(Exception e){
  80.  
  81.                       e.printStackTrace();
  82.                    }finally{
  83.  
  84.                       try{
  85.                          if(stmt!=null)
  86.                             stmt.close();
  87.                       }catch(SQLException se2){
  88.                       }// nothing we can do
  89.                       try{
  90.                          if(conn!=null)
  91.                             conn.close();
  92.                       }catch(SQLException se){
  93.                          se.printStackTrace();
  94.         }}}
  95.        
  96.             public static Vector<StringBuffer> GetCity() {
  97.                
  98.                Connection conn = null;
  99.                Statement stmt = null;
  100.                try{
  101.                   Class.forName("com.mysql.jdbc.Driver");
  102.                   conn = DriverManager.getConnection(DB_URL, USER, PASS);
  103.                   stmt = conn.createStatement();
  104.                   ResultSet rs = stmt.executeQuery("SELECT * FROM City");
  105.  
  106.                   while(rs.next()) {
  107.                       String abcd1=rs.getString("CityID");
  108.                       String abcd2=rs.getString("CityName");
  109.                       Integer x=abcd2.length();
  110.                       Controller.Cityy.addElement(x);
  111.                      
  112.                       String abcd3=rs.getString("Country");
  113.                       Integer xx=abcd3.length();
  114.                       Controller.Countryy.addElement(xx);
  115.                      
  116.                      
  117.                       Controller.abc=new StringBuffer(abcd1+abcd2+abcd3);
  118.  
  119.                       Controller.searching.addElement(abc);
  120.  
  121.                   }
  122.  
  123.                   rs.close();
  124.                   stmt.close();
  125.                }catch(SQLException se){
  126.                   //Handle errors for JDBC
  127.                   se.printStackTrace();
  128.                }catch(Exception e){
  129.                   //Handle errors for Class.forName
  130.                   e.printStackTrace();
  131.                }finally{
  132.                   //finally block used to close resources
  133.                   try{
  134.                      if(stmt!=null)
  135.                         stmt.close();
  136.                   }catch(SQLException se2){
  137.                   }// nothing we can do
  138.                   try{
  139.                      if(conn!=null)
  140.                         conn.close();
  141.                   }catch(SQLException se){
  142.                      se.printStackTrace();
  143.                   }//end finally try
  144.                }//end try
  145.             return Controller.searching;
  146.             }
  147.  
  148.            
  149.             @Override
  150.             public void initialize(URL url, ResourceBundle rb)
  151.                 {  
  152.             ObservableList<City> o = FXCollections.observableArrayList();      
  153.             colCityID.setCellValueFactory(new PropertyValueFactory<>("CityID"));
  154.             colCityName.setCellValueFactory(new PropertyValueFactory<>("CityName"));
  155.             colCountry.setCellValueFactory(new PropertyValueFactory<>("Country"));
  156.             a.setItems(o);
  157.            
  158.            
  159.             Controller.GetCity();
  160.             for(int i=0; i<(Controller.searching.size());i++)
  161.                 {
  162.                 String abca= searching.get(i).toString();
  163.                 String from=abca.substring(0, 3);
  164.                
  165.                 Integer aa= Controller.Cityy.get(i);
  166.                 String where=abca.substring(3, 3+aa);
  167.                
  168.                 Integer aaa= Controller.Countryy.get(i);
  169.                 String fl=abca.substring(3+aa,3+ aa + aaa);
  170.                
  171.                 o.add(new City(from,where,fl));
  172.                
  173.                 }
  174.                 }
  175.        
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement