
Hello Sir, I made search button with textfield .when i enter search value in the search field only one row is displayed in the jtable .Suppose i enter name in search field which has two rows in the database table so i want to display both or any number of identicle rows in the jtable .I am posting my code .plz see and give me the remaning code .
Will always be gratefull. Waiting for ur reply .Thanks
import java.awt.*; import java.sql.*; import java.awt.event.*; import javax.swing.*;
class Search {
public static void main(String[] args) {
JLabel lab = new JLabel("Enter Name:");
final JTextField t = new JTextField(20);
JButton b = new JButton("Search");
JPanel p = new JPanel(new GridLayout(1, 1));
p.add(lab);
p.add(t);
p.add(b);
JFrame f = new JFrame();
f.getContentPane().add(p);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationRelativeTo(null);
f.pack();
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = t.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/techsoft", "root", "techsoft");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from ankur where name='" + name + "'");
String n = "", add = "", contact = "", email = "";
if (rs.next()) {
n = rs.getString("name");
add = rs.getString("address");
contact = rs.getString("contactNo");
email = rs.getString("email");
}
String data[][] = new String[1][4];
data[0][0] = n;
data[0][1] = add;
data[0][2] = contact;
data[0][3] = email;
JFrame frame = new JFrame();
String labels[] = {"Name", "Address", "Contact No", "Email"};
JTable table = new JTable(data, labels);
JScrollPane pane = new JScrollPane(table);
frame.add(pane);
frame.setSize(500, 100);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// frame.pack();
} catch (Exception ex) {
System.out.println(e);
}
}
});
}
}

Here is a code that search the data from the database and show multiple identical rows from database on clicking search button to jtable.
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
class Search{
public static void main(String[] args) {
final Vector columnNames = new Vector();
final Vector data = new Vector();
JLabel lab=new JLabel("Enter Name:");
final JTextField t=new JTextField(20);
JButton b = new JButton("Search");
JPanel p = new JPanel(new GridLayout(2,2));
p.add(lab);
p.add(t);
p.add(b);
JFrame f = new JFrame();
f.getContentPane().add(p);
f.pack();
f.setVisible(true);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String name=t.getText();
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 name='"+name+"'");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++){
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
JFrame frame=new JFrame();
JTable table = new JTable(data, columnNames);
JScrollPane pane=new JScrollPane(table);
frame.add(pane);
frame.setVisible(true);
frame.pack();
}
catch(Exception ex){
System.out.println(e);
}
}
});
}
}
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.