source file name: jdbcform.java
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
class jdbcform extends Frame implements ActionListener
{
TextField t1,t2,t3,t4;
public jdbcform()
{
GridLayout gl=new GridLayout(0,2,15,15);
setLayout(gl);
Label l1=new Label("Enter Rollno");
Label l2=new Label("Enter class");
Label l3=new Label("Enter marks of subejct 1");
Label l4=new Label("Enter marks of subject 2");
t1=new TextField(5);
t2=new TextField(5);
t3=new TextField(5);
t4=new TextField(5);
Button bt1=new Button("insert");
Button bt2=new Button("display");
add(l1);add(t1);
add(l2);add(t2);
add(l3);add(t3);
add(l4);add(t4);
add(bt1);add(bt2);
bt1.addActionListener(this);
bt2.addActionListener(this);
}
public void actionPerformed(java.awt.event.ActionEvent ae)
{
String strbt=ae.getActionCommand();
System.out.println("sql is: "+strbt);
if(strbt.equals("insert"))
addrecord();
if(strbt.equals("display"))
showrecord();
}
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 result values ("+t1.getText()+",' "+t2.getText()+" ',"+t3.getText()+","+t4.getText()+")";
st.executeUpdate(s);
conn.close();
st.close();
}
catch(SQLException e)
{System.out.println(e); }
catch(Exception e1)
{System.out.println(e1);}
}
public void showrecord()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:javadsn");
Statement st = conn.createStatement();
String s="select * from result";
ResultSet rs=st.executeQuery(s);
String data="";
while(rs.next())
{
data=data+rs.getString(1)+" "+rs.getString(2)+ " "+rs.getString(3)+" "+rs.getString(4)+"\n";
}
TextArea ta=new TextArea(data);
Dialog d=new Dialog(this);
d.setLocation(400,100);
d.add(ta);
d.setSize(300,300);
d.setVisible(true);
conn.close();
st.close();
}
catch(SQLException e)
{System.out.println(e); }
catch(Exception e1)
{System.out.println(e1);}
}
public static void main(String[] args)
{
jdbcform f=new jdbcform();
f.setTitle("jdbc form program");
f.setSize(400,300);
f.setVisible(true);
}
}
output:
0 Comments
Post a Comment