problem with update query

Hi ,

I am new to JDBC and requesting this help from you all.

I have a table in MySQL db and the tabel consists of 3 columns(nodeid(integer),parentid(integer),nodename(varchar)).this is a chatroom application and each time a user joins the particular chatroom,i should update this table.I should first count the no. of users in a particular topic and update the topic column .for eg,if the nodename column is something like this before,careers(0),where 0 signifies no. of users.In this case there are 0 users.When the user joins careers chatroom i should update the nodename like this,careers(1).I can get this number from another table and assign the number to a variable say numOfUsers(int).but i don't know how to write an update query to update the entry.please help

[789 byte] By [javapria] at [2007-11-27 11:14:34]
# 1

hi it's me again .I am getting error for this statement

if(numOfUsers>0)

stmt.executeUpdate("update defaultchatrooms set nodename = '"+chatTopic+"'('"+numOfUsers+"')'");

javapria at 2007-7-29 14:07:39 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

> hi it's me again .I am getting error for this

> statement

> > if(numOfUsers>0)

> stmt.executeUpdate("update defaultchatrooms set

> s set nodename =

> '"+chatTopic+"'('"+numOfUsers+"')'");

>

1. Your update string looks like this:

update defaultchatrooms set nodename = 'someChatTopic'('0')'

Does that look right to you?

2. This will update the column nodename in every row in the table defaultchatrooms. Aren't you missing a "where" condition?

dwga at 2007-7-29 14:07:39 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

As you can see from dwg's answer, it's hard to build a query string like that. It's easy to get confused with all those quotes and so on.

(I have never understood why people post that sort of code here without first just printing the resulting query string to see exactly what's in it. Why is that not the obvious first step?)

But anyway, stop doing that. Use a PreparedStatement. Not only is it easier, it's more reliable.

DrClapa at 2007-7-29 14:07:39 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...