Results 1 to 3 of 3
  1. #1
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default Check if field in DB is empty, if it is insert ?!?

    Hello

    I'm again working with databases. Now I have this little problem. I need to insert some data into the database, but before I do that, I must check if the field that I'm inserting data into, already contains some values. If it does, don't insert the data, if it does, insert the data. It takes two integers "winnerid" and "taskid".

    Here are the classes
    1. Mapper
    Java Code:
    public boolean setWinner(int winnerid, int taskid, Connection con)
    	{
    		int rowsInserted = 0;
    		 String SQLString = "select * from taskauction where taskid=? and winnerid is null";   
    		 
    		 String SQLString2 = "update taskauction set winnerid=?"+
    	    	                  "where taskid=?";
    	      
    	     
    	       PreparedStatement statement=null;
    	       
    	       try
    	        {
    	            //===check if user assigned
    	            statement = con.prepareStatement(SQLString);
    	            statement.setInt(1,taskid);
    	            ResultSet rs  = statement.executeQuery();
    	            if (rs.wasNull())
    	            {
    	            	try{
    	      	          statement = con.prepareStatement(SQLString2);
    	      	          statement.setInt 	  (1, winnerid);
    	      	          statement.setInt    (2, taskid);
    	      	             rowsInserted  = statement.executeUpdate();
    	      	      }
    	      	      catch (Exception e)
    	      	      {   
    	      	          System.out.println("Fail in TaskAuctionMapper - setWinner");
    	      	          System.out.println(e.getMessage());
    	      	      }
    	      	 
    	      	      return rowsInserted == 1;
    	             
    	            }  
    	            else
    	            	return rowsInserted == 0;
    	        }
    	        catch (Exception e)
    	        {   
    	            System.out.println("Fail");
    	            System.out.println(e.getMessage());
    	        }
    		return rowsInserted == 1;
    	       
    	      
    	    }
    2. Controller
    Java Code:
    public void setWinner(int wid, int tid, Connection con)
        {
            TaskAuctionMapper tam = new TaskAuctionMapper();
           tam.setWinner(wid, tid, con);
    
        }
    3.Button in GUI

    Java Code:
    private JButton getSelect() {
    		if (Select == null) {
    			Select = new JButton();
    			Select.setBounds(new Rectangle(504, 116, 76, 22));
    			Select.setText("Select");
    			Select.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					//GET VALUES
    					String us = jTextField.getText();
    					int userid = Integer.parseInt(us);
    					String tis = jTextField1.getText();
    					int tid = Integer.parseInt(tis);
    					//UPDATE
    					try{
    						GUIController toc3 = new GUIController();
    						toc3.setWinner(userid, tid, cons.getConnection());
                                                    annouceField.setText("User assigned !!!");
    					}catch (Exception esd)
    					{
    						System.out.println(esd);
    					}
    					
    					
    				}
    			});
    		}
    		return Select;
    	}

  2. #2
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    SQL has an if not exist statement you should be able to just plug that in to your insert statement somehow. That's how i do it. or just return a resultset from a test select statement and verify the number of elements in it. If it's 0 then it doesn't exist

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,814
    Rep Power
    19

    Default

    Are you talking about a single field in an existing row, rather than inserting a new row?
    If so (and it looks like you are) then a single statement:
    UPDATE taskauction SET winnerid=? WHERE taskid=? AND winnerid = NULL

    You don't want to select and then update based on the select as the rows may have been updated inbetween the two statements.

Similar Threads

  1. Replies: 2
    Last Post: 07-28-2010, 04:14 PM
  2. insert data from text field to jtable
    By mackinas in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 04:30 AM
  3. Check for empty arguments
    By sandy1028 in forum New To Java
    Replies: 8
    Last Post: 04-17-2009, 01:30 PM
  4. Replies: 1
    Last Post: 10-10-2008, 08:47 AM
  5. Not Empty check of texboxes using javascript
    By Anju Jose in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-09-2008, 12:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •