Java Reference
In-Depth Information
String sql = "SELECT ID, RECIPE_NUMBER, RECIPE_NAME,
DESCRIPTION " +
"FROM RECIPES " +
"WHERE RECIPE_NUMBER = ?";
try(PreparedStatement pstmt
= conn.prepareStatement(sql);) {
pstmt.setString(1, recipeNumber);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString(2) + ": "
+ rs.getString(3) +
" - " + rs.getString(4));
}
} catch (SQLException ex) {
ex.printStackTrace();
}
The next example demonstrates the use of a PreparedStatement for inserting
a record into the database. Assume that the recipeNumber , title , descrip-
tion , and text strings are passed to this code as variables.
String sql = "INSERT INTO RECIPES VALUES(" +
"NEXT VALUE FOR RECIPES_SEQ, ?,?,?,?)";
try(PreparedStatement pstmt
= conn.prepareStatement(sql);) {
pstmt.setString(1, recipeNumber);
pstmt.setString(2, title);
pstmt.setString(3, description);
pstmt.setString(4, text);
pstmt.executeUpdate();
System.out.println("Record successfully inserted.");
} catch (SQLException ex){
ex.printStackTrace();
}
Search WWH ::




Custom Search