Showing posts with label swing. Show all posts
Showing posts with label swing. Show all posts

Wednesday, 12 December 2012

java swing program to retrive data from database and display using JTABLE



import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

class ShowFrame extends JFrame
{
ShowFrame()
{ try
{



JTable tab = new JTable();
  JScrollPane sc=new JScrollPane(tab);
DefaultTableModel df=new DefaultTableModel();
add(sc);

Class.forName("com.mysql.jdbc.Driver");
Connection cn=DriverManager.getConnection("jdbc:mysql://localhost/nirav","root","root");
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from t1 ");


ResultSetMetaData meta= rs.getMetaData();
int count=meta.getColumnCount();
String c[]=new String[count];
for(int i=0;i<count;i++)
{
c[i]=meta.getColumnName(i+1);
df.addColumn(c[i]);
}


Object row[]=new Object[count];
while(rs.next())
{
for(int i=0;i<count;i++)
{
row[i]=rs.getString(i+1);
}
df.addRow(row);
}

tab.setModel(df);
//add(pan);
rs.close();
  cn.close();
}
catch(Exception e)
{
}
}

             }


class MainRetrive
{
public static void main(String str[])
{
ShowFrame sf=new ShowFrame();
//sf.setSize(200,200);
sf.setBounds(200,200,400,300);
sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sf.setVisible(true);

}

}

java swing program with jdbc to performed insert and retrive opeartion.




import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

class ShowFrame extends JFrame
{
ShowFrame()
{ try
{

JTable tab = new JTable();
JScrollPane sc=new JScrollPane(tab);


DefaultTableModel df=new DefaultTableModel();
add(sc);
Class.forName("com.mysql.jdbc.Driver");
Connection cn=DriverManager.getConnection("jdbc:mysql://localhost/nirav","root","root");
Statement st=cn.createStatement();


ResultSet rs=st.executeQuery("select * from t1 where");
ResultSetMetaData meta= rs.getMetaData();
int count=meta.getColumnCount();
String c[]=new String[count];


for(int i=0;i<count;i++)
{
c[i]=meta.getColumnName(i+1);
df.addColumn(c[i]);
}
Object row[]=new Object[count];
while(rs.next())
{
for(int i=0;i<count;i++)
{
row[i]=rs.getString(i+1);
}
df.addRow(row);
}
tab.setModel(df);

rs.close();
cn.close();
}

catch(Exception e)
{
System.out.println(e.toString());
}
}

}
class MyClass extends JFrame
{
JLabel l1,l2,l3;
JTextField id,name;
JPanel pan;
JButton insert,retrive;
MyClass()
{
l1=new JLabel("Id");
l2=new JLabel("Name");
l3=new JLabel("");
id=new JTextField(15);
name=new JTextField(15);
insert=new JButton("Insert");
retrive=new JButton("Retrive");

pan=new JPanel(new GridLayout(4,2));
pan.add(l1);
pan.add(id);
pan.add(l2);
pan.add(name);
pan.add(insert);
pan.add(retrive);
pan.add(l3);
add(pan,BorderLayout.CENTER);

insert.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{ try{

Class.forName("com.mysql.jdbc.Driver");
Connection cn=DriverManager.getConnection("jdbc:mysql://localhost/nirav","root","root");

Statement st=cn.createStatement();
int idd=Integer.parseInt(id.getText());
String n=name.getText();
int i=st.executeUpdate("insert into t1(id,name) values("+ idd +",'"+ n +"')");
cn.close();
l3.setText(i+"successFully inserted");
}
catch(Exception e)
{l3.setText(e.toString());}
}
});
retrive.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
ShowFrame sf=new ShowFrame();
//sf.setSize(200,200);
sf.setBounds(200,200,400,300);
sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sf.setVisible(true);

}
});
}
}


class MainInsert
{
public static void main(String str[])
{

MyClass frm=new MyClass();
frm.setSize(200,200);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frm.setVisible(true);

}

}