943,534 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4738
  • Java RSS
Dec 9th, 2008
0

Retrive Database content to JTextField and write JTextField Data to database

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vickzbrit is offline Offline
14 posts
since Mar 2006
Dec 9th, 2008
0

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

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Dec 9th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vickzbrit is offline Offline
14 posts
since Mar 2006
Dec 9th, 2008
0

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

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.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Dec 9th, 2008
0

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

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Dec 9th, 2008
0

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

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?

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vickzbrit is offline Offline
14 posts
since Mar 2006
Dec 9th, 2008
0

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

Please post rest of the code, this select statement doesn't satisfies what you trying to achieve
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Dec 9th, 2008
0

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

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.
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vickzbrit is offline Offline
14 posts
since Mar 2006
Dec 9th, 2008
0

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

Okay, I got first record set to display on my JTextFields. Following is the code for that
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vickzbrit is offline Offline
14 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: validating password
Next Thread in Java Forum Timeline: JButtons and colours





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC