| | |
Retrive Database content to JTextField and write JTextField Data to database
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 14
Reputation:
Solved Threads: 0
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
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
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.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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.
•
•
Join Date: Mar 2006
Posts: 14
Reputation:
Solved Threads: 0
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?
Vickzbrit
Java Syntax (Toggle Plain Text)
private void formWindowOpened(java.awt.event.WindowEvent evt) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } // Trying to connect to the DB try { Connection con = DriverManager.getConnection("jdbc:odbc::JNorthwind", "", ""); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT EmployeeID, LastName, FirstName, Title, City from EMPLOYEES"); while (rs.next()) { int i = rs.getInt("Employee ID"); String fn = rs.getString("First Name"); String ln = rs.getString("Last Name"); String title = rs.getString("Title"); String city = rs.getString("City"); // String record = rs.getString("Record"); // String outof = rs.getString("OutOf"); // System.out.println(i + s" " + fn + " " + ln); txtFirstName.setText(fn); txtLastName.setText(ln); txtTitle.setText(title); txtCity.setText(city); txtFirstName.getText(); } stmt.close(); con.close(); } catch (SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); }
Vickzbrit
Last edited by vickzbrit; Dec 9th, 2008 at 7:45 pm.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Mar 2006
Posts: 14
Reputation:
Solved Threads: 0
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.
vickzbrit
Java Syntax (Toggle Plain Text)
private void formWindowOpened(java.awt.event.WindowEvent evt) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Wasn't there... } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } // Try to connect to the DB try { Connection con = DriverManager.getConnection("jdbc:odbc:JNorthwind","",""); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT EmployeeID, FirstName, LastName, Title, City from EMPLOYEES"); while (rs.next()) { int i = rs.getInt("Employee ID"); String fn = rs.getString("First Name"); String ln = rs.getString("Last Name"); String title = rs.getString("Title"); String city = rs.getString("City"); // String record = rs.getString("Record"); // String outof = rs.getString("OutOf"); // System.out.println(i + " " + fn + " " + ln); txtFirstName.setText(fn); txtLastName.setText(ln); txtTitle.setText(title); txtCity.setText(city); } stmt.close(); con.close(); } catch (SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { Connection con = DriverManager.getConnection("jdbc:odbc:JNorthwind","",""); Statement stmt = con.createStatement(); // stmt.executeUpdate("INSERT INTO EMPLOYEES (FirstName, LastName, Title, City) VALUES ('Vicky', 'Brit', 'abcd', 'Cali')"); stmt.close(); con.close(); }catch(SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } }
vickzbrit
Last edited by vickzbrit; Dec 9th, 2008 at 8:58 pm.
•
•
Join Date: Mar 2006
Posts: 14
Reputation:
Solved Threads: 0
Okay, I got first record set to display on my JTextFields. Following is the code for that
Now I have to figure out how can I make the forward button to work. If anyone willing to help out please do.
Vickzbrit
Java Syntax (Toggle Plain Text)
rs.getInt(1); rs.getInt(2); rs.getInt(3); 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
![]() |
Other Threads in the Java Forum
- Previous Thread: validating password
- Next Thread: JButtons and colours
| Thread Tools | Search this Thread |
Tag cloud for Java
account actuate android api apple applet application arguments array arrays automation banking binary bluetooth character chat class classes client code component constructor crashcourse database design draw eclipse error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide idea if_statement image input integer interface j2me java javaarraylist javadoc javaee javamicroeditionuseofmotionsensor javaprojects jetbrains jmf jni jpanel julia linux list loop map method methods mobile multithreading netbeans newbie number oracle print printing problem program programming project property recursion remove scanner screen server set size sms socket software sort splash sql stop string swing test textfield thread threads time tree validation windows






