HSQLDB SQL ORDER BY nur mit fixen String machbar?

DefconDev

Bekanntes Mitglied
Java:
ResultSet table_01 = statement.executeQuery("SELECT TOP 5 * FROM \"Karten_Siege\" ORDER BY "+this.mapName+" DESC");

Java:
ResultSet table_01 = statement.executeQuery("SELECT TOP 5 * FROM \"Karten_Siege\" ORDER BY \"this.mapName\" DESC");


Beide Statements lassen sich nicht realisieren. Im ersten Fall sagt er mir nur, "user lacks privilege or object not found: AKROPOLIS"

Der Spaltenname existiert , ich habe aber keine Ahnung warum AKROPOLIS komplett groß geschrieben wird.

Beim zweiten Stament sagt er mir nur das this.mapName nicht existiert.


Kann ich da keine variable einfügen nach ORDER BY?
 
Java:
PreparedStatement ps = deineConnection.prepareStatement("SELECT * FROM Karten_Siege ORDER BY ? DESC LIMIT 5");
ps.setString(1, this.mapName);
System.out.println(ps.toString());
ResultSet table_01 = ps.executeQuery();

Was schmeißt es dir denn jetzt um die Ohren?
 
Zuletzt bearbeitet:
wenn ich

Java:
PreparedStatement ps = con.prepareStatement("SELECT * FROM \"Karten_Siege\" ORDER BY ? DESC LIMIT 5");

dein Karten_Siege zu = \"Karten_Siege\" ändere sagt er mir "data type cast needed for parameter or null literal"


wenn ich nur Karten_Siege so stehen lasse wie in deinem Code, dann sagt er mir nur dass er das Object von Karten_Siege nicht kennt.

\"Karten_Siege\" Ich muss alles mit slash angeben, ganz verstehen tu ich das bisher noch nicht.
 
1. ja, habe alles so eingefügt wie du es gepostet hast, dann habe ich ResultSet table_01 = ps.executeQuery(); bzw table_01.next() durch eine While schleife laufen lassen.

2. Ist ein String. Also das casten sollte ich ausschließen können, weil der sonst ne cast Exception auswirft, ist mir heute woanders passiert.

3. siehe unten.

4. HSQLDB bzw. die Datenbank selber habe ich in OpenOffice Base vorbereitet.


Java:
2014-09-07T15:16:28.828+0200  SEVERE  null
java.sql.SQLSyntaxErrorException: data type cast needed for parameter or null literal
	at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
	at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
	at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
	at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
	at matchMaker_v2.SpielerStatsMap.loadMapStats(SpielerStatsMap.java:45)
	at matchMaker_v2.SpielerStatsMap.<init>(SpielerStatsMap.java:31)
	at matchMaker_v2.PreviewStatsMap.<init>(PreviewStatsMap.java:59)
	at matchMaker_v2.PreviewStatsMap.mousePressed(PreviewStatsMap.java:238)
	at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$400(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.hsqldb.HsqlException: data type cast needed for parameter or null literal
	at org.hsqldb.error.Error.error(Unknown Source)
	at org.hsqldb.error.Error.error(Unknown Source)
	at org.hsqldb.ExpressionOrderBy.resolveTypes(Unknown Source)
	at org.hsqldb.QuerySpecification.resolveExpressionTypes(Unknown Source)
	at org.hsqldb.QuerySpecification.resolveTypesPartOne(Unknown Source)
	at org.hsqldb.QueryExpression.resolve(Unknown Source)
	at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
	at org.hsqldb.ParserCommand.compilePart(Unknown Source)
	at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
	at org.hsqldb.Session.compileStatement(Unknown Source)
	at org.hsqldb.StatementManager.compile(Unknown Source)
	at org.hsqldb.Session.execute(Unknown Source)
	... 38 more

java.sql.SQLSyntaxErrorException: data type cast needed for parameter or null literal
	at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
	at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
	at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
	at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
	at matchMaker_v2.SpielerStatsMap.loadMapStats(SpielerStatsMap.java:45)
	at matchMaker_v2.SpielerStatsMap.<init>(SpielerStatsMap.java:31)
	at matchMaker_v2.PreviewStatsMap.<init>(PreviewStatsMap.java:59)
	at matchMaker_v2.PreviewStatsMap.mousePressed(PreviewStatsMap.java:238)
	at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$400(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.hsqldb.HsqlException: data type cast needed for parameter or null literal
	at org.hsqldb.error.Error.error(Unknown Source)
	at org.hsqldb.error.Error.error(Unknown Source)
	at org.hsqldb.ExpressionOrderBy.resolveTypes(Unknown Source)
	at org.hsqldb.QuerySpecification.resolveExpressionTypes(Unknown Source)
	at org.hsqldb.QuerySpecification.resolveTypesPartOne(Unknown Source)
	at org.hsqldb.QueryExpression.resolve(Unknown Source)
	at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
	at org.hsqldb.ParserCommand.compilePart(Unknown Source)
	at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
	at org.hsqldb.Session.compileStatement(Unknown Source)
	at org.hsqldb.StatementManager.compile(Unknown Source)
	at org.hsqldb.Session.execute(Unknown Source)
	... 38 more
 
Soweit komme ich gar nicht.

wenn ich das Statement auf das kürze.
Java:
PreparedStatement ps = con.prepareStatement("SELECT * FROM \"Karten_Siege\"");

Java:
ps.setString(1, this.mapName);

wirft er ab dieser Methode folgende Fehlermeldung:

Invalid argument in JDBC call: parameter index out of range: 1



System.out.println(ps.toString());

wird gar nicht auf der Konsole angezeigt.
 
Java:
PreparedStatement ps = con.prepareStatement("SELECT * FROM \"Karten_Siege\" ORDER BY ? DESC LIMIT 5");

wenn ich das so belasse, dann wirft der hier schon die Exception.

data type cast needed for parameter or null literal



Java:
private String mapName;
	private int[] mapArraySieg;
	private int[] mapArrayNiederlage;
	private int mapMaxWin;
	private int mapMaxLoss;
	private String mapMaxWinName;
	private String mapMAxLossName;
	
	
	public SpielerStatsMap(String mapName) throws IOException{
		this.mapName = mapName;
		loadMapStats();
	}
	
	private void loadMapStats(){
		try{
			String db_file_name_prefix = "c:\\Users\\Hades 2\\Desktop\\database\\mydb";
			Connection con = null;
			Class.forName("org.hsqldb.jdbcDriver");
			con = DriverManager.getConnection("jdbc:hsqldb:file:" + db_file_name_prefix,"SA",""); //SA Username : kein Passwort
			Statement statement = con.createStatement();
			
			mapArraySieg = new int[10];
			mapArrayNiederlage = new int[10];
			
		    PreparedStatement ps = con.prepareStatement("SELECT * FROM \"Karten_Siege\" ORDER BY ? DESC LIMIT 5");
		    ps.setString(1, this.mapName);
		    System.out.println(ps.toString());
		    ResultSet table_01 = ps.executeQuery();
			//ResultSet table_01 = statement.executeQuery("SELECT TOP 10 * FROM \"Karten_Siege\" ORDER BY '"+this.mapName+"' DESC");
			int i=0;
			while(table_01.next()){
				
				mapArraySieg[i] = table_01.getInt(this.mapName);
				i++;
			}
			
			table_01 = statement.executeQuery("SELECT TOP 10 * FROM \"Karten_Niederlage\"");
			i=0;
			while(table_01.next()){
				
				mapArrayNiederlage[i] = table_01.getInt(this.mapName);
				i++;
			}
			String mapname = this.mapName;
			mapname.toUpperCase();
			table_01 = statement.executeQuery("SELECT MAX '(\"Akropolis\")' AS \"Akropolis\" FROM \"Karten_Siege\"");
			i=0;
			while(table_01.next()){
				
				mapMaxWin = table_01.getInt(this.mapName);
				//mapMaxWinName = table_01.getString("Spieler");
				System.out.println("Höchster Sieg "+mapMaxWin+"     Name: ");
				i++;
			}
			statement.close();
			con.close();
		}
		catch (SQLException ex){
	     Logger.getLogger(SpielerStatsMap.class.getName()).log(Level.SEVERE, null, ex);
	     ex.printStackTrace();
	   	} 
		catch (ClassNotFoundException ex){
	     Logger.getLogger(SpielerStatsMap.class.getName()).log(Level.SEVERE, null, ex);
		}
		
	}

	public int[] getMapArraySieg() {
		return mapArraySieg;
	}

	public int[] getMapArrayNiederlage() {
		return mapArrayNiederlage;
	}

	
	
	
}
 
Sorry, bin auf der Couch eingeschlafen^^

Karten_Siege.jpg

So sieht meine DB aus.

Jetzt ist meine Frage, ist der Name des Spaltenkopf ungleich der Spaltenname?
 
Java:
"SELECT TOP 5 * FROM \"Karten_Siege\" ORDER BY "+this.mapName+" DESC"

Java:
"SELECT TOP 5 * FROM \"Karten_Siege\" ORDER BY \"" + this.mapName + "\" DESC"


Das obere ist das erste gewesen, was nicht funktioniert hat.

Das zweite ist jetzt das Richtige, welches endlich funktioniert.

Ein User aus einem anderen Forum hat mich darauf hingewiesen. Verstehen tu ich es noch nicht 100%. Anscheinend muss this.mapName auch in Anführungszeichen, warum auch immer.
 
Ich möchte nur kurz anmerken, das mein Lieblingsframework myBATIS😉 das Setzen der order by Clause OHNE String-Geraffel beherrscht.

Aus der Doku

By default,using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g.?). While this is safer,faster and almost always preferred, sometimes you just want to directly inject a string unmodified into the SQL Statement. For example, for ORDER BY, you might use something like this:
Code:
ORDER BY ${columnName}
Here MyBatis won’t modify or escape the string.

Verifiziert und funzt natürlich🙂
Code:
<select id="test" parameterType="String" resultType="Person">
		select id,vorname,nachname from person order by ${value} desc
	</select>
Verwendung im Code
Java:
List<Person> listPersons = session.selectList("Person.test", "vorname");
 
Weil wahrscheinlich in der Mapname Leerzeichen oder andere Sonderzeichen sein können?

Gruß

Claus

Nein, habe mir die Namen meiner Spalten, die die Mapnamen beinhalten auf der Konsole ausgegeben. Ebenfalls in der Script-Datei waren keine Sonderzeichen dort anzutreffen.

Jedenfalls habe ich mir erst mal ein Dokument erstellt, damit ich die Syntax zu den Querys nicht vergessen.
 

Zurück
Oben