import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DBPgm extends Frame implements ActionListener
{
Label NameLabel,RollLabel;
TextField NameText,RollText;
Button Next,Previous,Insert,Clear;
Connection conn;
Statement st;
ResultSet rs;
PreparedStatement ps1;
DBPgm() throws Exception
{
super("Database Example ");
setSize(500,200);
setVisible(true);
setLayout(null);
addWindowListener(new WAdapter());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
conn=DriverManager.getConnection("jdbc:odbc:studsn");
System.out.println("Connection Established");
ps1=conn.prepareStatement("INSERT INTO Student(name,rollno) VALUES(?,?);");
st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
refresh();
NameLabel=new Label("Student Name ");
NameLabel.setBounds(20,30,90,25);
add(NameLabel);
RollLabel=new Label("Student Rollno ");
RollLabel.setBounds(20,60,90,25);
add(RollLabel);
NameText=new TextField(20);
NameText.setBounds(110,35,200,18);
add(NameText);
RollText=new TextField(3);
RollText.setBounds(110,60,50,18);
add(RollText);
Next=new Button("Next Record");
add(Next);
Next.setBounds(30,100,100,20);
Next.addActionListener(this);
Previous=new Button("Previous Record");
add(Previous);
Previous.setBounds(140,100,100,20);
Previous.addActionListener(this);
Insert=new Button("Insert New Record");
add(Insert);
Insert.setBounds(250,100,130,20);
Insert.addActionListener(this);
Clear=new Button("Clear");
add(Clear);
Clear.setBounds(400,100,60,20);
Clear.addActionListener(this);
if(rs.next())
{
NameText.setText(rs.getString("name"));
RollText.setText(rs.getString("rollno"));
}
}
private void refresh() throws Exception
{
rs=st.executeQuery("select * from Student");
}
private void ClearField()
{
NameText.setText("");
RollText.setText("");
}
private void InsertData() throws Exception
{
ps1.setString(1,NameText.getText());
ps1.setInt(2,Integer.parseInt(RollText.getText()));
ps1.executeUpdate();
refresh();
ClearField();
}
private void putData() throws Exception
{
ResultSetMetaData rsmd=rs.getMetaData();
int count=rsmd.getColumnCount();
for(int i=0;i<count;i++)
{
NameText.setText(rs.getString(rsmd.getColumnName(1)));
RollText.setText(rs.getString(rsmd.getColumnName(2)));
}
}
public void actionPerformed(ActionEvent a)
{
try
{
if(a.getSource()==Previous)
{
if(rs.previous())
{
putData();
Next.setEnabled(true);
}
else
Previous.setEnabled(false);
}
else if(a.getSource()==Next)
{
if(rs.next())
{
Previous.setEnabled(true);
putData();
}
else
Next.setEnabled(false);
}
else if(a.getSource()==Clear)
{
ClearField();
}
else if(a.getSource()==Insert)
{
InsertData();
}
}
catch(Exception e)
{}
}
}
class WAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent me)
{
System.exit(0);
}
}
class DBClass
{
public static void main(String args[]) throws Exception
{
DBPgm a=new DBPgm();
}
}