I edit the question. I'm creating a Jtable that feeds from a BD Sql server. The procedure for the query is this:
private static final String SELECT_FILTRO_FECHAS="SELECT REPOSTAJES.ID, FLOTA.MATRICULA,REPOSTAJES.NUM_FLOTA,REPOSTAJES.FECHA,REPOSTAJES.KMTS,"+
"REPOSTAJES.LITROS, REPOSTAJES.LIT_ADBLUE,VEHICULOS.EMPRESA,REPOSTAJES.COM_ACUM,REPOSTAJES.ADB_ACUM\n" +
"FROM REPOSTAJES INNER JOIN FLOTA\n" +
"ON REPOSTAJES.NUM_FLOTA=FLOTA.NUM_FLOTA\n" +
"INNER JOIN VEHICULOS\n" +
"ON VEHICULOS.NUM_FLOTA=FLOTA.NUM_FLOTA WHERE REPOSTAJES.NUM_FLOTA=?"+
"AND REPOSTAJES.FECHA>=? AND REPOSTAJES.FECHA <=?"+
"ORDER BY REPOSTAJES.KMTS DESC";
public static HashMap<Integer,RepostajeDTO> selectRepostajeFiltradoFechas( int num, Calendar fecha1, Calendar fecha2)throws Exception{
HashMap<Integer,RepostajeDTO> mapa=new HashMap<>();
ResultSet rs=null;
PreparedStatement st=null;
Datasource data=null;
try{
data=new Datasource();
st=data.getStatement(SELECT_FILTRO_FECHAS,ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
st.setInt(1, num);
java.util.Date fecha=fecha1.getTime();
java.sql.Date fsql= new java.sql.Date(fecha.getTime());
st.setDate(2, fsql);
java.util.Date fech=fecha2.getTime();
java.sql.Date fsql2= new java.sql.Date(fech.getTime());
st.setDate(3, fsql2);
rs=data.ejecutarSelect(st);
while(rs.next()){
RepostajeDTO m=new RepostajeDTO();
m.setId(rs.getInt("ID"));
m.setMatricula(rs.getString("MATRICULA"));
m.setNum_flota(rs.getInt("NUM_FLOTA"));
//FECHA
SimpleDateFormat dateformater=new SimpleDateFormat("dd/MM/yyyy");
Date f=rs.getDate("FECHA");
Calendar fechaoP= Calendar.getInstance();
fechaoP.setTime(f);
m.setFecha(fechaoP);
//Hasta aqui la fecha
m.setKmts(rs.getInt("KMTS"));
m.setLitros(rs.getFloat("LITROS"));
m.setLit_adblue(rs.getFloat("LIT_ADBLUE"));
m.setEmpresa(rs.getString("EMPRESA"));
m.setCom_acum(rs.getFloat("COM_ACUM"));
m.setAdb_acum(rs.getFloat("ADB_ACUM"));
//añadido al Mapa. El ID será la clave para el objeto añadido.
mapa.put(rs.getInt(1), m);
}
//Cerrar el ResultSet
rs.close();
return mapa;
}catch(Exception ex){
throw ex;
}finally{
if(st!=null){
//Cerrar la consulta parametrizada
st.close();
//Cerrar la conexion.
data.cerrarConexion();
}
}
}
The past parameters are: Two dates that come from a JDateChooser (In the code I pass them to SQL format so that the SQL Server DB understands them) An int that comes from an Integers matrix.
When the program is executed, the values of the dates and the int are loaded correctly, but when arriving at rs = data.eexecuteSelect (st); jumps an error that says: "You must declare the scalar variable @POAND"