2 Replies - 5817 Views - Last Post: 12 July 2011 - 03:40 PM Rate Topic: -----

#1 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

java netbeans how to create methods

Posted 12 July 2011 - 07:28 AM

please help me again friends, sir, ma'am
i really want to learn java with your help

this is my code

Connect class

import java.sql.*;
import javax.swing.*;
public class Connect {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
public static Connection ConnectDB(){     // to return a Connection object
    try{
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection conn = DriverManager.getConnection ("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Database1.mdb");
     //JOptionPane.showMessageDialog(null, "Connected");
     return conn;     // <--------------
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
        return null;
    }
}
}


this code shows the data from access database to jtable using netbeans IDE
this is similar to formload in vb programming
 private void formWindowOpened(java.awt.event.WindowEvent evt) {
        // TODO add your handling code here:
        conn = Connect.ConnectDB();
            String sql =("select * from table1") ;
             try {

               pst = conn.prepareStatement(sql);
               rs = pst.executeQuery();
            jTable1.setModel(DbUtils.resultSetToTableModel(rs));
            }
            catch (Exception ex) {
            System.out.print(ex);
            }
    }


but i need to retype the code again after the message box update or delete. so how to create a method so that i will just call the method of showing all the data. i have tried some code but doesn't show me error or data


but if i use jtable events property change the error is

 private void jTable1PropertyChange(java.beans.PropertyChangeEvent evt) {
        // TODO add your handling code here:
        conn = Connect.ConnectDB();
            String sql =("select * from table1") ;
             try {

               pst = conn.prepareStatement(sql);
               rs = pst.executeQuery();
            jTable1.setModel(DbUtils.resultSetToTableModel(rs));
            }
            catch (Exception ex) {
            System.out.print(ex);
            }
         
    }


1. java.sql.SQLException:[Microsoft][ODBC Microsoft Access Driver] Too many Client tasks.

2. java.lang.NullPointerException

Is This A Good Question/Topic? 0
  • +

Replies To: java netbeans how to create methods

#2 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: java netbeans how to create methods

Posted 12 July 2011 - 10:28 AM

it's not so much a problem with netbeans that you are having a problem with. What I would do is in your file that you have

private void formWindowOpened(java.awt.event.WindowEvent evt) {
    UpdateJTable();
}
private void UpdateJTable(){
       // TODO add your handling code here:
       conn = Connect.ConnectDB();
           String sql =("select * from table1") ;
            try {

              pst = conn.prepareStatement(sql);
              rs = pst.executeQuery();
           jTable1.setModel(DbUtils.resultSetToTableModel(rs));
           }
           catch (Exception ex) {
           System.out.print(ex);
           }
   }



Was This Post Helpful? 1
  • +
  • -

#3 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

Re: java netbeans how to create methods

Posted 12 July 2011 - 03:40 PM

thanks man its working. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1