Java Oracle PreparedStatement leer

Helios co.

Lt. Commander
Registriert
März 2005
Beiträge
1.863
Hallo @all,

ich verwende einen OraclePreparedStaement für SELECT-Anfragen und speichere das Ergebnis in einem ResultSet.

Jetzt kann es aber passieren, dass eine Anfrage kein Ergebnis liefert. Versuche ich nun, ohne das zu prüfen,ganz normal auf das ResultSet zuzugreifen, dann gibts natürlich eine Exception.

Nun ist es so, dass ich sogar überprüfen will,ob das ResultSet leer.

Gibt es hierfür eine Möglichkeit?

Danke im Voraus!
 
Was für ein Zufall, habe gerade selber auch damit gearbeitet :D

Umklammere die Abfrage auf das ResultSet doch mit der If-Bedingung:
if (resultSet.next()){
dein code
}

Hier lies dir mal den Abschnitt durch. Besonders das was bei "Returns" steht unten.

boolean java.sql.ResultSet.next() throws SQLException
Moves the cursor froward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.

When a call to the next method returns false, the cursor is positioned after the last row. Any invocation of a ResultSet method which requires a current row will result in a SQLException being thrown. If the result set type is TYPE_FORWARD_ONLY, it is vendor specified whether their JDBC driver implementation will return false or throw an SQLException on a subsequent call to next.

If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read.

Returns:
true if the new current row is valid; false if there are no more rows
Throws:
SQLException - if a database access error occurs or this method is called on a closed result set
 
mit einem simplen Exception Handling.

Welche Programiersprache verwendest du denn?
 
Ah, wie simpel!

Vielen Dank Leute!


Eine ähnlich Frage habe ich aber noch.
Und zwar möchte ich folgende INSERT-Query verwenden:

Code:
String insertQuery = "INSERT INTO UniqueArticleTerms(id, term) VALUES(UniqueArticleTermsSequence.NEXTVAL,?)";

Geht das so mit dem NEXTVAL überhaupt?
 
Zurück
Oben