Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.ArrayList;
  7.  
  8.  
  9. public class Main {
  10.  
  11. static final String DB_URL = "jdbc:mysql://localhost/lab1?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  12.  
  13. static Statement stmt = null;
  14.  
  15. static final String USER = "root";
  16. static final String PASS = "root";
  17.  
  18. public static void main(String[] args) throws ClassNotFoundException,
  19. SQLException, InstantiationException, IllegalAccessException {
  20.  
  21. Class.forName("com.mysql.cj.jdbc.Driver");
  22. Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
  23.  
  24.  
  25. stmt = conn.createStatement();
  26.  
  27. String sql = "SELECT * FROM Products";
  28. ResultSet rs = stmt.executeQuery(sql);
  29.  
  30. while (rs.next()) {
  31. String name = rs.getString("prod_name");
  32. //System.out.println(name);
  33. }
  34.  
  35. rs.close();
  36. stmt.close();
  37. conn.close();
  38. System.out.println(getVendorsNames());
  39. System.out.println(getProductsNames("Bear Emporium"));
  40.  
  41. }
  42.  
  43. static ArrayList<String> getVendorsNames() throws ClassNotFoundException,
  44. SQLException, InstantiationException, IllegalAccessException {
  45.  
  46. Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
  47.  
  48.  
  49. stmt = conn.createStatement();
  50.  
  51. String sql = "SELECT * FROM Vendors";
  52. ResultSet rs = stmt.executeQuery(sql);
  53.  
  54. ArrayList<String> res = new ArrayList<String>();
  55.  
  56. while (rs.next()) {
  57. String name = rs.getString("vend_name");
  58. String id = rs.getString("vend_id");
  59. res.add(name);
  60. System.out.println(name+" "+id);
  61. }
  62.  
  63. rs.close();
  64. stmt.close();
  65. conn.close();
  66. return res;
  67. }
  68. static ArrayList<String> getProductsNames(String customerName) throws ClassNotFoundException,
  69. SQLException, InstantiationException, IllegalAccessException {
  70.  
  71. Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
  72.  
  73.  
  74. stmt = conn.createStatement();
  75.  
  76. String sql = "SELECT * FROM Vendors WHERE vend_name='"+customerName+"'";
  77. ResultSet rs = stmt.executeQuery(sql);
  78.  
  79. ArrayList<String> res = new ArrayList<String>();
  80. String vendorId="";
  81.  
  82. while (rs.next()) {
  83. vendorId = rs.getString("vend_id");
  84. }
  85.  
  86. rs.close();
  87.  
  88. sql = "SELECT * FROM Products";
  89. rs = stmt.executeQuery(sql);
  90. System.out.println(rs);
  91.  
  92. while (rs.next()) {
  93. String productName = rs.getString("prod_name");
  94. String dd = rs.getString("vend_id");
  95. System.out.println(productName+" "+dd);
  96. }
  97.  
  98. rs.close();
  99.  
  100. stmt.close();
  101. conn.close();
  102. return res;
  103. }
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement