Retrive Database content to JTextField and write JTextField Data to database

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 14
Reputation: vickzbrit is an unknown quantity at this point 
Solved Threads: 0
vickzbrit vickzbrit is offline Offline
Newbie Poster

Retrive Database content to JTextField and write JTextField Data to database

 
0
  #1
Dec 9th, 2008
Hello,
After making a database connection to java, I have been trying to populate my JTextFields and having a hard time to do so. Can anyone give me a quick example of what class or method am i suppose to use in order to get the results? After that my next goal will be to write from text fields to database table. Thank you

Vickzbrit
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,485
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Retrive Database content to JTextField and write JTextField Data to database

 
0
  #2
Dec 9th, 2008
Both operations are covered in the JDBC Tutorial. You'll read the values from a ResultSet object to populate the text fields. To update the database, use a PreparedStatement.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 14
Reputation: vickzbrit is an unknown quantity at this point 
Solved Threads: 0
vickzbrit vickzbrit is offline Offline
Newbie Poster

Re: Retrive Database content to JTextField and write JTextField Data to database

 
0
  #3
Dec 9th, 2008
That is a good tutorial Ezzaral. But it shows you how to do it in console app. I have no idea how to do it in GUI Java and I am using Net beans to do so. If there is any other material which could help me please let me know.

vickzbrit
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,212
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: 489
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Retrive Database content to JTextField and write JTextField Data to database

 
0
  #4
Dec 9th, 2008
That tutorial is all you need, however if you so eager to see an example get book Java-How to Program by Deitel, find chapter 25. Happy reading...
Last edited by peter_budo; Dec 9th, 2008 at 7:37 pm.
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: May 2007
Posts: 4,485
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Retrive Database content to JTextField and write JTextField Data to database

 
0
  #5
Dec 9th, 2008
Console or GUI makes no difference at all. The methods to retrieve and update database information are the same. If you get a string from a result set, you can print it to the console or put it in a text field - it's just data. Same for update - hard-coded data, console input, or text field values can be used to set the parameters on a PreparedStatement in the same manner. JDBC just gives you the API to work with the database. It doesn't matter where the data values come from or what you do with them afterwards.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 14
Reputation: vickzbrit is an unknown quantity at this point 
Solved Threads: 0
vickzbrit vickzbrit is offline Offline
Newbie Poster

Re: Retrive Database content to JTextField and write JTextField Data to database

 
0
  #6
Dec 9th, 2008
Thanks for the help so far. Here is the code that I have. I have no idea where am i going wrong but so far this piece of code is error free and I believe it is connected to the database. The only problem is it is not retrieving any data. Am I missing something here?

  1. private void formWindowOpened(java.awt.event.WindowEvent evt) {
  2. try {
  3. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  4.  
  5. } catch (java.lang.ClassNotFoundException e) {
  6. System.err.print("ClassNotFoundException: ");
  7. System.err.println(e.getMessage());
  8. }
  9. // Trying to connect to the DB
  10. try {
  11. Connection con = DriverManager.getConnection("jdbc:odbc::JNorthwind", "", "");
  12. Statement stmt = con.createStatement();
  13. ResultSet rs = stmt.executeQuery("SELECT EmployeeID, LastName, FirstName, Title, City from EMPLOYEES");
  14. while (rs.next()) {
  15. int i = rs.getInt("Employee ID");
  16. String fn = rs.getString("First Name");
  17. String ln = rs.getString("Last Name");
  18. String title = rs.getString("Title");
  19. String city = rs.getString("City");
  20. // String record = rs.getString("Record");
  21. // String outof = rs.getString("OutOf");
  22. // System.out.println(i + s" " + fn + " " + ln);
  23. txtFirstName.setText(fn);
  24. txtLastName.setText(ln);
  25. txtTitle.setText(title);
  26. txtCity.setText(city);
  27.  
  28. txtFirstName.getText();
  29.  
  30.  
  31. }
  32. stmt.close();
  33. con.close();
  34. } catch (SQLException ex) {
  35. System.err.print("SQLException: ");
  36. System.err.println(ex.getMessage());
  37. }

Vickzbrit
Last edited by vickzbrit; Dec 9th, 2008 at 7:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,212
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: 489
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Retrive Database content to JTextField and write JTextField Data to database

 
0
  #7
Dec 9th, 2008
Please post rest of the code, this select statement doesn't satisfies what you trying to achieve
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: Mar 2006
Posts: 14
Reputation: vickzbrit is an unknown quantity at this point 
Solved Threads: 0
vickzbrit vickzbrit is offline Offline
Newbie Poster

Re: Retrive Database content to JTextField and write JTextField Data to database

 
0
  #8
Dec 9th, 2008
That is all the code that I have right now. I am just trying to get 1 record right now. If i can get 1 record, I am pretty sure i can get the whole thing working fine.
  1. private void formWindowOpened(java.awt.event.WindowEvent evt) {
  2. try {
  3. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  4. // Wasn't there...
  5. } catch (java.lang.ClassNotFoundException e) {
  6. System.err.print("ClassNotFoundException: ");
  7. System.err.println(e.getMessage());
  8. }
  9. // Try to connect to the DB
  10. try {
  11. Connection con = DriverManager.getConnection("jdbc:odbc:JNorthwind","","");
  12. Statement stmt = con.createStatement();
  13. ResultSet rs = stmt.executeQuery("SELECT EmployeeID, FirstName, LastName, Title, City from EMPLOYEES");
  14. while (rs.next()) {
  15. int i = rs.getInt("Employee ID");
  16. String fn = rs.getString("First Name");
  17. String ln = rs.getString("Last Name");
  18. String title = rs.getString("Title");
  19. String city = rs.getString("City");
  20. // String record = rs.getString("Record");
  21. // String outof = rs.getString("OutOf");
  22. // System.out.println(i + " " + fn + " " + ln);
  23. txtFirstName.setText(fn);
  24. txtLastName.setText(ln);
  25. txtTitle.setText(title);
  26. txtCity.setText(city);
  27.  
  28.  
  29.  
  30. }
  31. stmt.close();
  32. con.close();
  33. } catch (SQLException ex) {
  34. System.err.print("SQLException: ");
  35. System.err.println(ex.getMessage());
  36. }
  37.  
  38. try {
  39. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  40.  
  41. } catch(java.lang.ClassNotFoundException e) {
  42. System.err.print("ClassNotFoundException: ");
  43. System.err.println(e.getMessage());
  44. }
  45.  
  46. try {
  47. Connection con = DriverManager.getConnection("jdbc:odbc:JNorthwind","","");
  48. Statement stmt = con.createStatement();
  49. // stmt.executeUpdate("INSERT INTO EMPLOYEES (FirstName, LastName, Title, City) VALUES ('Vicky', 'Brit', 'abcd', 'Cali')");
  50. stmt.close();
  51. con.close();
  52.  
  53. }catch(SQLException ex) {
  54. System.err.print("SQLException: ");
  55. System.err.println(ex.getMessage());
  56. }
  57.  
  58. }

vickzbrit
Last edited by vickzbrit; Dec 9th, 2008 at 8:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 14
Reputation: vickzbrit is an unknown quantity at this point 
Solved Threads: 0
vickzbrit vickzbrit is offline Offline
Newbie Poster

Re: Retrive Database content to JTextField and write JTextField Data to database

 
0
  #9
Dec 9th, 2008
Okay, I got first record set to display on my JTextFields. Following is the code for that
  1. rs.getInt(1);
  2. rs.getInt(2);
  3. rs.getInt(3);
  4. rs.getInt(4);

Now I have to figure out how can I make the forward button to work. If anyone willing to help out please do.

Vickzbrit
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC