Skip to content

Postgres – Column out of range in PreparedStatement – java

got a problem with a preparedstatement using Java on Windows 8.1…

Basically I get an error as follows (from the test):

2014-08-17 16:56:37
SEVERE: Strt Testing
2014-08-17 16:56:37
SEVERE: Before pstText: SELECT "lineText" FROM "public"."dbText_?" WHERE "Textid" = '?'
2014-08-17 16:56:37
SEVERE: The column index is out of range: 1, number of columns: 0.

I can see it’s telling me I have no ? but they are there for all to see in the before setString message.

Can anyone see what is wrong with what I’m doing – I’m new to PreparedStatements and I think it’s right…

thanks and regards Seán

        String zLocale = "en_EN";
        PreparedStatement pst1 = null;

        logger.severe("Strt Testing");

        String sql1;
        sql1= "SELECT "lineText" FROM "public"."dbText_?" WHERE "Textid" = '?';";
        pst1 = con.prepareStatement(sql1);

        logger.severe("Before pstText: "+ pst1.toString());
        pst1.setString(1, "ABCDEF-001");
        pst1.setString(2, zLocale);
        logger.severe("After pstText: "+ pst1.toString());

        logger.severe("Done Testing");


Your query should be like

String sql = "SELECT lineText FROM public.dbText_ABCDEF-001 WHERE Textid = ?";

Also you cannot have prepared statement for table names as it is only for column values