Guest User

Untitled

a guest
May 16th, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. /*
  2. * Eliezer Encarnacion
  3. * CSC-112-D01
  4. * Final Exam
  5. * Email: eeencarnacion0001@student.stcc.edu
  6. * ZipCode Translation DataBase Connection
  7. */
  8.  
  9. import java.sql.*;
  10. import java.util.ArrayList;
  11.  
  12. import javafx.scene.control.TextField;
  13.  
  14. public class ZipCodeDataProcess {
  15.  
  16. public static final String dataBase = "silvestri";
  17. public static final String userName = "readonly";
  18. public static final String passWord = "readonly";
  19. static String driver = "com.mysql.jdbc.Driver";
  20. static String url = "jdbc:mysql://cs.stcc.edu/" + dataBase + "?user=" + userName + "&password=" + passWord +
  21. "&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  22.  
  23. public ZipCodeDataProcess() {
  24.  
  25. }
  26.  
  27. public String zipToCityProcess(String zipC, String columnLbl, TextField status) {
  28. String columnValue = null;
  29. PreparedStatement sqlState = null;
  30. try {
  31. Connection connection = DriverManager.getConnection(url);
  32. System.out.println("Connected to Database");
  33. String select = "SELECT * From silvestri.Zipcodes WHERE zipcode= ?";
  34. sqlState = connection.prepareStatement(select);
  35. sqlState.setString(1, zipC);
  36. ResultSet rows = sqlState.executeQuery();
  37.  
  38. if (!rows.next())
  39. status.setText("Zip Code does not exist in Database");
  40. rows.beforeFirst();
  41. while (rows.next()) {
  42. columnValue = rows.getString(columnLbl);
  43. }
  44. connection.close();
  45. System.out.println("Closed Connection");
  46.  
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. }
  50. return columnValue;
  51. }
  52.  
  53. public ArrayList cityToZipProcess (String city, String state, TextField status) {
  54. ArrayList zipCodes = new ArrayList();
  55. PreparedStatement sqlState = null;
  56. try {
  57. Connection connection = DriverManager.getConnection(url);
  58. System.out.println("Connected to Database");
  59. String select = "SELECT * From silvestri.Zipcodes WHERE state= ? and city= ?";
  60. sqlState = connection.prepareStatement(select);
  61. sqlState.setString(1, state);
  62. sqlState.setString(2, city);
  63. ResultSet rows = sqlState.executeQuery();
  64. if (!rows.next())
  65. status.setText("Either state or city does not exist. Please check and try again");
  66. rows.beforeFirst();
  67. while (rows.next()) {
  68. zipCodes.add(rows.getString("zipcode"));
  69. }
  70. connection.close();
  71. System.out.println("Closed Connection");
  72.  
  73. } catch (SQLException e) {
  74. e.printStackTrace();
  75. System.out.println(e + "error");
  76. }
  77. return zipCodes;
  78. }
  79. }
Add Comment
Please, Sign In to add comment