Advertisement
Guest User

Untitled

a guest
Sep 9th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. private void salePropertyActionPerformed(java.awt.event.ActionEvent evt) {
  2.  
  3. String sSelectQuery = "";
  4. Statement statement=null;
  5. Connection conn = null;
  6. //PreparedStatement pStatement =null;
  7.  
  8. Vector columnNames = new Vector();
  9. Vector data = new Vector();
  10. JPanel panel= spPanel;
  11. try {
  12. String myDriver = "com.mysql.jdbc.Driver";
  13. String myURL = "jdbc:mysql://localhost:3306/realestate?autoReconnect=true&useSSL=false";
  14. Class.forName(myDriver);
  15. conn=DriverManager.getConnection(myURL,"username","password");
  16.  
  17. /*Storing SQL statement*/
  18. sSelectQuery ="SELECT propertyID, propertyPrice FROM property";
  19.  
  20.  
  21. statement = conn.createStatement();
  22. try (ResultSet rs = statement.executeQuery(sSelectQuery) //executes the query
  23. ) {
  24. ResultSetMetaData metaData = rs.getMetaData();
  25. int columns = metaData.getColumnCount();
  26. for(int i = 1; i<=columns; i++){
  27. columnNames.addElement(metaData.getColumnName(i));
  28. }
  29.  
  30. while (rs.next()){
  31. Vector row = new Vector(columns);
  32. for (int i=1; i<=columns; i++){
  33. row.addElement(rs.getObject(i));
  34. }
  35. data.addElement(row);
  36. System.out.println(data);
  37. }
  38. rs.close();
  39.  
  40. JTable spTable = new JTable(data,columnNames);
  41. TableColumn column;
  42. for (int i=0; i<spTable.getColumnCount(); i++){
  43. column=spTable.getColumnModel().getColumn(i);
  44. //column.setMaxWidth(250);
  45.  
  46. }
  47. JScrollPane scrollPane = new JScrollPane(spTable);
  48. panel.add(scrollPane);
  49.  
  50. }
  51.  
  52.  
  53. statement.close();
  54. } catch (SQLException e) {
  55. System.err.println("An exception ocurred");
  56. System.err.println(e.getMessage());
  57.  
  58. } catch (ClassNotFoundException ex) {
  59. Logger.getLogger(realEstateUI.class.getName()).log(Level.SEVERE, null, ex);
  60. }
  61.  
  62.  
  63. JOptionPane.showMessageDialog(this,"Query Complete");
  64. }
  65.  
  66. /**
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement