Avatar of Mafalda
Mafalda

asked on 

Problem inserting a record with JDBC

The following fails saying:  Column Not Found  although the columns exist.

public ResultSet insertNew(String type, String color, int rows, int cols, boolean abc) throws SQLException
  {
    rs = st.executeQuery ("select pid, type, color, rows, columns, ABC from stands");

    rs.moveToInsertRow();
    int pid = getNextID();
    rs.updateInt("pid", pid);
    rs.updateString("type", type);
    rs.updateString("color", color);
    rs.updateInt("rows", rows);
    rs.updateInt("columns", cols);
    rs.updateBoolean("ABC", abc);
    rs.insertRow();

    return rs;

  }

Any ideas ?
Java

Avatar of undefined
Last Comment
zzynx
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of kiranhk
kiranhk

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                       ResultSet.CONCUR_UPDATABLE);

have you set your resultset as updatable...
SOLUTION
Avatar of kiranhk
kiranhk

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

whats the stack trace for the error?
It may be that one of your columns names is a reserved word.

can u also post the create statement from the table.
Avatar of Mafalda
Mafalda

ASKER

1) I used "type as t" in case "type is a reserved word"
2) I use Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                       ResultSet.CONCUR_UPDATABLE);
3) I followed the examples like  http://java.sun.com/docs/books/tutorial/jdbc/jdbc2dot0/sample.html
4) i just get an SQLException: Column not found
 
Avatar of Mafalda
Mafalda

ASKER

Table is in Access, simple types no constrains, no must fields, very simple
Avatar of Mafalda
Mafalda

ASKER

I usualy do not like to construct SQL INSERT statements as the lenght is limited (2k in some dbs)
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you can use the following statement to print out a stack trace

e.printStackTrace();

where e is the exception being thrown
Avatar of Mafalda
Mafalda

ASKER

Trace:

java.sql.SQLException: Column not found
        at sun.jdbc.odbc.JdbcOdbcResultSet.findColumn(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcResultSet.updateInt(Unknown Source)
        at Stands.insertNew(Stands.java:64)
        at MedOrganizer.insertStand(MedOrganizer.java:84)
        at MedOrganizer.main(MedOrganizer.java:98)
Avatar of Mick Barry
Mick Barry
Flag of Australia image

which is line 64 in the above code?
Avatar of Mafalda
Mafalda

ASKER

line 64 is
rs.updateInt("pid", pid);
Avatar of Mafalda
Mafalda

ASKER

Chaning the column names into number

    rs.updateInt(1, pid);
    rs.updateString(2, type);
    rs.updateString(3, color);
    rs.updateInt(4, rows);
    rs.updateInt(5, cols);
    rs.updateBoolean(6, abc);



gives me the followinf error:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]Error in row
        at sun.jdbc.odbc.JdbcOdbcResultSet.setPos(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcResultSet.insertRow(Unknown Source)
        at Stands.insertNew(Stands.java:76)
        at MedOrganizer.insertStand(MedOrganizer.java:84)
        at MedOrganizer.main(MedOrganizer.java:98)

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

what version of java are you running it with?
Avatar of dorothy2
dorothy2
Flag of United States of America image

Are you trying to change the value of the primary key in the "stands" table? Access may be throwing errors to prevent you from doing that. I'm not sure why you would want to - if the column is defined as a primary key then you have a unique identifier for your record.

Dorothy
Avatar of zzynx
zzynx
Flag of Belgium image

Thanks for accepting
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo