
Hello Sir, I am working on project where i have to show the search result in the same panel of where search field and Button is present .I have code which is doing the same work but when i enter next search value in the textfield it makes another set of columns in the gridview of search table .
whereas Accordingprint("code sample"); to my need I want only current search results alongwith the column as header of gridview table.Another set of database column headers should not be added to the gridview table and old search result should be deleted showing only current results.
I am posting my code.Plz have a look and send me the required code .
will always be gratefull. Thankyou for ur consideration....
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);print("code sample");
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.