
Hello Sir, I have a search frame window from which we can search and see the results in the same panel of the Jframe in Gridview Jtable format ...
Jtable is being constructed dynamically based on columns of database query....
But when we execute search for the second time database table columns are also being added alongwith the search result fields in the dynamically created Jtable Gridview ....
Sir, According to my requirement i need results in the Jtable gridview format in the sam epanel of jframe window .But when i execute second search first search result should be deleted and current search result should be displayed in the jpanel of jframe window....
import java.sql.*; import java.util.*; import java.awt.event.*; import java.lang.String; import javax.swing.*; import javax.swing.table.*;
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");
final JTable table=new JTable();
final JScrollPane pane=new JScrollPane(table);
JFrame f = new JFrame();
lab.setBounds(10,10,100,20);
t.setBounds(120,10,100,20);
b.setBounds(120,40,80,20);
pane.setBounds(10,90,480,170);
f.add(lab);
f.add(t);
f.add(b);
f.add(pane);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pane.setVisible(false);
f.setSize(600,300);
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+"'");
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);
Vector str=columnNames;
DefaultTableModel model=new DefaultTableModel(data,str);
table.setModel(model);
pane.setVisible(true);
}
}
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.