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);
}
}
}
如果我正在撰写st1=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE,ResultSet.HOLD_CURSORS_OVER_COMMIT);
,我会遇到异常java.lang.UnsupportedOperationException
。
我必须使用ResultSet.HOLD_CURSORS_OVER_COMMIT
,因为我想在添加新记录后保持能力。
答案 0 :(得分:7)
通常,java.lang.UnsupportedOperationException意味着实现者不希望实现该方法。在这种情况下,Odbc人员决定不实现createConnection的三参数重载,可能是因为它们不支持可保持性。
我记得听过一次(在哪里?)Java人员不认为Odbc驱动程序是生产质量。因此,如果您正在使用mysql数据库或其他东西,最好使用特定于供应商的驱动程序。
哦,顺便说一句,如果这不是丢弃代码,请不要在同一个类中混合使用GUI和SQL。