Advertisement
BorislavaYordanova

Untitled

Mar 17th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. package SQLconnection;
  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.  
  9.  
  10. public class SQLconnection
  11. {
  12. public static void main(String args[])
  13. {
  14.  
  15. Connection connection = null;
  16. Statement statement = null;
  17.  
  18. try {
  19. Class.forName("com.mysql.jdbc.Driver");
  20. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClothingItem", "root", "");
  21.  
  22. System.out.println("Success");
  23. statement = connection.createStatement();
  24. ResultSet result = statement.executeQuery(" SELECT * FROM ClothingItem; ");
  25.  
  26. while (result.next())
  27. {
  28. int id = result.getInt("id");
  29. String type = result.getString("type");
  30. float price = result.getFloat("price");
  31. float weight = result.getFloat("weight");
  32. String size = result.getString("size");
  33. String color = result.getString("color");
  34. String colorPattern = result.getString("color_pattern");
  35. String fabric = result.getString("fabric");
  36. System.out.println( "ID = " + id );
  37. System.out.println( "Type = " + type );
  38. System.out.println( "Price = " + price );
  39. System.out.println( "Weight = " + weight );
  40. System.out.println( "Size = " + size );
  41. System.out.println( "Color = " + color );
  42. System.out.println( "Color Pattern = " + colorPattern );
  43. System.out.println( "Fabric = " + fabric );
  44.  
  45. }
  46. result.close();
  47. statement.close();
  48. connection.close();
  49. }
  50. catch (SQLException ex)
  51. {
  52. System.out.println("No success");
  53. System.out.println("SQLException: " + ex.getMessage());
  54. System.out.println("SQLState: " + ex.getSQLState());
  55. System.out.println("VendorError: " + ex.getErrorCode());
  56. }
  57. catch (ClassNotFoundException x_not_found)
  58. {
  59. System.out.println("Class not found");
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement