Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Update limited rows

Thread view: 
Khan - 25 Mar 2006 10:35 GMT
Hi,
    I want to update only 2 rows in a table. I'm using rowcount but
having error (Syntax error, expecting one of the following: MODE,
TRANSACTION....)

stmt.executeUpdate("set rowcount 2");
stmt.executeUpdate("update sasuser.Color set text = 'blue' where value
= 'blue'");

Advise some solution.

Thanx.
Rhino - 25 Mar 2006 15:19 GMT
> Hi,
>     I want to update only 2 rows in a table. I'm using rowcount but
[quoted text clipped - 6 lines]
>
> Advise some solution.

From the code fragment in your question, I strongly suspect that you are a
newbie: "set rowcount 2" in an executeUpdate() statement makes no sense. The
'rowCount' is the _result_ of the executeUpdate() method, not an input. For
example, this fragment shows the proper use of executeUpdate():

       String updateRowsSQL = "update my.table set salary = salary * 1.5
where workdept = 'D21'";
       Statement updateRowsStmt = null;
       try {
           updateRowsStmt = conn01.createStatement();
           int rowCount = updateRowsStmt.executeUpdate(updateRowsSQL);
       } catch (SQLException sql_excp) {
           System.err.println(CLASS_NAME + "." + METHOD_NAME
                   + " - Encountered SQLException during update. Error: " +
sql_excp);
           sql_excp.printStackTrace();
           System.exit(16);
       }

In this example, 'rowCount' is the value returned by executeUpdate() to show
you the number of rows which _were_ updated; it is not the number of rows
that you _want_ to update.

Now, you may still want to limit the number of rows of your table which are
updated but the normal way of accomplishing that is to write the WHERE
clause of the UPDATE statement so that only the desired rows are updated.
Remember, the SQL UPDATE statement is designed to update _all_ of the rows
which satisfy the WHERE, no matter how many rows that is.

If you can't write a WHERE clause that lets you identify the exact two rows
you want to update, it is very possible that you haven't defined your table
correctly; perhaps you need an additional column or columns to describe each
row more fully. It is also highly possible that you haven't defined a
primary key on your table or that you have chosen the wrong primary key. You
may want to post your table design, indicating all columns in the table and
the primary key that you have chosen, so that someone can assess your design
to see why it doesn't let you update the rows you want to update. Newbies to
database design frequently make mistakes so they should always get more
experienced designers to review their designs.

--
By the way, database questions like yours should normally be asked on
comp.lang.java.databases, not here, since they are more appropriate for that
newsgroup.

--
Rhino
Khan - 26 Mar 2006 06:43 GMT
you catch me , i'm new to java, but i was trying to use sql function
"set roucount" before executing the update stmt.

i'll refer java.database.
Owen Jacobson - 26 Mar 2006 10:45 GMT
> you catch me , i'm new to java, but i was trying to use sql function
> "set roucount" before executing the update stmt.

But what, exactly, do you expect 'set rowcount' to do?  It's not a
standard SQL settable value, and rowcount is normally used for output from
the database engine only.

Take a step back from your problem.  Instead of saying "I only want to
update two rows", consider the question "which rows do I want to update?"
and "what criteria did I use to decide that?".  Once you know that you
have something you can use in a WHERE clause in the update statement.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.