Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

sql-update program in Java

BOB21BOB21 Member Posts: 11
Hello

I have written a program in Java that connect to mysql-db.

The resultSet of sql-queries are displayed in a JTable in a Swing GUI.

I would like to be able to update columns of the record stored in the db by double-clicking on a column in my JTable next entering a new value and hit an update-button in my Swing GUI, which commits the changes to the mysql-db and refreshes the JTable.

I should mention that I use the DefaultTableModel..

Here is update-function source-code:

[code]
private void updateRecord() {
Statement statement;
ResultSet rs;
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
String cl = getColumnAsString(table.getSelectedRow(), table.getSelectedColumn());
String cn = getColumnhead(table.getSelectedColumn());
try {
statement = connection.createStatement();
String query = "update contacts set" + cn + " where '" + cn + " = '" + cl + "'";
statement.executeUpdate( query );
//
statement.close();
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}


[/code]

When I hit the update - Button I get an SQL-syntax error, which I have been unable to figure out what to do with.

Maybe there some of You Guys who can figure it out ?

Secondly that do I need to add to this function, that refreshes the JTable then I have commited some updates to the db ??

Cheers
BOB

Comments

  • JonathanJonathan Member Posts: 2,914
    : Here is update-function source-code:
    :
    : [code]
    : private void updateRecord() {
    : Statement statement;
    : ResultSet rs;
    : int row = table.getSelectedRow();
    : int column = table.getSelectedColumn();
    : String cl = getColumnAsString(table.getSelectedRow(), table.getSelectedColumn());
    : String cn = getColumnhead(table.getSelectedColumn());
    : try {
    : statement = connection.createStatement();
    : String query = "update contacts set" + cn + " where '" + cn + " = '" + cl + "'";
    : statement.executeUpdate( query );
    : //
    : statement.close();
    : }
    : catch (SQLException sqlex) {
    : sqlex.printStackTrace();
    : }
    : }
    :
    :
    : [/code]

    Looks like you've got the update query syntax wrong. See:-

    http://www.codepedia.com/1/SQLUpdate

    Also try outputting the value of query, and posting that here, if you can't see what's wrong from looking at the page above.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.