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

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

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

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.

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

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

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

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

That is very nice information.
Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.