ݺߣ

ݺߣShare a Scribd company logo
Scrollable Demo App
package com.nt.gui;
importjava.awt.Color;
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.Statement;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JTextField;
publicclassScrollableDemoextendsJFrame implementsActionListener{
private JLabel lno,lname,ladd;
private JTextFieldtno,tname,tadd;
private JButtonbfirst,bprevious,bnext,blast;
private Connectioncon;
private Statementst;
private ResultSetrs;
ScrollableDemo() throwsException{
setTitle("StudentDetails");
setLayout(newFlowLayout());
setBackground(Color.green);
setSize(400,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
lno=newJLabel("StudentID:");
add(lno);
tno=newJTextField(10);
add(tno);
lname=newJLabel("StudentName");
add(lname);
tname=newJTextField(10);
add(tname);
ladd=newJLabel("StudentAddress:");
add(ladd);
tadd=newJTextField(10);
add(tadd);
//buttons
bfirst=newJButton("First");
bfirst.addActionListener(this);
add(bfirst);
bprevious=newJButton("Previous");
bprevious.addActionListener(this);
add(bprevious);
bnext=newJButton("Next");
bnext.addActionListener(this);
add(bnext);
blast=newJButton("Last");
blast.addActionListener(this);
add(blast);
setVisible(true);
myInit();
}
private voidmyInit()throwsException{
try{
//registerderiverandestablishconnection
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","pass
word");
//create Statementobj withScrollable ResultSet
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY
);
//execute query
rs=st.executeQuery("select*from student");
}
catch(Exceptione){e.printStackTrace();}
}
@Override
publicvoidactionPerformed(ActionEventae) {
booleanfound=false;
try{
if(ae.getSource()==bfirst)
{
System.out.println("Firstbuttonclicked...");
rs.first();
found=true;
}
else if(ae.getSource()==bprevious)
{System.out.println("PreviousButtonClicked...");
if(!rs.isFirst())
{
rs.previous();
found=true;
}
}
else if(ae.getSource()==bnext)
{System.out.println("Nextbuttonclicked...");
if(!rs.isLast())
{
rs.next();
found=true;
}
}
else if(ae.getSource()==blast)
{
System.out.println("Lastbuttonclicked...");
rs.last();
found=true;
}
if(found==true)
{
tno.setText(rs.getString("sno"));
tname.setText(rs.getString("sname"));
tadd.setText(rs.getString("sadd"));
}
}
catch(Exceptione){e.printStackTrace();}
}
publicstaticvoidmain(String[] args) throwsException{
newScrollableDemo();
}
}

More Related Content

Scrollable Demo App