Results 1 to 3 of 3
- 08-19-2011, 03:55 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
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
2. ControllerJava 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; }
3.Button in GUIJava Code:public void setWinner(int wid, int tid, Connection con) { TaskAuctionMapper tam = new TaskAuctionMapper(); tam.setWinner(wid, tid, con); }
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; }
- 08-20-2011, 02:58 AM #2
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
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
- 08-22-2011, 11:57 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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
-
How to insert/update Date field in Oracle with java code
By sasi.tati in forum AWT / SwingReplies: 2Last Post: 07-28-2010, 04:14 PM -
insert data from text field to jtable
By mackinas in forum New To JavaReplies: 2Last Post: 06-09-2010, 04:30 AM -
Check for empty arguments
By sandy1028 in forum New To JavaReplies: 8Last Post: 04-17-2009, 01:30 PM -
how to insert date field read from Excel using JExcel to database
By saran123 in forum JDBCReplies: 1Last Post: 10-10-2008, 08:47 AM -
Not Empty check of texboxes using javascript
By Anju Jose in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-09-2008, 12:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks