Re: JDBC SQL2005 Read encripting fields problem

Tech-Archive recommends: Fix windows errors by optimizing your registry





Joe Weinstein wrote:



Big & Green wrote:

Good morning,

I am trying to read a field encrypted from SQL2005. My test code is this:

String sql = "";
sql = "select usuario " +
"from accesos where usuario = 'patito' and cast(DecryptByAsymKey(AsymKey_ID('EncriptadorOrbe'),clave_E, N'Contrasena') as varchar(100)) = 'patito2'"; ResultSet rs = null; Statement stmt = con.createStatement(); rs = stmt.executeQuery(sql);
System.out.println("Resultado:");
rs.next();
while(rs.next()){ System.out.println(rs.getString("usuario")); }
stmt.close();
stmt = null;
rs.close();
rs = null; When I run the query in the Query Analizer it return a correct value, but when I run mi java aplication it return a null resultSet.

what I doing bad? I need HELP!


Hi. The problem is the first call to next(), which goes to the first
row, then you do a second next() which skips it. Do this:

String sql = "select usuario "
+ " from accesos where usuario = 'patito'"
+ " and cast(DecryptByAsymKey(AsymKey_ID('EncriptadorOrbe'), "
+ " clave_E, N'Contrasena') as varchar(100)) = 'patito2'";

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("Resultado:");
while(rs.next()){
System.out.println(rs.getString("usuario"));
}
stmt.close();
stmt = null;
rs.close();
rs = null;

Oh, and close the result set first, then the statement. If you close
the statement first, it automatically closes the result set, so when
you do that, the rs.close() will throw an exception.
Joe

.



Relevant Pages

  • Problem with actualization
    ... When I show these strings on the strings I got: ... public static void executeQuery(String query) { ... Statement stmt; ... public static ResultSet getQueryResult{ ...
    (comp.lang.java.databases)
  • Re: Opening db connections
    ... Statement stmt = conn.getStatement; ... and, of course, the Javadocs for ResultSet and Statement. ...
    (comp.lang.java.programmer)
  • Re: NullPointerException error...
    ... On 20-2-2005 17:55, Frances Del Rio wrote: ... The ResultSet never got assigned with the result of the query (hence, ...
    (comp.lang.java.help)