import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
public class Student3 extends JFrame implements ActionListener
{
Connection cn;
Statement st1,st2;
ResultSet rs;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel lblNo = new JLabel("Roll No ");
JLabel lblName = new JLabel("First Name ");
JLabel lblCont = new JLabel("Contect no ");
JLabel lblCity = new JLabel("City ");
JTextField txtNo = new JTextField(12);
JTextField txtName = new JTextField(12);
JTextField txtCont = new JTextField(12);
JTextField txtCity = new JTextField(12);
JButton btnFirst = new JButton("First");
JButton btnNext = new JButton("Next");
JButton btnPrevious = new JButton("Previous");
JButton btnLast = new JButton("Last");
JButton btnAdd = new JButton("Add");
JButton btnUpdate = new JButton("Update");
JButton btnDelete = new JButton("Delete");
JButton btnExit = new JButton("Exit");
Student3()
{
try
{
panel1.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//c.fill=GridBagConstraints.BOTH;
c.gridwidth=2;
c.gridx=0;
c.gridy=0;
panel1.add(lblNo,c);
c.gridx=2;
c.gridy=0;
panel1.add(txtNo,c);
c.gridx=0;
c.gridy=1;
panel1.add(lblName,c);
c.gridx=2;
c.gridy=1;
panel1.add(txtName,c);
c.gridx=0;
c.gridy=2;
panel1.add(lblCont,c);
c.gridx=2;
c.gridy=2;
panel1.add(txtCont,c);
c.gridx=0;
c.gridy=3;
panel1.add(lblCity,c);
c.gridx=2;
c.gridy=3;
panel1.add(txtCity,c);
c.fill=GridBagConstraints.BOTH;
//c.gridwidth=1;
c.gridx=0;
c.gridy=5;
panel1.add(btnFirst,c);
c.gridwidth=1;
c.gridx=2;
c.gridy=5;
panel1.add(btnNext,c);
c.gridx=3;
c.gridy=5;
panel1.add(btnPrevious,c);
c.gridx=4;
c.gridy=5;
panel1.add(btnLast,c);
c.gridwidth=2;
c.gridx=0;
c.gridy=6;
panel1.add(btnAdd,c);
c.gridwidth=1;
c.gridx=2;
c.gridy=6;
panel1.add(btnUpdate,c);
c.gridx=3;
c.gridy=6;
panel1.add(btnDelete,c);
c.gridx=4;
c.gridy=6;
panel1.add(btnExit,c);
getContentPane().add(panel1);
btnFirst. addActionListener(this);
btnLast. addActionListener(this);
btnNext. addActionListener(this);
btnPrevious. addActionListener(this);
btnAdd. addActionListener(this);
btnUpdate. addActionListener(this);
btnDelete. addActionListener(this);
btnExit. addActionListener(this);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Load the JDBC-ODBC bridge driver
cn = DriverManager.getConnection("jdbc:odbc:STUDENT_DSN");//Connection to database is done
//st1=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
st1=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE,ResultSet.HOLD_CURSORS_OVER_COMMIT);
//cn.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
//st1.setFetchSize(25);
st2=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String query = "select * from Student";
rs=st1.executeQuery(query);
rs.first();
getRecord();
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main (String[] args)
{
Student3 my =new Student3();
my.setTitle("Java Database Operation..");
my.setVisible(true);
my.setResizable(false);
my.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
my.setBounds(200,250,300,175);
}
void getRecord()
{
try
{
txtNo.setText(rs.getObject(1).toString());
txtName.setText(rs.getObject(2).toString());
txtCont.setText(rs.getObject(3).toString());
txtCity.setText(rs.getObject(4).toString());
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public void actionPerformed(ActionEvent e)
{
try
{
Object obj = e.getSource();
if(obj == btnFirst)
{
rs.first();
getRecord();
}
if(obj == btnLast)
{
rs.last();
getRecord();
}
if(obj == btnNext)
{
rs.next();
if(!rs.isAfterLast())
{
getRecord();
}
else
{
rs.previous();
}
}
if(obj == btnPrevious)
{
rs.previous();
if(!rs.isBeforeFirst())
{
getRecord();
}
else
{
rs.next();
}
}
if(obj == btnAdd)
{
String name=txtName.getText();
String cont=txtCont.getText();
String city=txtCity.getText();
String query="insert into Student(sName,sCont,sCity) values ('"+name+"','"+cont+"','"+city+"')" ;
st2.executeUpdate(query);
query = "select * from Student";
rs=st1.executeQuery(query);
rs.last();
}
if(obj == btnUpdate)
{
int no =Integer.parseInt(txtNo.getText());
//System.out.println(no);
String new_name=txtName.getText();
String new_cont=txtCont.getText();
String new_city=txtCity.getText();
String query="update Student set sName = '"+new_name+"', sCont = '"+new_cont+"' , sCity = '"+new_city+"' where sNo = "+no+" " ;
st2.executeUpdate(query);
}
if(obj == btnDelete)
{
int no =Integer.parseInt(txtNo.getText());
String query="delete from Student where sNo = "+no+" " ;
st2.executeUpdate(query);
}
if(obj == btnExit)
{
System.exit(0);
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
If I am writing st1=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE,ResultSet.HOLD_CURSORS_OVER_COMMIT);
I get exception java.lang.UnsupportedOperationException
.
I have to use ResultSet.HOLD_CURSORS_OVER_COMMIT
due to I want hold-ability after adding new record.
In general, java.lang.UnsupportedOperationException means the implementer doesn't want to implement the method. In this case, the Odbc folks decided not to implement the three-argument overload of createConnection, probably because they don't support holdability.
I remember hearing once (where?) that the Java folks don't consider the Odbc driver to be production quality. So if you are hitting a mysql database or something, it would be better to use the vendor-specific drivers.
Oh, and by the way, If this is not throw-away code, NEVER mix your GUI and SQL in the same class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With