
hi i want to search data from oracle data base bus i couldn't do it
in my program i have to text field and save data with save button,data saved successfully but i unable to retrieve data, my code as follow
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
String n=name.getText(); String c=cont.getText();
Connection1 con = new Connection1();
Connection conob=con.Open();
PreparedStatement pst=conob.prepareStatement("insert into contact values(?,?)");
pst.setString(1,n);
pst.setString(2,c);
pst.executeUpdate();
}catch(Exception e) { e.printStackTrace(); } }
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String y="";
try{
Connection1 con =new Connection1(); Connection conob=con.Open();
PreparedStatement pst=conob.prepareStatement("select num from contact where name =?");
ResultSet rs = pst.executeQuery();
while(rs.next()) {
x.setText(rs.getString(2));
}
}
catch(Exception e){ e.printStackTrace(); } // TODO add your handling code here: }

Retrieve data from the database and display data on the JFrame
The given code retrieves the data from the mysql database and display it on the textfields through button action.
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
class Retrieve{
public static void main(String[] args){
JFrame f=new JFrame();
JLabel label1=new JLabel("Name: ");
JLabel label2=new JLabel("Address: ");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
JButton show=new JButton("Show data");
show.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from data where id=1");
String name="",address="";
if(rs.next()){
name=rs.getString("name");
address=rs.getString("address");
}
text1.setText(name);
text2.setText(address);
}
catch(Exception ex){
}
}
});
JPanel p=new JPanel(new GridLayout(3,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(show);
f.add(p);
f.setVisible(true);
f.pack();
}
}
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.