
Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed in a table. Use JDBC.

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class SearchStudent {
public static void main(String[] args) {
Frame f = new Frame();
Label label = new Label("First Name: ");
final Vector columnNames = new Vector();
final Vector data = new Vector();
final TextField text = new TextField(20);
Button b = new Button("Search");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fname = text.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/roseindia", "root",
"root");
Statement st = con.createStatement();
ResultSet rs = st
.executeQuery("Select * from student where firstname='"
+ fname + "'");
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);
}
} catch (Exception ex) {
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame();
frame.add(scrollPane);
frame.setVisible(true);
frame.pack();
frame.repaint();
}
});
Panel p = new Panel(new GridLayout(2, 2));
p.add(label);
p.add(text);
p.add(b);
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.