Hi Friend,
Try the following code:
1)Create 'CreateTable.java':
import javax.swing.*;
import java.awt.*;
class CreateTable extends JFrame{
CreateTable()
{
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setSize(300, 150);
}
}
2)Create 'Test.java':
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
class Form extends JFrame {
JButton ADD,SAVE;
JPanel panel;
JLabel label1;
final JTextField text1;
Form() {
final Vector columnNames = new Vector();
final Vector data = new Vector();
label1 = new JLabel("Extract Record(s) of:- ");
text1 = new JTextField(20);
ADD=new JButton("Serach");
panel=new JPanel();
panel.add(label1);
panel.add(text1);
panel.add(ADD);
add(panel,BorderLayout.CENTER);
ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value=text1.getText();
Connection con = null;
String url = "jdbc:
mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
Statement st = con.createStatement();
ResultSet rs= st.executeQuery("Select * from "+value);
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 );
}
rs.close();
st.close();
}
catch(Exception e) {
System.out.println( e );
}
CreateTable tab=new CreateTable();
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane( table );
tab.add( scrollPane );
tab.setVisible(true);
}
});
}
}
class Test{
public static void main(String arg[]) {
Form frame=new Form();
frame.setSize(350,100);
frame.setVisible(true);
}
}
Hope that it will be helpful for you.
Thanks