Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ClothingItemShop;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.ArrayList;
- public class SQLconnection
- {
- static ArrayList<ClothingItemShop> items = new ArrayList<ClothingItemShop>();
- public static void main(String [] args)
- {
- Hat new_hat1 = new Hat(10, 1, "S", "Black", "none", "Cotton");
- items.add(new_hat1);
- Suite new_suite1 = new Suite(15, 2, "M", "Grey", "none", "Cotton");
- items.add(new_suite1);
- EveningDress new_dress1 = new EveningDress(25, 1, "X", "Red", "none", "Cotton");
- items.add(new_dress1);
- Connection connection = null;
- Statement statement = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/clothingitem", "root", "");
- statement = connection.createStatement();
- ResultSet result = statement.executeQuery(" SELECT * FROM clothingitem ");
- while (result.next())
- {
- int id = result.getInt("id");
- String type = result.getString("type");
- float price = result.getFloat("price");
- float weight = result.getFloat("weight");
- String size = result.getString("size");
- String color = result.getString("color");
- String colorPattern = result.getString("color_pattern");
- String fabric = result.getString("fabric");
- System.out.println( "ID = " + id );
- System.out.println( "Type = " + type );
- System.out.println( "Price = " + price );
- System.out.println( "Weight = " + weight );
- System.out.println( "Size = " + size );
- System.out.println( "Color = " + color );
- System.out.println( "Color Pattern = " + colorPattern );
- System.out.println( "Fabric = " + fabric );
- System.out.println("______________________________________");
- if (type.equals("Hat"))
- {
- Hat new_hat = new Hat(price, weight, size, color, colorPattern, fabric);
- items.add(new_hat);
- }
- else if (type.equals("Socks"))
- {
- Socks new_socks = new Socks(price, weight, size, color, colorPattern, fabric);
- items.add(new_socks);
- }
- else if (type.equals("Dress"))
- {
- EveningDress new_eveningDress = new EveningDress(price, weight, size, color, colorPattern, fabric);
- items.add(new_eveningDress);
- }
- else if (type.equals("Tie"))
- {
- Tie new_tie = new Tie(price, weight, size, color, colorPattern, fabric);
- items.add(new_tie);
- }
- else if (type.equals("Suite"))
- {
- Suite new_suite = new Suite(price, weight, size, color, colorPattern, fabric);
- items.add(new_suite);
- }
- else
- {
- System.out.println("Sorry, there is no such item");
- }
- }
- result.close();
- statement.close();
- connection.close();
- }
- catch (SQLException ex)
- {
- System.out.println("No successful connection");
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- catch (ClassNotFoundException x_not_found)
- {
- System.out.println("Class not found");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement