Java Mysql按可变字段排序

Java Mysql按可变字段排序,java,mysql,sql,Java,Mysql,Sql,我正在尝试创建一个mysql select查询,使用可更改的ORDERBY字段条件 此查询正在Java应用程序中运行 查询是: public static final String LECTURAREFADMINPAGFILTRO = "SELECT Agente.idAgente, Agente.nomAgente, Agente.emailAgente, Agente.nomDir, Agente.zonaAgente, Agente.paisAgente, Agente.equipoAge

我正在尝试创建一个mysql select查询,使用可更改的ORDERBY字段条件

此查询正在Java应用程序中运行

查询是:

public static final String LECTURAREFADMINPAGFILTRO = "SELECT Agente.idAgente, Agente.nomAgente, Agente.emailAgente, Agente.nomDir, Agente.zonaAgente, Agente.paisAgente, Agente.equipoAgente, Agente.catAgente, Referencia.idReferencia,Referencia.nomMaterial, Referencia.tipoMaterial, Referencia.protocometa, Referencia.precio, Referencia.guidlineUso, Referencia.especialista, Solicitud.IdSolicitud, Solicitud.estSolicitud, AgenteSolicitud.prescriptor, AgenteSolicitud.stockAgente, AgenteSolicitud.stockagenteConf, AgenteSolicitud.undspropSist, AgenteSolicitud.undsconfAgente, AgenteSolicitud.undsEnvio, AgenteSolicitud.idAgenteSolicitud FROM AgenteSolicitud join Solicitud on AgenteSolicitud.Solicitud_idSolicitud=Solicitud.idSolicitud  and Solicitud.Agente_idAgente=AgenteSolicitud.Agente_idAgente and Solicitud.Peticion_idPeticion = AgenteSolicitud.Solicitud_Peticion_idPeticion and Solicitud.Peticion_Actividad_idActividad=AgenteSolicitud.Solicitud_Peticion_Actividad_idActividad join Agente on Agente.idAgente=Solicitud.Agente_idAgente join Actividad on Agente.Actividad_idActividad=Actividad.idActividad, Referencia where Referencia.idReferencia=Solicitud.Referencia_idReferencia and Actividad.nomActividad=? and Solicitud.estSolicitud='Activa' ORDER BY ? ASC LIMIT ? OFFSET ?";
@Override
public List<String> listaFiltro(String ciclo, String ordenacion, String limit, String offset) {
CRUD bd = new CRUD();
List<String> lista = new ArrayList<String>();
String[] valores = new String[4];
valores[0]=ciclo;
valores[1]=ordenacion;
valores[2]=limit;
valores[3]=offset;
try {
        lista = bd.leer(Constantes.LECTURAREFADMINPAGFILTRO, valores);
}catch (Exception e) {
        e.printStackTrace();
}

        return lista;   
}


public List <String> leer(String cadena, String [] valores) throws SQLException{
    log.info("Entra en la función leer(String cadena, String [] valores)");
    this.conectar();

    List <String> listaDatos = new ArrayList <String> ();
    try{

    PreparedStatement stmt = con.prepareStatement(cadena);
    System.out.println("Número de filas maximo "+stmt.getMaxRows());        
    if(valores != null){
        for(int i=0; i< valores.length;i++){
            stmt.setString(i+1,valores[i]);
        }
    }
    ResultSet rs = null;
    rs = stmt.executeQuery();
    ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
    int numeroColumnas= rsmd.getColumnCount();

    while (rs.next()){
//          contar_filas++;
        //Si la columna es solo 1, no hace falta que dividamos las columnas
        if (numeroColumnas >1){
            String fila ="";
            for (int i=1;i<=numeroColumnas;i++){
                    fila=fila + "::" + rs.getString(i);
            }
            fila=fila+"%%";
            listaDatos.add(fila);
        }else{
            listaDatos.add(rs.getString(1));
        }
    }

    log.info("La lista total se compone de "+listaDatos.size()+ " elementos");
    log.info("Los valores leidos son: " + listaDatos.toString());
    if (stmt!=null) {
        stmt.close();
    }
    }catch(Exception e){
        log.info("CRUD: Ha ocurrido un error" );
        log.info("CRUD: el error es " + e.getMessage().toString());
        con.close();
        e.printStackTrace();
    }finally{
        this.cerrarConexion();
    }
     log.info("Sale de la función leer(String cadena, String [] valores)");

    return listaDatos;

}
那个?值作为参数传递,作为order by条件,任何字段作为参数传递

我已经在squirrel中运行了这个查询,给orderby字段“”一个字段值,查询成功运行

但是,在Java应用程序中,当我执行同一个查询时,它就像是不按条件支付订单

与查询相关的Java代码是:

public static final String LECTURAREFADMINPAGFILTRO = "SELECT Agente.idAgente, Agente.nomAgente, Agente.emailAgente, Agente.nomDir, Agente.zonaAgente, Agente.paisAgente, Agente.equipoAgente, Agente.catAgente, Referencia.idReferencia,Referencia.nomMaterial, Referencia.tipoMaterial, Referencia.protocometa, Referencia.precio, Referencia.guidlineUso, Referencia.especialista, Solicitud.IdSolicitud, Solicitud.estSolicitud, AgenteSolicitud.prescriptor, AgenteSolicitud.stockAgente, AgenteSolicitud.stockagenteConf, AgenteSolicitud.undspropSist, AgenteSolicitud.undsconfAgente, AgenteSolicitud.undsEnvio, AgenteSolicitud.idAgenteSolicitud FROM AgenteSolicitud join Solicitud on AgenteSolicitud.Solicitud_idSolicitud=Solicitud.idSolicitud  and Solicitud.Agente_idAgente=AgenteSolicitud.Agente_idAgente and Solicitud.Peticion_idPeticion = AgenteSolicitud.Solicitud_Peticion_idPeticion and Solicitud.Peticion_Actividad_idActividad=AgenteSolicitud.Solicitud_Peticion_Actividad_idActividad join Agente on Agente.idAgente=Solicitud.Agente_idAgente join Actividad on Agente.Actividad_idActividad=Actividad.idActividad, Referencia where Referencia.idReferencia=Solicitud.Referencia_idReferencia and Actividad.nomActividad=? and Solicitud.estSolicitud='Activa' ORDER BY ? ASC LIMIT ? OFFSET ?";
@Override
public List<String> listaFiltro(String ciclo, String ordenacion, String limit, String offset) {
CRUD bd = new CRUD();
List<String> lista = new ArrayList<String>();
String[] valores = new String[4];
valores[0]=ciclo;
valores[1]=ordenacion;
valores[2]=limit;
valores[3]=offset;
try {
        lista = bd.leer(Constantes.LECTURAREFADMINPAGFILTRO, valores);
}catch (Exception e) {
        e.printStackTrace();
}

        return lista;   
}


public List <String> leer(String cadena, String [] valores) throws SQLException{
    log.info("Entra en la función leer(String cadena, String [] valores)");
    this.conectar();

    List <String> listaDatos = new ArrayList <String> ();
    try{

    PreparedStatement stmt = con.prepareStatement(cadena);
    System.out.println("Número de filas maximo "+stmt.getMaxRows());        
    if(valores != null){
        for(int i=0; i< valores.length;i++){
            stmt.setString(i+1,valores[i]);
        }
    }
    ResultSet rs = null;
    rs = stmt.executeQuery();
    ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
    int numeroColumnas= rsmd.getColumnCount();

    while (rs.next()){
//          contar_filas++;
        //Si la columna es solo 1, no hace falta que dividamos las columnas
        if (numeroColumnas >1){
            String fila ="";
            for (int i=1;i<=numeroColumnas;i++){
                    fila=fila + "::" + rs.getString(i);
            }
            fila=fila+"%%";
            listaDatos.add(fila);
        }else{
            listaDatos.add(rs.getString(1));
        }
    }

    log.info("La lista total se compone de "+listaDatos.size()+ " elementos");
    log.info("Los valores leidos son: " + listaDatos.toString());
    if (stmt!=null) {
        stmt.close();
    }
    }catch(Exception e){
        log.info("CRUD: Ha ocurrido un error" );
        log.info("CRUD: el error es " + e.getMessage().toString());
        con.close();
        e.printStackTrace();
    }finally{
        this.cerrarConexion();
    }
     log.info("Sale de la función leer(String cadena, String [] valores)");

    return listaDatos;

}
@覆盖
公共列表listaFiltro(字符串ciclo、字符串ordenacion、字符串限制、字符串偏移量){
积垢bd=新积垢();
List lista=new ArrayList();
字符串[]valores=新字符串[4];
瓦洛雷斯[0]=ciclo;
瓦洛雷斯[1]=ordenacion;
valores[2]=极限;
valores[3]=偏移量;
试一试{
lista=bd.leer(Constantes.讲师Fadminpagfiltro,valores);
}捕获(例外e){
e、 printStackTrace();
}
返回列表a;
}
公共列表leer(字符串cadena,字符串[]valores)抛出SQLException{
log.info(“Entra en la función leer(String cadena,String[]valores)”;
这个。conectar();
List listaDatos=newarraylist();
试一试{
准备好的声明stmt=con.准备好的声明(cadena);
System.out.println(“Número de filas maximo”+stmt.getMaxRows());
if(valores!=null){
for(int i=0;i1){
字符串fila=“”;
对于(int i=1;i
  • 不确定是否可以为order by参数?如果使用。生成的SQL与“abc”的order类似。不确定这是否有效

  • 可以使用?表示限制和偏移量,但其数据类型是INT,而不是String,因此必须使用setInt

  • 您可以在leer函数中这样做(构建sql)

    String sql=“选择您的_列…其中…和Actividad.noactividad=?和requireud.estrequirecud='Activa',并按“+valores[1]+“ASC LIMIT”+valores[2]+“OFFSET”+valores[2]排序

    PreparedStatement stmt=con.prepareStatement(sql)

    stmt.setString(1,valores[0]);//仅1个参数


  • 什么是您的java代码?什么是Constants.讲师FadminPagFiltro?我们需要查看您在JavaDYNAMIC SQL中的查询?我会研究它,但您能给我一个链接或示例作为实现此目的的模板吗?。感谢您的快速响应。我认为您的答案已被接受:)。