i am currently working on a Project with a database in the back and i wanted to order the files by coloums with this method: For this reason there are 4 different parameter in the method head, the first one is for the connection, the next one is the parameter is the username, because only the person who uploads the file can see the file and the next one is the coloum of the table in the database and the next one is ASC or DESC.
public ArrayList<Daten> meineDaten(Connection conn,String sortierparameter,String spalte,String reihung)
{
//generieren einer ArrayList zum Zwischenspeichern von den Werten aus der Datenbank
ArrayList<Daten> DatenSortiertPrivate = new ArrayList<>();
String READ_DATEN_PRIVATE = null;
//SQL-Abfrage
if(reihung.equals("ASC"))
{
READ_DATEN_PRIVATE="select uploadid,dateityp, dateiname, autor, uploaddatum, dokumentdatum, status from uploaddaten where uploader= ? and zustand='true' order by ? ASC;";
}
else if(reihung.equals("DESC")){
READ_DATEN_PRIVATE="select uploadid,dateityp, dateiname, autor, uploaddatum, dokumentdatum, status from uploaddaten where uploader= ? and zustand='true' order by ? DESC;";
}
//READ_DATEN_PRIVATE="select uploadid,dateityp, dateiname, autor, uploaddatum, dokumentdatum, status from uploaddaten where uploader=? and zustand='true' order by ? ?;";
try {
pstmt = conn.prepareStatement(READ_DATEN_PRIVATE);
pstmt.setString(1, sortierparameter);
pstmt.setString(2, spalte);
rs = pstmt.executeQuery();
System.out.println("SQL: "+READ_DATEN_PRIVATE);
while(rs.next())
{
int uploadid = rs.getInt(1);
String dateityp = rs.getString(2);
String dateiname = rs.getString(3);
String autor = rs.getString(4);
String uploaddatum = rs.getString(5);
String dokumentdatum = rs.getString(6);
String status = rs.getString(7);
Daten zeile = new Daten(uploadid,dateityp,dateiname, autor, uploaddatum, dokumentdatum, status);
DatenSortiertPrivate.add(zeile);
}
pstmt.close(); pstmt=null;
rs.close();rs=null;
} catch (SQLException e) {
e.printStackTrace();
}
return DatenSortiertPrivate;
}
And i don't know why this is the result: SQL Daten auf Website angebenselect uploadid,dateityp, dateiname, autor, uploaddatum, dokumentdatum, status from uploaddaten where uploader=? and zustand='true' order by ? ASC;
For example to order by "dateiname" and the username is thoker and ASC.
This method will be used by clicking on a button.
P.S. Sorry for my bad english