Advertisement
Guest User

Untitled

a guest
Sep 17th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. public void connectDB(JComboBox code)
  2. {
  3. try
  4. {
  5. Class.forName("com.mysql.jdbc.Driver");
  6. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stock","root","");
  7. String query = "SELECT stockCode FROM stock_master";
  8. Statement stmt = con.createStatement();
  9. ResultSet rs = stmt.executeQuery(query);
  10. while(rs.next())
  11. {
  12. int stockCode = rs.getInt(1);
  13. //String description = rs.getString(2);
  14. //int unitSize = rs.getInt(3);
  15. //String unitOfMeasure = rs.getString(4);
  16. //double price = rs.getDouble(5);
  17. //int quantityInStock = rs.getInt(6);
  18. code.setSelectedItem(Integer.toString(stockCode));
  19.  
  20. }
  21. con.close();
  22. }
  23. catch(Exception e)
  24. {
  25. JOptionPane.showMessageDialog(null, "This is the error: " + e);
  26. }
  27. }
  28.  
  29. buttonConnectToDB.addActionListener(new ActionListener() {
  30. @Override
  31. public void actionPerformed(ActionEvent e)
  32. {
  33. connectDB(comboBoxStockCode);
  34. }
  35. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement