5
    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,因为我想在添加新记录后保持能力。

4

1 回答 1

7

一般来说,java.lang.UnsupportedOperationException 意味着实现者不想实现该方法。在这种情况下,Odbc 人员决定不实现 createConnection 的三参数重载,可能是因为他们不支持可保持性。

我记得曾经听说过(在哪里?)Java 人员不认为 Odbc 驱动程序具有生产质量。因此,如果您要访问 mysql 数据库或其他东西,最好使用供应商特定的驱动程序。


哦,顺便说一句,如果这不是一次性代码,千万不要在同一个类中混合你的 GUI 和 SQL。

于 2012-12-22T06:56:59.247 回答