Updating a list of users on the database

A

AY

Hi All!

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!
 
D

Diomidis Spinellis

AY said:
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.
 
D

David Segall

AY said:
Hi All!

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)
I think this is the correct way to do it. It allows you to postpone
locking the record for update until you know the update can be
completed in a few milliseconds rather than at the pace of the person
updating the record. It also means you can usually provide a sensible
error message if two users try to insert the same record at about the
same time. Unfortunately, you still have to code the INSERT exception
in case your SELECT was too early to catch another INSERT.
 
R

Rhino

AY said:
Hi All!

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() ?
Doing a SELECT so that you can determine whether a key already exists is a
very standard thing in programs that use databases. You should _not_ worry
that this is going to put some great extra load on the database. A properly
written professional database manager, like DB2 or MySQL, will be able to
support hundreds and even thousands of SQL statements per second if the
database is properly designed and the hardware supports it.

I'm not sure why you're so worried about coding exceptions or "logic flow";
they're really not that difficult to write.

Depending on what database you are using, you may be able to simplify your
life somewhat. For example, MySQL has a special version of the INSERT
statement that does an INSERT if a given key is new but does an UPDATE if
the key already exists. Here is the relevant page from the MySQL manual:
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html. This would
let you have a single statement that can do either an INSERT or an UPDATE
and skip the SELECT entirely. But many database managers don't support this
special extension of SQL; certainly DB2 doesn't unless this support has been
added in Version 9.

With regards to error handling, SQLException.getErrorCode() will give you
the vendor's error code for a given situation. That error code will be
specific to the vendor: that means that the error code that you get for
trying to insert a duplicate record will be different between MySQL and DB2,
for example. If you are absolutely certain that you will stay with the same
database for a very long time, the vendor error code is probably a
reasonable thing to check. On the other hand, if you think you might switch
to a different database vendor somewhere down the road, you might find it
smarter to check the SQLState via SQLException.getSQLState(). The SQLState
value is a return code shared by virtually all of the vendors of database
management systems. Therefore, an Oracle database, a DB2 database, and a
MySQL database will all return the exact same value if they try to insert a
duplicate row. That means that if you eventually switch your database
manager from DB2 to Oracle or Sybase to MySQL, your error checking should
not have to change if your error checking uses the SQLState. Some of your
_other_ database code may have to change, especially if you used database
extensions that are only available on a particular vendor's system rather
than standard stuff available from all vendors, but your error handling
shouldn't have to change. At least in theory!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top