View Poll Results: poll back

Voters
0. You may not vote on this poll
  • Excellent

    0 0%
  • good

    0 0%
  • medium

    0 0%
  • bad

    0 0%
  • very bad

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

    Default Invalid cursor state(jdbc and odbc connection using resultset statement)

    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;

    /**
    *
    * @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;


    //constructure
    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"));
    bntsearchn.addActionListener(this);
    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);


    //call method getConnected()
    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();
    } 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();
    } else {
    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();
    getUpdate();
    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 getUseQuery(){
    try {
    //String query = tfusequery.getText();
    String query = "select * from Employee";
    if(rs!=null){
    rs=null;
    }
    rs = 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 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();
    }
    }



    /*
    message error is
    Error is[Microsoft][ODBC Driver Manager] Invalid cursor state


    when pressed Query Button immoderately it show message Error above because i want to use query to select all from table Employee to show all fields Employee on table
    */

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What you really want to do here? What's your question, and what's the connection between this thread and the poll you've started?

    And also please keep in mind that not to post the same thread multiple times.

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

    Default

    If I had to vote I'd vote 'very bad' for the following reasons:

    1) unformatted code, it is hardly readable;
    2) a gui that also handles a database connection;
    3) an unclear question.

    ... but I don't have to vote ...

    kind regards,

    Jos

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

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

    Default

    Quote Originally Posted by Eranga View Post
    That's a very good point Jos. I'll keep eye on this to check what's going on.
    I don't think very much is going on here; it's just bad code (for reasong I mentioned in my previous reply) but the OP thought it necessary that we'd all vote. I didn't vote for or against it ...

    kind regards,

    Jos

Similar Threads

  1. Replies: 2
    Last Post: 06-30-2010, 02:00 PM
  2. Replies: 0
    Last Post: 07-02-2009, 10:42 AM
  3. Help me this error in JDBC-ODBC connection
    By lordelf in forum Java Servlet
    Replies: 3
    Last Post: 04-04-2009, 08:37 AM
  4. Replies: 1
    Last Post: 08-06-2007, 09:24 PM

Posting Permissions

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