Data Connectivity

No Comment - Post a comment

/* Java Program that implements data connectivity */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Test extends JFrame implements ActionListener
{
Container con;
JLabel l1;
JLabel l2;
JLabel l3;
JLabel l4;
JLabel l5;
JTextField lt1;
JTextField lt2;
JTextField lt3;
JTextField lt4;
JTextField lt5;
JButton lb1;
Connection con1;
Statement s;
ResultSet rs;
public Test()
{
super("student details!!!");
con=getContentPane();
con.setLayout(null);
l1=new JLabel("Roll no.");
l1.setBounds(50,10,250,75);
con.add(l1);

l2=new JLabel("Name");
l2.setBounds(50,30,250,75);
con.add(l2);

l3=new JLabel("Age");
l3.setBounds(50,50,250,75);
con.add(l3);

l4=new JLabel("Address");
l4.setBounds(50,70,250,75);
con.add(l4);

l5=new JLabel("Marks");
l5.setBounds(50,90,250,75);
con.add(l5);

lt1=new JTextField(10);
lt1.setBounds(115,35,100,15);
con.add(lt1);

lt2=new JTextField(20);
lt2.setBounds(115,60,100,15);
con.add(lt2);

lt3=new JTextField(30);
lt3.setBounds(115,80,100,15);
con.add(lt3);

lt4=new JTextField(40);
lt4.setBounds(115,103,100,15);
con.add(lt4);

lt5=new JTextField(50);
lt5.setBounds(115,127,100,15);
con.add(lt5);

lb1=new JButton("GO");
lb1.setBounds(250,30,100,50);
con.add(lb1);

lb1.addActionListener(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con1=DriverManager.getConnection("jdbc:odbc:mydsn","sa","");
s=con1.createStatement();
}
catch(SQLException e)
{
System.out.println(e.toString());
}
catch(ClassNotFoundException e)
{
System.out.println(e.toString());
}
}
public void actionPerformed(ActionEvent e)
{
/*int m=Integer.parseInt(lt1.getText());
String n=lt2.getText();
int o=Integer.parseInt(lt3.getText());
String p=lt4.getText();
int q=Integer.parseInt(lt5.getText());*/

String str="select *from student_det1 where roll_no='"+lt1.getText()+ "'";
System.out.println(str);
try
{
rs=s.executeQuery(str);
while(rs.next())
{
lt2.setText(rs.getString(2));
lt3.setText(rs.getString(3));
lt4.setText(rs.getString(4));
lt5.setText(rs.getString(5));
}
}
catch(SQLException ee)
{
System.out.println(ee.toString());
}
}
public static void main(String args[])
{
Test t=new Test();
t.setSize(500,600);
t.setVisible(true);
}
}

 
This Post has No Comment Add your own!

Post a Comment