Newsgroup: comp.lang.java.programmer


Date: Wed, 12 Apr 2006 09:20:48 +0300
From: Diomidis Spinellis <dds@aueb.gr>
Organization: Athens University of Economics and Business
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060130 SeaMonkey/1.0
MIME-Version: 1.0
Newsgroups: comp.lang.java.programmer
Subject: Re: Updating a list of users on the database
References: <Xns97A2E04D65A16asdfasdfasdf@70.169.32.36>
In-Reply-To: <Xns97A2E04D65A16asdfasdfasdf@70.169.32.36>
Content-Type: text/plain; charset=ISO-8859-7; format=flowed
Content-Transfer-Encoding: 7bit
AY wrote:
> I'm kinda new at this Java stuff! But I gotta question about 
> insert/updating a list of users on a database. You see I have a table:
> 
> USERS
> -----
> USERID (PK)
> USERNAME (UNIQUE INDEXED)
> PASSWORD
> 
> I made a GUI to allow a user to either add or edit a user. But the problem 
> is, I need to check whether or not a user exists before I insert or update. 
> I have to do a SELECT query first before I do an INSERT. I also do a SELECT 
> before I update a user to check if the user exists. (Why? So I don't have 
> to use exceptions for logic flow) Is this too much querying on a database 
> just do add/update a user? Any suggestions on what I should do? Is that a 
> bad design? Should I use the SQLException.getErrorCode() ?
> 
> Thanks in advance!

A database is your friend, don't be afraid to query it.  There may 
however be an issue with what you propose.  Consider what will happen 
when other programs (or instances of your program) access the database 
at the same time.  If this can't be outruled, then you have to group the 
query/update pair into a transaction; see 
java.sql.Connection.setAutoCommit, and commit/rollback.

-- 
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality?cljp



Newsgroup comp.lang.java.programmer contents
Newsgroup list
Diomidis Spinellis home page

Creative Commons License Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.