Good afternoon,
I am developing a University project which consists of creating a hotel page using Java Server Pages. I am using the Server Faces framework. I want to make the base template of the program, which contains a search engine (One Form), order the form containing the index, update with the search results. As a database I use Derby.
Now, my query for some reason is not working ... I understand that Derby is not the same thing as SQL in different details. The code of my Bean is this:
package Beans;
import javax.inject.Named;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.sql.ResultSet;
import Database.GestorDB;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author SEMINARIO
*/
@Named(value = "portada")
@ManagedBean
@ViewScoped
public class Portada {
private String html;
private String fechaE, fechaS, localidad;
private int personas, Pmostrado;
public Portada() {
fechaE=null;
fechaS=null;
localidad=null;
personas=0;
Pmostrado=0;
}
public void setHtml(String htm){html=htm;}
public void setFechaEntrada(String dato){fechaE=dato;}
public void setFechaSalida(String dato){fechaS=dato;}
public void setLocalidad(String dato){localidad=dato;}
public void setPersonas(int dato){personas=dato;}
public void setPortada(){
String []hoteles = new String[10];
String []nombh = new String[10];
String []localid = new String[10];
html=null; html="<table>";
ResultSet rs;
String query = "SELECT HOTELES.NOMBRE AS A, HOTELES.IMGHOTEL, HOTELES.DESCRIPCION, LOCALIDADES.NOMBRE AS B FROM HOTELES INNER JOIN LOCALIDADES"
+ " ON LOCALIDADES.IDLOCALIDADES = HOTELES.LOCALIDADES_IDLOCALIDADES";
try {
rs = GestorDB.getConsulta(query);
int i = 0;
while(rs.next()){ //en nombre de juan manuel
nombh[i] = rs.getString("A");
hoteles[i] = rs.getString("IMGHOTEL");
localid[i] = rs.getString("B");
i++;
}
int a = 0;
for(int c=0; c < 3; c++){
html += "<tr>";
for(int f=0; f < 3; f++){
html += "<td><div id=\"HotP\"><b><p style=\"text-align:center;font-style:italic;color:red\">"+ nombh[f+a] +"</p></b>"+
"<img src=\"resources/images/"+ hoteles[f+a] +"\" style=\"width:160px;height:70px;border:1px solid gray;\"/><br>"
+ "<span style=\"color: darkblue;text-align: center;\">" + localid[f+a] +"</span></td>";
}
html += "</tr>";
a+=3;
}
html += "</table>";
} catch (SQLException ex) {
Logger.getLogger(Portada.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(html);
Pmostrado=1;
}
public void setBusqueda() throws SQLException{
String parametros[] = {localidad,String.valueOf(personas)};
int f = 0;
ResultSet rs = null;
String query = "SELECT HOTELES.NOMBRE, HOTELES.IMGHOTELES, LOCALIDADES.NOMBRE AS NOMBRE1 FROM ((DISPONIBILIDAD INNER JOIN TARIFAS ON TARIFAS.[DISPONIBILIDAD_IDDISPONIBILIDAD] = DISPONIBILIDAD.IDDISPONIBILIDAD) INNER JOIN HOTELES ON TARIFAS.[HOTELES_IDHOTELES] = HOTELES.IDHOTELES) INNER JOIN LOCALIDADES ON HOTELES.[LOCALIDADES_IDLOCALIDADES] = LOCALIDADES.IDLOCALIDADES WHERE (LOCALIDADES.NOMBRE LIKE '%Mexico%' AND TARIFAS.PERSONAS >= 3) AND DISPONIBILIDAD.N_DISPONIBLE > 0";
html=null; html = "<table>";
try{
rs = GestorDB.getConsulta(query);
} catch (SQLException ex) {
Logger.getLogger(Portada.class.getName()).log(Level.SEVERE, null, ex);
}
if(rs != null){
while(rs.next()){
if(f == 0) html += "<tr>";
html += "<td><div id=\"HotP\"><b><p style=\"text-align:center;font-style:italic;color:red\">"+ rs.getString("A") +"</p></b>"+
"<img src=\"resources/images/"+ rs.getString("IMGHOTEL") +"\" style=\"width:160px;height:70px;border:1px solid gray;\"/><br>"
+ "<span style=\"color: darkblue;text-align: center;\">" + rs.getString("B") +"</span></td>";
f++;
if(f == 2){ html += "</tr>"; f=0;}
}
html += "</table>";
System.out.println(html);
}
}
public String getHtml(){return html;}
public String getFechaEntrada(){return fechaE;}
public String getFechaSalida(){return fechaS;}
public String getLocalidad(){return localidad;}
public int getPersonas(){return personas;}
public String getBusqueda() throws SQLException{
if(Pmostrado !=0) setBusqueda();
else setPortada();
return html;
}
}
For those who get dizzy when they see so much code, if the problem starts here:
String query = "SELECT HOTELES.NOMBRE, HOTELES.IMGHOTELES, LOCALIDADES.NOMBRE AS NOMBRE1 FROM ((DISPONIBILIDAD INNER JOIN TARIFAS ON TARIFAS.[DISPONIBILIDAD_IDDISPONIBILIDAD] = DISPONIBILIDAD.IDDISPONIBILIDAD) INNER JOIN HOTELES ON TARIFAS.[HOTELES_IDHOTELES] = HOTELES.IDHOTELES) INNER JOIN LOCALIDADES ON HOTELES.[LOCALIDADES_IDLOCALIDADES] = LOCALIDADES.IDLOCALIDADES WHERE (LOCALIDADES.NOMBRE LIKE '%Mexico%' AND TARIFAS.PERSONAS >= 3) AND DISPONIBILIDAD.N_DISPONIBLE > 0";
I started asking parametrized queries with the form, but decided for reasons of proof to make a simple query. The error thrown by Netbeans (org.apache.derby) is as follows:
Error de sintaxis: Encountered "[" at line 1, column 126.
If you like, I can leave the whole stack ... but it is clear that derby complains about a syntax error. What error would my query have ?, would it be that in derby it would be written differently?