1,105,633 Community Members

Retrive Database content to JTextField and write JTextField Data to database

Member Avatar
vickzbrit
Newbie Poster
14 posts since Mar 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
vickzbrit
Newbie Poster
14 posts since Mar 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

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...

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
vickzbrit
Newbie Poster
14 posts since Mar 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

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

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

Please post rest of the code, this select statement doesn't satisfies what you trying to achieve

Member Avatar
vickzbrit
Newbie Poster
14 posts since Mar 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

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

Member Avatar
vickzbrit
Newbie Poster
14 posts since Mar 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Okay, I got first record set to display on my JTextFields. Following is the code for that

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article