
I have 2 database tables one is history the other is patient,
How can I get which every patient is being displayed on the text fields To have their history on History table displayed on a Jtextarea

hi friend,
try the following code below :
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class FetchDataIntoJTextArea implements ActionListener{
JTextField textbox = new JTextField();
JTextArea textarea=new JTextArea(5,20);
JButton b=new JButton("Patient History");
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/record";
String userName = "root";
String password = "root";
public void createUI()
{
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
JLabel tbLabel = new JLabel("Patient Name : ");
JLabel taLabel=new JLabel("Patient History : ");
b.addActionListener(this);
tbLabel.setBounds(10, 30, 100, 20);
textbox.setBounds(120,30,150,20);
taLabel.setBounds(10,60,100,20);
textarea.setBounds(120,60,150,60);
b.setBounds(120,130,150,20);
f.add(tbLabel);
f.add(textbox);
f.add(taLabel);
f.add(textarea);
f.add(b);
f.setVisible(true);
f.setSize(400,300);
}
public static void main(String[] args){
FetchDataIntoJTextArea dd = new FetchDataIntoJTextArea();
dd.createUI();
}
@Override
public void actionPerformed(ActionEvent e) {
b = (JButton)e.getSource();
getOperation();
}
public void getOperation()
{
String textboxValue = textbox.getText();
try
{
Class.forName(driverName);
Connection con = DriverManager.getConnection(url, userName, password);
String sql = "select pHistory from patient where pName" +
" = '"+textboxValue+"'";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
textarea.setText(rs.getString("pHistory"));
}
JOptionPane.showMessageDialog(null, "Retrieved data succesfully." +
"","Record Retrieved",
JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.