user13129304
user13129304

Reputation:

Data conversion error converting varchar using h2 database and eclipse

I have connected eclipse with h2 database and already created a table and inserted some input. These are my classes:

CreateTable.java:

PreparedStatement ps = con.prepareStatement("CREATE TABLE IF NOT EXISTS REGISTRATION "
            + "(id INTEGER auto_increment, "
            + " first VARCHAR(255), "
            + " last VARCHAR(255), "
            + " age INTEGER, "
            + " PRIMARY KEY ( id )) ");

    ps.executeUpdate();
    System.out.println("Table Created");

When I execute this, it prints "table created" on the console.

this is the Insert.java class:

Connection con = DriverManager.getConnection("jdbc:h2:file:C:/h2/test", "sami", "");
PreparedStatement ps = con.prepareStatement("insert into REGISTRATION(first,last,age) values ('python', 'java', 24),"
            + "('jQuery', 'Angularjs', 10)");

    int i = ps.executeUpdate();
    System.out.println(i + " Record(s) inserted");

When I run this, it prints 2 Records inserted.

This is my Display.java class:

Connection con = DriverManager.getConnection("jdbc:h2:file:C:/h2/test", "sami", "");
PreparedStatement ps = con.prepareStatement("select * from REGISTRATION");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
    System.out.println(rs.getInt(1) + " " + rs.getInt(2) + " " + rs.getInt(3) + " " + rs.getInt(4));
}

And when I run this, it says "Data conversion error converting "python" [22018-200]"

I already changed my code to "prepared statement" but it doesn't fix the problem.

Can someone help me?

Thanks a lot

Upvotes: 0

Views: 2349

Answers (1)

Evgenij Ryazanov
Evgenij Ryazanov

Reputation: 8188

You can't read a VARCHAR column with ResultSet.getInt(), use ResultSet.getString() instead for columns 2 and 3.

Upvotes: 1

Related Questions