Advertisement
BorislavaYordanova

Untitled

Mar 31st, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. package ClothingItemShop;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9.  
  10.  
  11. public class SQLconnection
  12. {
  13. static ArrayList<ClothingItemShop> items = new ArrayList<ClothingItemShop>();
  14.  
  15. public static void main(String [] args)
  16. {
  17.  
  18.  
  19. Hat new_hat1 = new Hat(10, 1, "S", "Black", "none", "Cotton");
  20. items.add(new_hat1);
  21. Suite new_suite1 = new Suite(15, 2, "M", "Grey", "none", "Cotton");
  22. items.add(new_suite1);
  23. EveningDress new_dress1 = new EveningDress(25, 1, "X", "Red", "none", "Cotton");
  24. items.add(new_dress1);
  25.  
  26. Connection connection = null;
  27. Statement statement = null;
  28.  
  29. try {
  30. Class.forName("com.mysql.jdbc.Driver");
  31. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/clothingitem", "root", "");
  32.  
  33. statement = connection.createStatement();
  34. ResultSet result = statement.executeQuery(" SELECT * FROM clothingitem ");
  35.  
  36. while (result.next())
  37. {
  38.  
  39. int id = result.getInt("id");
  40. String type = result.getString("type");
  41. float price = result.getFloat("price");
  42. float weight = result.getFloat("weight");
  43. String size = result.getString("size");
  44. String color = result.getString("color");
  45. String colorPattern = result.getString("color_pattern");
  46. String fabric = result.getString("fabric");
  47. System.out.println( "ID = " + id );
  48. System.out.println( "Type = " + type );
  49. System.out.println( "Price = " + price );
  50. System.out.println( "Weight = " + weight );
  51. System.out.println( "Size = " + size );
  52. System.out.println( "Color = " + color );
  53. System.out.println( "Color Pattern = " + colorPattern );
  54. System.out.println( "Fabric = " + fabric );
  55. System.out.println("______________________________________");
  56.  
  57. if (type.equals("Hat"))
  58. {
  59. Hat new_hat = new Hat(price, weight, size, color, colorPattern, fabric);
  60. items.add(new_hat);
  61. }
  62. else if (type.equals("Socks"))
  63. {
  64. Socks new_socks = new Socks(price, weight, size, color, colorPattern, fabric);
  65. items.add(new_socks);
  66. }
  67. else if (type.equals("Dress"))
  68. {
  69. EveningDress new_eveningDress = new EveningDress(price, weight, size, color, colorPattern, fabric);
  70. items.add(new_eveningDress);
  71. }
  72. else if (type.equals("Tie"))
  73. {
  74. Tie new_tie = new Tie(price, weight, size, color, colorPattern, fabric);
  75. items.add(new_tie);
  76. }
  77. else if (type.equals("Suite"))
  78. {
  79. Suite new_suite = new Suite(price, weight, size, color, colorPattern, fabric);
  80. items.add(new_suite);
  81. }
  82. else
  83. {
  84. System.out.println("Sorry, there is no such item");
  85. }
  86. }
  87. result.close();
  88. statement.close();
  89. connection.close();
  90. }
  91.  
  92. catch (SQLException ex)
  93. {
  94. System.out.println("No successful connection");
  95. System.out.println("SQLException: " + ex.getMessage());
  96. System.out.println("SQLState: " + ex.getSQLState());
  97. System.out.println("VendorError: " + ex.getErrorCode());
  98. }
  99. catch (ClassNotFoundException x_not_found)
  100. {
  101. System.out.println("Class not found");
  102. }
  103.  
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement