Updating a MySQL database using Java

Thread Solved

Join Date: Jan 2008
Posts: 19
Reputation: jsully1 is an unknown quantity at this point 
Solved Threads: 0
jsully1 jsully1 is offline Offline
Newbie Poster

Updating a MySQL database using Java

 
0
  #1
Nov 2nd, 2008
I am taking a web design and development class and we have been thrown into MySQL along with java without much knowledge beforehand. Was wondering if anyone could help me with understanding how to query the database and update entries, as I am confused on how to accomplish this.
I am able to insert new entries and display table info, but when it comes to adding to existing info nothing seems to work. I can get the statement to work if I supply the data in the SQL statement i.e UPDATE tableName SET columnName1 = value1, WHERE criteria. But when I attempt to use user input and send variables along nothing seems to work.

Any help would be appreciated.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Updating a MySQL database using Java

 
0
  #2
Nov 3rd, 2008
same thing as in the forum PreparedStatements

http://java.sun.com/docs/books/tutor.../prepared.html
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,197
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Updating a MySQL database using Java

 
-1
  #3
Nov 3rd, 2008
Maybe this tutorial can help you
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: jsully1 is an unknown quantity at this point 
Solved Threads: 0
jsully1 jsully1 is offline Offline
Newbie Poster

Re: Updating a MySQL database using Java

 
0
  #4
Nov 6th, 2008
OK, I am still having trouble with this. All I want right now is to get something to work so I am going small. I am accessing a books database that consists of three tables, and all I want is to extract the authorID(primary key) by having the user enter the authors lastName. Currently this is what I have:
  1. public void AddTitle() throws SQLException {
  2.  
  3. //PreparedStatement addTitle = (PreparedStatement) connection.prepareStatement
  4. //("INSERT INTO titles (title) VALUES (?) WHERE authorId FROM authorISBN = (?)");
  5. try
  6. {
  7. PreparedStatement p = connection.prepareStatement("SELECT authorID FROM authors WHERE lastName = ?") ;
  8.  
  9. String authorsFirstName, authorsLastName, newTitle, id;
  10. //authorsFirstName = GetAuthorFirstName();
  11. authorsLastName = GetAuthorLastName();
  12. //newTitle = GetNewTitle();
  13.  
  14. p.setString(1, authorsLastName);
  15. //p.execute();
  16. ResultSet rs = p.executeQuery();
  17. Integer key = rs.getInt("authorID");
  18.  
  19. //Object key = rs.();
  20. System.out.printf("Key for author %s is %d \n", authorsLastName, key);
  21. }//end try
  22. catch(Exception exception)
  23. {
  24.  
  25. exception.printStackTrace();
  26. }
  27.  
  28.  
  29. }//end AddTitle method
And I receive a java.sql.SQLException: Before start of result set when I try and use the method. Any suggestions would be helpful. Thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,197
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Updating a MySQL database using Java

 
-1
  #5
Nov 7th, 2008
authorsLastName, I do not see that variable passed down to AddTitle() method. If you not able to solved it, provide the whole code for this servlet
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Updating a MySQL database using Java

 
1
  #6
Nov 7th, 2008
Uhm, Peter, what's this
  1. authorsLastName = GetAuthorLastName();
;-)

However, with the way it's written (if it compiles at all) authorsLastName is an instance variable anyway (as it is not declared in the method simply defined), and that is probably not a good idea.

Otherwise, knowing exactly what SQLException you get would help. Post the rest of the message. I can almost guarantee, however, that it has to do with what GetAuthorLastName() is returning (or not).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: jsully1 is an unknown quantity at this point 
Solved Threads: 0
jsully1 jsully1 is offline Offline
Newbie Poster

Re: Updating a MySQL database using Java

 
0
  #7
Nov 7th, 2008
Figured it out. Thanks to all who responded.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC