package mrPack.web;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import mrPack.model.Oferta;
public class Opis extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection con = null;
ArrayList lista = new ArrayList();
DataSource dataSource = null;
public void init() throws ServletException {
try {
InitialContext ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/myDerby");
} catch (Exception exc) {
throw new ServletException("Nie ustanowiono połaczenia z bazą", exc);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("id " + request.getParameter("id"));
int id = Integer.parseInt(request.getParameter("id"));
String query = "select * from biuro.miasto, biuro.panstwo, "
+ "biuro.rodzaj, biuro.oferta where "
+ "biuro.oferta.miasto = biuro.miasto.idMiasto and biuro.oferta.panstwo ="
+ " biuro.panstwo.idPanstwo and biuro.oferta.rodzaj = biuro.rodzaj.idRodzaj " +
"and biuro.oferta.idoferta = " + id;
System.out.println(query);
Statement st = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
st = con.createStatement();
rs = st.executeQuery(query);
System.out.println(rs.wasNull());
String wyjazd = "";
String powrot = "";
while (rs.next()) {
wyjazd = (rs.getDate("dwyjazd")).toString();
powrot = rs.getDate("dpowrot").toString();
Oferta o = new Oferta(rs.getInt("idOferta"), rs
.getString("namePanstwo"), rs.getString("nameMiasto"),
rs.getString("nameRodzaj"), rs.getInt("cena"), wyjazd,
powrot, rs.getString("opis"));
lista.add(o);
}
rs.close();
st.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
request.setAttribute("styles", lista);
request.setAttribute("query", query);
RequestDispatcher view = request.getRequestDispatcher("opis.jsp");
view.forward(request, response);
lista.clear();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);
}
}