Java programming tutorial with Official Hord - Retrieving data from database into text field with TableClick

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@official-hord·
0.000 HBD
Java programming tutorial with Official Hord - Retrieving data from database into text field with TableClick
#### What Will I Learn?
- You will learn how to Use onClickAction on Jtable
- You will learn to Pass data from ResultSet to TextField

#### Requirements
- Netbeans IDE
- Basic Java Knowlege

#### Difficulty
- Intermediate

#### Tutorial Contents
Hi everyone, from our last tutorial [Here](https://utopian.io/utopian-io/@official-hord/java-programming-tutorial-with-official-hord-connecting-to-database-and-retrieving-data-to-table) we have learnt how to Connect to a database and also pull data from the connected database and pass it into a table. In this tutorial I'll be explaining how to pick from the data in the database and place in a jTextField.
>Text fields allow users to input text and usually appear in forms. Users may enter text, numbers, or mixed-format types of input.

*****************************
**First we add the text fields from the pallet and place labels by their side.**
![](https://steemitimages.com/DQmdunv9ysq4wP7LjmqCWphadiPdHR4Xh9wqy9WikHmCjyb/image.png)

*****************************
Next we right click on the table and go to events, go to mouse and click mouse clicked.
![](https://steemitimages.com/DQmZSJNpcb5Vw3Qav35MRAfdpFoGyuNeRfwxC8GDdjS7QVU/image.png)

Your screen will switch to the source view and will look like this.
![](https://steemitimages.com/DQmYaZr13cGzviHfpesvHQkzYqnBJHU64xYr4RKdbffBKWv/image.png)
******************************
Now we create an Integer variable to hold the row index, and a string to get the text in the chosen column index of the selected row.
```
int row  = table.getSelectedRow();
String tableclick = (table.getModel().getgetValueAt(row, 0).toString());
```
![](https://steemitimages.com/DQmdoXizVfqMrT7JJexGS7NEeLS4bViykvFhY76NuTtyrNk/image.png)

In the  mouseclicked action segment enter;
```
String sql = "select * from yourtablename where username = '"+tableclick+"'"; //creates the string to hold the text in the first column of the row clicked

try{
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();

}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
```

![](https://steemitimages.com/DQmbAQsVByUa756PTYBfyviTgjfsb7snYs1xbh452AovPNW/image.png)

Now we create two strings to hold data from the database.
```
String firstname = rs.getString("username");//This pulls the username data from the database
String lastname = rs.getString("Fullname");
```

![](https://steemitimages.com/DQmdrN49rFCwmryNSgwsUo6gDMMY9dvvv5ahpc4wBzhAvdH/image.png)
Now change the variable name of the text fields from the default to something that can be easily remembered by you, i've used firstnames and lastnames for my textfields.  Follow this screenshot.
![](https://steemitimages.com/DQmbQLcRTPGQv56gMEawtnDVoSnp4nh51mHy9SKCvSV9VTL/image.png)
And you should get this ![](https://steemitimages.com/DQmbn62jV8YxP3M3X4D145mPuEjv31J4qSs61enAhgukGXa/image.png) enter your desired variable name and click ok, repeat the same process for the other textfield.
********************************
Finally we add the Strings to the TextFields using .setText(); method. This methods allow you change  the text in a textfield or label in java.
```
firstnames.setText("firstname");
lastnames.setText("lastname");
```
![](https://steemitimages.com/DQmRF2UtomjvtoATD6j7XJCvrSFMHBXgQWaCupNKqoFMK82/image.png)
Before running the code, do add the if statement above the rs.getString(); else there will be and error, the if statement checks if there is feedback from the database.
***************************************************
**And here's what we have done succesfully**
![](https://steemitimages.com/DQmUmGn46nEoMgzdUbJnRDGrgD2U7XyD5WhDsm8SsorkCpC/image.png)
Upon clicking on any of the row, the username and fullname  gets copied into the textfields respectively.

#### Curriculum
Other Related tutorial
- [Java programming tutorial with Official Hord - Connecting to Database and Retrieving data to Table](https://utopian.io/utopian-io/@official-hord/java-programming-tutorial-with-official-hord-connecting-to-database-and-retrieving-data-to-table)
    

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@official-hord/java-programming-tutorial-with-official-hord-retrieving-data-from-database-into-text-field-with-tableclick">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,