Search code examples
javasqljdbcoracle11grdbms

Error "java.sql.SQLException: ORA-04054" JDBC-ORACLE


My code looks as follows:

ResulSet rs = stmt.executeQuery("select passwd from mrs_user where email="+mail_id);
String usr_paswd = rs.getString(1);

But the error is as follows:

java.sql.SQLException: ORA-04054: database link G.COM does not exist

mail_id=dk@g.com


Solution

  • First, String should be between to quotes 'mail_id', but this way is not secure it can cause SQL Injection or syntax error instead you can use PreparedStatement.

    Second, you still not get any result, you have to call rs.next() before to moves the cursor to the next row (read about Retrieving and Modifying Values from Result Sets).


    Code example

    String usr_paswd = null;
    try (PreparedStatement stmt = connection.prepareStatement(
            "select passwd from mrs_user where email=?")) {
        stmt.setString(1, mail_id);
        ResulSet rs = stmt.executeQuery();
        if(rs.next()){
           usr_paswd = rs.getString(1);      
        }
    }