Advertisement
GAProgress

ReadQuery

Apr 14th, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. package helper;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import db.Building;
  10.  
  11. public class ReadQuery {
  12.  
  13. private Connection connection;
  14. private ResultSet results;
  15.  
  16. public ReadQuery(String dbName, String uname, String pwd){
  17. String url = "jdbc:mysql://localhost:3306/" + dbName;
  18.  
  19. try {
  20. this.connection = DriverManager.getConnection(url, uname, pwd);
  21. } catch (SQLException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25.  
  26. }
  27.  
  28. public void doRead(){
  29. String query = "select * from building";
  30.  
  31. try {
  32. PreparedStatement ps = connection.prepareStatement(query);
  33.  
  34. results = ps.executeQuery();
  35.  
  36. } catch (SQLException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40. }
  41.  
  42. public String getHTMLTable(){
  43. //process the resultset to create an html table
  44. String table ="";
  45.  
  46. try {
  47. table += "<table>";
  48. while(results.next()){
  49. Building building = new Building();
  50. building.setBuildingID(results.getInt("BuildingID"));
  51. building.setBuildingAD(results.getString(2));
  52. building.setBuildingCity(results.getString(3));
  53. building.setBuildingSt(results.getString(4));
  54. building.setBuildingZip(results.getInt(5));
  55. building.setBuildingContact(results.getString(6));
  56. building.setBuildingPhone(results.getString(7));
  57.  
  58. table += "<tr>";
  59.  
  60. table += "<td>";
  61. table += building.getBuildingID();
  62. table += "</td>";
  63.  
  64. table += "<td>";
  65. table += building.getBuildingAD();
  66. table += "</td>";
  67.  
  68. table += "<td>";
  69. table += building.getBuildingCity();
  70. table += "</td>";
  71.  
  72. table += "<td>";
  73. table += building.getBuildingSt();
  74. table += "</td>";
  75.  
  76. table += "<td>";
  77. table += building.getBuildingZip();
  78. table += "</td>";
  79.  
  80. table += "<td>";
  81. table += building.getBuildingContact();
  82. table += "</td>";
  83.  
  84. table += "<td>";
  85. table += building.getBuildingPhone();
  86. table += "</td>";
  87.  
  88. table += "<td>";
  89. table += "<a href=update?buildingID=" + building.getBuildingID() + ">Update</a>";
  90. table += "<a href=delete?buildingID=" + building.getBuildingID() + ">Delete</a>";
  91. table += "</td>";
  92.  
  93. table += "</tr>";
  94. }
  95.  
  96. table += "</table>";
  97. } catch (SQLException e) {
  98. // TODO Auto-generated catch block
  99. e.printStackTrace();
  100. }
  101.  
  102. return table;
  103.  
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement