Thread: How to Insert the Row in ResultSet by moveToInsertRow() ... insertRow()?

How to Insert the Row in ResultSet by moveToInsertRow() ... insertRow()?

From
vasylenko@uksatse.org.ua
Date:

Hello!
|--------------------------------------------------------------------------|
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|         I've got the Postgre database and table insdide                  |
|                                                                          |
|         code:                                                            |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|         CREATE TABLE groups                                              |
|         (                                                                |
|          id int4 NOT NULL DEFAULT nextval('seq_group_id'::regclass),     |
|          g_name text NOT NULL,                                           |
|          CONSTRAINT i_group PRIMARY KEY (id)                             |
|         )                                                                |
|         WITHOUT OIDS;                                                    |
|         ALTER TABLE groups OWNER TO vasylenko;                           |
|         COMMENT ON TABLE groups IS 'Группы работников (объект, смена     |
|         ...)';                                                           |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|         As You can see: 2 fields                                         |
|         id - it's an autofill field.                                     |
|                                                                          |
|         But when I try to insert the new Row in code putting under It    |
|         generete the SQLException, that "id" filed was not found in      |
|         ResultSet. But it mustn't be there as for me.                    |
|         I created the id field with default value from the sequence      |
|         code:                                                            |
|                                                                          |
|                                                                          |
|                                                                          |
|         CREATE SEQUENCE seq_group_id                                     |
|          INCREMENT 1                                                     |
|          MINVALUE 1                                                      |
|          MAXVALUE 9223372036854775807                                    |
|          START 26                                                        |
|          CACHE 1;                                                        |
|         ALTER TABLE seq_group_id OWNER TO vasylenko;                     |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|         The Code is:                                                     |
|         code:                                                            |
|                                                                          |
|                                                                          |
|                                                                          |
|         ResultSet rs;                                                    |
|         Statement stat =                                                 |
|         conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CO|
|         NCUR_UPDATABLE);                                                 |
|         rs =  stat.executeQuery("Select g_name FROM groups");            |
|         try{                                                             |
|                                          rs.moveToInsertRow();           |
|         /**                                                              |
|         ** !!!!!!!The exception generated there !!!!!!!!!                |
|         */                                                               |
|                                          }                               |
|                                          catch(SQLException ex){         |
|                                                                          |
|         System.out.print(ex.getMessage()+"\n");                          |
|                                                           return;        |
|                                          }                               |
|                                                                          |
|                                                           try{           |
|                                String temp = "New Group";                |
|                                                                          |
|         rs.updateObject(i+1, temp);                                      |
|                                                           }              |
|                                                                          |
|         catch(SQLException ex)                 {                         |
|                                                                          |
|         System.out.print(ex.getMessage()+"\n");                          |
|         return;                                                          |
|                                                           }              |
|                                                                          |
|                                                                          |
|                                          try{                            |
|                                          rs.insertRow();                 |
|                                          }                               |
|                                          catch(SQLException ex)          |
|         {                                                                |
|                                                                          |
|         System.out.print(ex.getMessage()+"\n");                          |
|                                                                          |
|                                           }                              |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|         I don't neew to make the ResultSet with both fields... 'cos I use|
|         this ResultSet Data in JTable table model.                       |
|         How to solve the problem?                                        |
|         Thanks.                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|                                                                          |
|--------------------------------------------------------------------------|




Re: How to Insert the Row in ResultSet by moveToInsertRow() ... insertRow()?

From
Dave Cramer
Date:
On 10-Jan-07, at 5:47 AM, vasylenko@uksatse.org.ua wrote:

>
>
> Hello!
> |---------------------------------------------------------------------
> -----|
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |         I've got the Postgre database and table
> insdide                  |
> |
>      |
> |
> code:                                                            |
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |         CREATE TABLE
> groups                                              |
> |
> (                                                                |
> |          id int4 NOT NULL DEFAULT nextval
> ('seq_group_id'::regclass),     |
> |          g_name text NOT
> NULL,                                           |
> |          CONSTRAINT i_group PRIMARY KEY
> (id)                             |
> |         )
>      |
> |         WITHOUT
> OIDS;                                                    |
> |         ALTER TABLE groups OWNER TO
> vasylenko;                           |
> |         COMMENT ON TABLE groups IS 'Группы
> работников (объект, смена     |
> |         ...)';
>      |
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |         As You can see: 2
> fields                                         |
> |         id - it's an autofill
> field.                                     |
> |
>      |
> |         But when I try to insert the new Row in code putting
> under It    |
> |         generete the SQLException, that "id" filed was not found
> in      |
> |         ResultSet. But it mustn't be there as for
> me.                    |
> |         I created the id field with default value from the
> sequence      |
> |
> code:                                                            |
> |
>      |
> |
>      |
> |
>      |
> |         CREATE SEQUENCE
> seq_group_id                                     |
> |          INCREMENT
> 1                                                     |
> |          MINVALUE
> 1                                                      |
> |          MAXVALUE
> 9223372036854775807                                    |
> |          START
> 26                                                        |
> |          CACHE
> 1;                                                        |
> |         ALTER TABLE seq_group_id OWNER TO
> vasylenko;                     |
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |         The Code
> is:                                                     |
> |
> code:                                                            |
> |
>      |
> |
>      |
> |
>      |
> |         ResultSet
> rs;                                                    |
> |         Statement stat
> =                                                 |
> |         conn.createStatement
> (ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CO|
> |
> NCUR_UPDATABLE);                                                 |


You only selected g_name , not id, g_name

Dave
> |         rs =  stat.executeQuery("Select g_name FROM
> groups");            |
> |         try
> {                                                             |
> |                                          rs.moveToInsertRow
> ();           |
> |         /
> **                                                              |
> |         ** !!!!!!!The exception generated
> there !!!!!!!!!                |
> |
> */                                                               |
> |                                          }
>      |
> |                                          catch(SQLException ex)
> {         |
> |
>      |
> |         System.out.print(ex.getMessage()
> +"\n");                          |
> |
> return;        |
> |                                          }
>      |
> |
>      |
> |                                                           try
> {           |
> |                                String temp = "New
> Group";                |
> |
>      |
> |         rs.updateObject(i+1,
> temp);                                      |
> |                                                           }
>      |
> |
>      |
> |         catch(SQLException ex)
> {                         |
> |
>      |
> |         System.out.print(ex.getMessage()
> +"\n");                          |
> |
> return;                                                          |
> |                                                           }
>      |
> |
>      |
> |
>      |
> |                                          try
> {                            |
> |                                          rs.insertRow
> ();                 |
> |                                          }
>      |
> |                                          catch(SQLException
> ex)          |
> |
> {                                                                |
> |
>      |
> |         System.out.print(ex.getMessage()
> +"\n");                          |
> |
>      |
> |                                           }
>      |
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |         I don't neew to make the ResultSet with both fields...
> 'cos I use|
> |         this ResultSet Data in JTable table
> model.                       |
> |         How to solve the
> problem?                                        |
> |
> Thanks.                                                          |
> |
>      |
> |
>      |
> |
>      |
> |
>      |
> |---------------------------------------------------------------------
> -----|
>
>
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster


Re: How to Insert the Row in ResultSet by moveToInsertRow() ... insertRow()?

From
vasylenko@uksatse.org.ua
Date:

>Re-post:
>Such declaration like this
>
>id int4 NOT NULL DEFAULT nextval('seq_group_id'::regclass)
>and
>CONSTRAINT i_group PRIMARY KEY (id)
>
>give me the oportumity don't bother about unique value of id
>and use another query.
>
>INSERT INTO  groups(g_name) VALUES("new group");
>
>I hope to find the same easy way in using ResultSet's objects (), i.e. to
forget about id in my code until it nessesary for me and not for jdbc.

Re: How to Insert the Row in ResultSet by moveToInsertRow() ... insertRow()?

From
vasylenko@uksatse.org.ua
Date:

Such declaration like this

id int4 NOT NULL DEFAULT nextval('seq_group_id'::regclass)
and
CONSTRAINT i_group PRIMARY KEY (id)

give me the oportumity don't bother about unique value of id
and use another query.

INSERT INTO  groups(g_name) VALUES("new group");

I hope to find the same easy way in using ResultSet's objects (), i.e. to
forget about id in my code until it nessesary for me and not for jdbc.