Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 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.fxml.Initializable;
  17. import javafx.scene.control.Button;
  18. import javafx.scene.control.TableColumn;
  19. import javafx.scene.control.TableView;
  20. import javafx.scene.control.TextField;
  21. import javafx.scene.control.cell.PropertyValueFactory;
  22. import model.City;
  23.  
  24. public class CityController {
  25.    
  26.    
  27.      static Vector<StringBuffer> searching= new Vector<StringBuffer>(1);
  28.      
  29.      static Vector<Integer> Cityy= new Vector<Integer>(1);
  30.      static Vector<Integer> Countryy= new Vector<Integer>(1);
  31.      
  32.      static  StringBuffer  abc=null ;
  33.      
  34.      public Button Add;
  35.      
  36.         public static TableView<City> a;
  37.        
  38.         public TextField CityID, CityName, Country;
  39.  
  40.         static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  41.         static final String DB_URL = "jdbc:mysql://mysql.agh.edu.pl:3306/gaszlar1";
  42.         static final String USER = "gaszlar1";
  43.         static final String PASS = "G5C9WwjovojKkH0T";
  44.        
  45.          public static TableColumn<City, String> colCityID;
  46.             public static TableColumn<City, String> colCityName;
  47.             public static TableColumn<City, String> colCountry;
  48.            
  49.            
  50.            
  51.            
  52.             @FXML
  53.             public void Add()
  54.             {
  55.                
  56.                 City city = new City (CityID.getText(),CityName.getText(),Country.getText());
  57.                 a.getItems().add(city);
  58.                 String aa=CityID.getText();
  59.                 String aaa=CityName.getText();
  60.                 String aaaa=Country.getText();
  61.                
  62.  
  63.                         CityID.clear();
  64.                         CityName.clear();
  65.                         Country.clear();
  66.  
  67.                    Connection conn = null;
  68.                    Statement stmt = null;
  69.                    PreparedStatement stmt1 = null;
  70.                    try{
  71.                       Class.forName("com.mysql.jdbc.Driver");
  72.                       conn = DriverManager.getConnection(DB_URL, USER, PASS);
  73.                       stmt = conn.createStatement();
  74.                         stmt.executeUpdate("INSERT INTO `gaszlar1`.`City` (`CityID`, `CityName`, `Country`) VALUES ('"+aa+"', '"+aaa+"', '"+aaaa+"');");
  75.  
  76.                       stmt.close();
  77.                    }catch(SQLException se){
  78.  
  79.                       se.printStackTrace();
  80.                    }catch(Exception e){
  81.  
  82.                       e.printStackTrace();
  83.                    }finally{
  84.  
  85.                       try{
  86.                          if(stmt!=null)
  87.                             stmt.close();
  88.                       }catch(SQLException se2){
  89.                       }// nothing we can do
  90.                       try{
  91.                          if(conn!=null)
  92.                             conn.close();
  93.                       }catch(SQLException se){
  94.                          se.printStackTrace();
  95.         }}}
  96.        
  97.             public static Vector<StringBuffer> GetCity() {
  98.                
  99.                Connection conn = null;
  100.                Statement stmt = null;
  101.                try{
  102.                   Class.forName("com.mysql.jdbc.Driver");
  103.                   conn = DriverManager.getConnection(DB_URL, USER, PASS);
  104.                   stmt = conn.createStatement();
  105.                   ResultSet rs = stmt.executeQuery("SELECT * FROM City");
  106.  
  107.                   while(rs.next()) {
  108.                       String abcd1=rs.getString("CityID");
  109.                       String abcd2=rs.getString("CityName");
  110.                       Integer x=abcd2.length();
  111.                       CityController.Cityy.addElement(x);
  112.                      
  113.                       String abcd3=rs.getString("Country");
  114.                       Integer xx=abcd3.length();
  115.                       CityController.Countryy.addElement(xx);
  116.                      
  117.                      
  118.                      CityController.abc=new StringBuffer(abcd1+abcd2+abcd3);
  119.  
  120.                     CityController.searching.addElement(abc);
  121.  
  122.                   }
  123.  
  124.                   rs.close();
  125.                   stmt.close();
  126.                }catch(SQLException se){
  127.                   //Handle errors for JDBC
  128.                   se.printStackTrace();
  129.                }catch(Exception e){
  130.                   //Handle errors for Class.forName
  131.                   e.printStackTrace();
  132.                }finally{
  133.                   //finally block used to close resources
  134.                   try{
  135.                      if(stmt!=null)
  136.                         stmt.close();
  137.                   }catch(SQLException se2){
  138.                   }// nothing we can do
  139.                   try{
  140.                      if(conn!=null)
  141.                         conn.close();
  142.                   }catch(SQLException se){
  143.                      se.printStackTrace();
  144.                   }//end finally try
  145.                }//end try
  146.             return CityController.searching;
  147.             }
  148.  
  149.            
  150.  
  151.        
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement