borked97
borked97

Reputation: 23

How to fix 'terminated' while using rs.getString

When trying to pull from an SQL database, I get in Java console.

I am able to enter my ID manually in the prepareStatement, but I can't use setString to work with with the prepareStatement.

I haven't throught of too much to try, but I did find that the issue is within the while statement. rs.next returns false when it should return true. The information in SQL has been committed, and I can call all of the information in java using a function that reads out the entire table.

getemployeeDataById("01");
private static void getemployeeDataById(String e_id) {
    DBConnection dbConnection = DBConnection.getInstance();
    PreparedStatement ps;

    try {
        ps = dbConnection.getConnection().prepareStatement("select * from employee where E_ID = ?");
        ps.setString(1, e_id);
        ResultSet rs = ps.executeQuery();

        while (rs.next()) {
            System.out.println(rs.getString("ename"));
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

SQL

CREATE TABLE EMPLOYEE
(E_ID CHAR(3) PRIMARY KEY,
ENAME CHAR(25),
DOB CHAR(25),
EMAIL CHAR(25),
PHONE CHAR(25),
ADDRESS CHAR(25));

INSERT INTO EMPLOYEE
VALUES
('01','JEREMY MANN','12/23/1992','[email protected]','317-528-1234','123 CandyCane Lane');

I am expecting to get the name outputted in the console, so for this example it would be "JEREMY MANN." The code runs and then in the Eclipse Java console it shows Application [Java Application]. It runs into an issue within the while statement, but I'm not sure what's causing rs.next to be false.

Upvotes: 0

Views: 64

Answers (1)

John Bayko
John Bayko

Reputation: 1091

In Oracle 11g (well, Oracle versions generally) CHAR is a fixed width type, and if you give it a value shorter than the given width, it will be padded with blanks (which might not be obvious if you just dumped the table contents). In this case, your key is length 3, but the string "01" is length 2, so that won't work.

Try VARCHAR2 for the column types, that's a variable length string.

Upvotes: 1

Related Questions