source file name: SwingJdbc.java
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SwingJdbc extends JFrame implements ActionListener
{
JTextField t1,t2,t3;
public SwingJdbc()
{
GridLayout gl=new GridLayout(0,2,15,15);
setLayout(gl);
JLabel l1=new JLabel("Enter Roll : ");
JLabel l2=new JLabel("Enter Name : ");
JLabel l3=new JLabel("Enter class: ");
t1=new JTextField(30);
t2=new JTextField(30);
t3=new JTextField(30);
JButton bt1=new JButton("insert");
JButton bt2=new JButton("clear");
JButton bt3=new JButton("delete");
JButton bt4=new JButton("display");
add(l1);add(t1);
add(l2);add(t2);
add(l3);add(t3);
add(bt1);add(bt2);
add(bt3);add(bt4);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
}
public void actionPerformed(java.awt.event.ActionEvent ae)
{
String bts=ae.getActionCommand();
System.out.println("bt pressed is "+bts);
if(bts.equals("insert"))
addrecord();
if(bts.equals("display"))
recordshow();
if(bts.equals("clear"))
clear();
if(bts.equals("delete"))
delete();
}
public void clear()
{
t2.setText("");
t3.setText("");
t1.setText("");
}
public void delete()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:javadsn");
Statement st = conn.createStatement();
String rname=JOptionPane.showInputDialog(this,"Enter roll no");
int rn=Integer.parseInt(rname);
String s="delete from student where rollno="+rn+" ";
st.executeUpdate(s);
conn.close();
st.close();
}
catch(SQLException e)
{System.out.println(e); }
catch(Exception e1)
{System.out.println(e1);}
}
public void recordshow()
{
String data="";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:javadsn");
Statement st = conn.createStatement();
String s="select * from student";
ResultSet rs=st.executeQuery(s);
ResultSetMetaData rsmd=rs.getMetaData();
int cnt=rsmd.getColumnCount();
String col[]=new String[cnt];
for(int i=0;i<cnt;i++)
col[i]=rsmd.getColumnName(i+1);
String[][] records=new String[20][cnt];
int row=0;
while(rs.next())
{
for(int i=0;i<cnt;i++)
records[row][i]=rs.getString(i+1)+" ";
row++;
}
JTable tb=new JTable(records,col);
JScrollPane sp=new JScrollPane(tb);
JDialog d=new JDialog(this);
d.add(sp);
d.setLocation(450,50);
d.setSize(400,350);
d.setVisible(true);
conn.close();
st.close();
}
catch(SQLException e)
{System.out.println(e); }
catch(Exception e1)
{System.out.println(e1);}
}
public void addrecord()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:javadsn");
Statement st = conn.createStatement();
String s="insert into student values("+t1.getText()+",'"+t2.getText()+" ','"+t3.getText()+"')";
st.executeUpdate(s);
conn.close();
st.close();
}
catch(SQLException e)
{System.out.println(e); }
catch(Exception e1)
{System.out.println(e1);}
JOptionPane.showMessageDialog(this,"Current record successfully added");
}
public static void main(String[] args)
{
SwingJdbc f=new SwingJdbc();
f.setTitle("Swing JDBC program");
f.setSize(400,250);
f.setVisible(true);
}
}
output:
1. Add record to Table
2. Delete record from Table
0 Comments
Post a Comment