Results 1 to 6 of 6

Thread: Delete record from a java interface

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Delete record from a java interface

    Hi,
    I wanted to delete the record of the database from a java interface . But the removal of the record does not work. This is the code of the request:
    Code:
    String query ="DELETE FROM users WHERE (Code = '"+ + CNTCODE" ', Name ='"+ + cntNom" ', FirstName ='"+ + cntnm"');
    This code of adding works:
    Code:
    String query ="insert into user values ( '"+ + CNTCODE"','"+ + cntNom"','"+ + cntnm"','"+ + cntLogin"','"+ + cntPassword"','"+ + cntpass"')";
    So how do I delete the record? or is there a problem in my program?
    Note - I am using database a Access.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Delete record from a java interface

    Hi,
    I think your syntax for DELETE is wrong. The various elements of the clause "WHERE" are separated by logical operators (AND, OR, etc.). And not by commas.
    Code:
    String query ="DELETE FROM users WHERE (Code = '"+ + CNTCODE" ', Name ='"+ + cntNom" ', FirstName ='"+ + cntnm"');
    In addition, Java uses readily available PreparedStatement class to give the driver the task of the DBMS to manage its peculiarities (escape characters, etc..). This allows for example to have strings containing with no problems. I think you should read some book or documents on sql, so that you can get familiar with the syntax.

  3. #3
    Join Date
    Dec 2009
    Posts
    296

    Re: Delete record from a java interface

    Hey,
    The syntax is correct now, the program compiles with 0 errors. But the removal is not done, I do not understand why and it does not display the error message that I specify. This is the code if you can still help me,
    Code:
    if(B == VSupprim)
    		{
    			if((SCode.getText().equals(""))||(Snom.getText().equals(""))||(SPrenom.getText().equals("")))
    				{/ / Error message.
    					JOptionPane.showMessageDialog(null, "All fields must be filled","Standard", JOptionPane.ERROR_MESSAGE);
    				}
    			else
    			{
    			String ccd = sCode.getText();
    String = CNOM Snom.getText();
    String cpnm = SPrenom.getText();
    
    String query ="DELETE FROM users WHERE code = '"+ + ccd" 'AND name ='"+ + CNOM" 'AND FirstName ='"+ + cpnm"'";
    			try
    			{/ / connection to the database and execute statement
    				Class.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    
      Connection conn = DriverManager.getConnection("jdbc: odbc: Database");
     Statement st = conn.createStatement();
     				
    				  int nb = st.executeUpdate(query);
    				  if (nb ==0)
      					{/ / Error message.
      						JOptionPane.showMessageDialog(null, "Error while deleting","Message", JOptionPane.WARNING_MESSAGE);
      					}
      				else
    				  	{
      						JOptionPane.showMessageDialog(null, "User deleted successfully","Message", JOptionPane.INFORMATION_MESSAGE);
      					}					
    					 st.close();
    conn.close();
      			}
      			
      			catch(Exception eb){
    				}
    			}
    		}

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Delete record from a java interface

    Hello,
    I think you are over committing your transactions, I mean to say, I just reread your code.
    Code:
      			catch(Exception eb){
    				}
    Just watch the above part of the code, I think you have used it too much in your code and the code has become more complicated. I think you should minimize the use of such things from your code.

  5. #5
    Join Date
    Dec 2009
    Posts
    296

    Re: Delete record from a java interface

    Hey
    catch(Exception eb){
    }
    Sorry, I do not understand what you want to say. I can not find the error, is it in the syntax of my program? When I replace the line by removing a motion to add it turns normally but does not delete the record. But, still I did not understand what you are trying to say with the catch block in my code. Is it wrongly placed in the program.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Delete record from a java interface

    Hello,
    You should also make sure that the connection will be released in all cases, just look at the sample code below, I would recommend this to you first.
    Code:
    Connection connection = null;
    try
    {
       connection = DriverManager.getConnection(...);
       ...
    }
    catch (Exception e)
    {
       ...
    }
    finally
    {
       if (connection! = null) connection.close();
    }

Similar Threads

  1. what is interface in java
    By Ansari Bros in forum Software Development
    Replies: 4
    Last Post: 10-01-2011, 10:07 AM
  2. Shape interface of java
    By Gajananvihari in forum Software Development
    Replies: 4
    Last Post: 09-03-2010, 05:08 PM
  3. Action Interface of java
    By Protectors in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 08:19 AM
  4. ButtonModel interface of java
    By Gokul20 in forum Software Development
    Replies: 5
    Last Post: 22-02-2010, 12:48 PM
  5. Delete this record when it becomes stale
    By Magnus Kirkerud in forum Windows Server Help
    Replies: 1
    Last Post: 19-04-2007, 09:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,881,405.50971 seconds with 17 queries