Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package helper;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import db.Building;
- public class ReadQuery {
- private Connection connection;
- private ResultSet results;
- public ReadQuery(String dbName, String uname, String pwd){
- String url = "jdbc:mysql://localhost:3306/" + dbName;
- try {
- this.connection = DriverManager.getConnection(url, uname, pwd);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void doRead(){
- String query = "select * from building";
- try {
- PreparedStatement ps = connection.prepareStatement(query);
- results = ps.executeQuery();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public String getHTMLTable(){
- //process the resultset to create an html table
- String table ="";
- try {
- table += "<table>";
- while(results.next()){
- Building building = new Building();
- building.setBuildingID(results.getInt("BuildingID"));
- building.setBuildingAD(results.getString(2));
- building.setBuildingCity(results.getString(3));
- building.setBuildingSt(results.getString(4));
- building.setBuildingZip(results.getInt(5));
- building.setBuildingContact(results.getString(6));
- building.setBuildingPhone(results.getString(7));
- table += "<tr>";
- table += "<td>";
- table += building.getBuildingID();
- table += "</td>";
- table += "<td>";
- table += building.getBuildingAD();
- table += "</td>";
- table += "<td>";
- table += building.getBuildingCity();
- table += "</td>";
- table += "<td>";
- table += building.getBuildingSt();
- table += "</td>";
- table += "<td>";
- table += building.getBuildingZip();
- table += "</td>";
- table += "<td>";
- table += building.getBuildingContact();
- table += "</td>";
- table += "<td>";
- table += building.getBuildingPhone();
- table += "</td>";
- table += "<td>";
- table += "<a href=update?buildingID=" + building.getBuildingID() + ">Update</a>";
- table += "<a href=delete?buildingID=" + building.getBuildingID() + ">Delete</a>";
- table += "</td>";
- table += "</tr>";
- }
- table += "</table>";
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return table;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement