Results 1 to 5 of 5
  1. #1
    srengvichet is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default Error isResultSet is closed ???

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    /**
    *
    * @author SRENGVICHET
    */
    public class Employee extends JFrame implements ActionListener {

    private JTextField tfid, tfname, tfsalary, tfsearchn, tfusequery;
    private JButton bntupdate, bntsearchn, bntfirst, bntprevious, bntnext, bntlast, bntinsert, bntdeleted, bntusequery;
    private ResultSet rs;
    private Statement stm;
    private JTable table;
    private DefaultTableModel tablemodel;
    private JScrollPane scroll;
    private Connection con;

    public void getUseQuery() {
    try {
    //String query = tfusequery.getText();//select * from Employee
    String query="select * from Employee";
    stm.executeQuery(query);
    Object ob[]={rs.getString(1),rs.getString(2),rs.getString(3)} ;
    tablemodel.addRow(ob);

    } catch (Exception e) {
    System.err.println("Error is" + e.getMessage());
    }
    }

    public Employee() {

    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(3, 2));
    p1.add(new JLabel("Id"));
    p1.add(tfid = new JTextField(10));
    p1.add(new JLabel("Name"));
    p1.add(tfname = new JTextField(10));
    p1.add(new JLabel("Salary"));
    p1.add(tfsalary = new JTextField(10));

    JPanel p2 = new JPanel();
    p2.setLayout(new FlowLayout());
    p2.add(bntfirst = new JButton("|<"));
    bntfirst.addActionListener(this);
    p2.add(bntprevious = new JButton("<"));
    bntprevious.addActionListener(this);
    p2.add(bntnext = new JButton(">"));
    bntnext.addActionListener(this);
    p2.add(bntlast = new JButton(">|"));
    bntlast.addActionListener(this);
    p2.add(bntupdate = new JButton("Update"));
    bntupdate.addActionListener(this);
    p2.add(bntdeleted = new JButton("Deleted"));
    bntdeleted.addActionListener(this);
    p2.add(bntinsert = new JButton("Insert"));
    bntinsert.addActionListener(this);


    JPanel p3 = new JPanel();
    p3.setLayout(new FlowLayout());
    p3.add(new JLabel("Search"));
    p3.add(tfsearchn = new JTextField(10));
    p3.add(bntsearchn = new JButton("Searchn"));
    p3.add(new JLabel("Use Query"));
    p3.add(tfusequery = new JTextField(10));
    p3.add(bntusequery = new JButton("Query"));
    bntusequery.addActionListener(this);

    JPanel p4=new JPanel();
    p4.setLayout(new FlowLayout());
    tablemodel=new DefaultTableModel();
    tablemodel.addColumn("ID");
    tablemodel.addColumn("Name");
    tablemodel.addColumn("Salary");
    table=new JTable(tablemodel);
    scroll=new JScrollPane(table);
    p4.add(scroll);



    JPanel p23=new JPanel();
    p23.setLayout(new FlowLayout());
    p23.add(p2);
    p23.add(p3);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(p1, BorderLayout.NORTH);
    panel.add(p23, BorderLayout.CENTER);
    panel.add(p4,BorderLayout.SOUTH);

    getConnected();
    add(panel);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(640, 650);
    setLocationRelativeTo(null);
    setVisible(true);

    }

    public void getConnected() {
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con = DriverManager.getConnection("jdbc:odbc:DataEmploye e", "", "");
    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIV E, ResultSet.CONCUR_UPDATABLE);
    rs = stm.executeQuery("select * from Employee");
    rs.first();
    getShowRecord();
    rs.close();
    } catch (Exception e) {
    System.err.println("Error is " + e.getMessage());
    }
    }

    public void getShowRecord() throws SQLException {
    tfid.setText(rs.getString(1));
    tfname.setText(rs.getString(2));
    tfsalary.setText(rs.getString(3));

    }

    public void getFirst() {
    try {
    if (rs.isLast()) {
    rs.first();
    }
    getShowRecord();
    } catch (Exception e) {
    System.err.println("Eorr is " + e.getMessage());
    }
    }

    public void getNext() {
    try {
    if (rs.isLast()) {
    rs.first();
    } else {
    rs.next();
    }
    getShowRecord();
    } catch (Exception e) {
    System.err.println("Error is" + e.getMessage());
    }
    }

    public void getLast() {
    try {
    if (rs.isFirst()) {
    rs.last();
    } else {
    rs.last();
    }
    getShowRecord();
    } catch (Exception e) {
    System.err.println("Error is" + e.getMessage());
    }
    }

    public void getPrevoius() {
    try {
    if (rs.isFirst()) {
    rs.last();
    } else {
    rs.previous();
    }
    getShowRecord();

    } catch (Exception e) {
    System.err.println("Error is" + e.getMessage());
    }
    }

    public void getUpdate() throws SQLException {
    rs.updateInt(1, Integer.parseInt(tfid.getText().trim()));
    rs.updateString(2, tfname.getText());
    rs.updateDouble(3, Double.parseDouble(tfsalary.getText().trim()));
    }

    public void getUpdateData() {
    try {
    getUpdate();
    rs.updateRow();
    } catch (Exception e) {
    System.err.println("Error is" + e.getMessage());
    }
    }

    public void getDeleted() {
    try {
    rs.deleteRow();
    } catch (Exception e) {
    System.err.println("Error is " + e.getMessage());
    }
    }

    public void getInsert() {
    try {

    rs.moveToInsertRow();
    rs.updateInt(1, Integer.parseInt(tfid.getText()));
    rs.updateString(2, tfname.getText());
    rs.updateDouble(3, Double.parseDouble(tfsalary.getText()));
    rs.insertRow();
    rs.moveToCurrentRow();
    } catch (Exception e) {
    System.err.println("Error is" + e.getMessage());
    }
    }

    public void getSearchName(){

    try {
    int id=Integer.parseInt(rs.getInt(1)+"");
    String name=rs.getString(1);
    double salary=Double.parseDouble(rs.getDouble(1)+"");
    Object ob[]={name};
    for(int i=0;i<ob.length-1;i++){
    for(int j=i+1;j<ob.length;j++){
    if(ob[i].equals(ob[j])){
    getShowRecord();
    }
    }
    }

    } catch (Exception e) {
    System.err.println("Error is"+e.getMessage());
    }
    }

    public void actionPerformed(ActionEvent e) {
    if (e.getSource() instanceof JButton) {
    JButton bnt = (JButton) e.getSource();
    if (bnt == bntfirst) {
    getFirst();
    } else if (bnt == bntprevious) {
    getPrevoius();
    } else if (bnt == bntnext) {
    getNext();
    } else if (bnt == bntlast) {
    getLast();
    } else if (bnt == bntsearchn) {
    getSearchName();
    } else if (bnt == bntinsert) {
    getInsert();
    } else if (bnt == bntdeleted) {
    getDeleted();
    } else if (bnt == bntupdate) {
    getUpdateData();
    } else if (bnt == bntusequery) {
    getUseQuery();

    }
    }
    }



    public static void main(String sr[]) {
    Employee emp = new Employee();
    }
    }
    :confused:

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    posting your whole code will not help. We will not look at your code and guess what might the problem or what is the error.

    please post
    What is your expected output?
    Show codes that CONCERNS with the error.
    If there is an error message then tell us.
    What is the output?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Use CODE tags.
    Ensure the code is formatted properly.
    And tell us the exact exception and stack trace you are getting, and what line (not line number) it is occurring on.

    Of course, for the stack trace you will need to improve on your error logging by doing printStackTrace(), and not simply printing the exception message.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    I waded through that mess and a mess it is ... in your getConnected method you obtain a ResultSet and you close it again in that same method; all your other method use that (closed) ResultSet; that's why the JVM threw the Exception.

    kind regards,

    Jos

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    The way you have framed the code is very confusing.

    Just add the comments.Keep steps and method name should match your operation.

    Order should be like this below to avoid messups and erroneous result.
    1. Open the connection
    2. Do the database operation.
    3. Close the databse resources.
    Ramya:cool:

Similar Threads

  1. Replies: 3
    Last Post: 04-05-2010, 02:19 AM
  2. Replies: 1
    Last Post: 01-31-2010, 03:17 AM
  3. Exception : ResultSet is closed
    By nanimtech in forum JDBC
    Replies: 5
    Last Post: 08-06-2009, 03:11 PM
  4. Socket Closed!
    By rameshraj in forum Networking
    Replies: 0
    Last Post: 05-06-2008, 11:48 AM
  5. Socket Closed!
    By rameshraj in forum Networking
    Replies: 0
    Last Post: 05-06-2008, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •