user3686208
user3686208

Reputation: 45

Pass an integer from one class to one class

I have a problem with passing the integer value from one class to another class. I have create 2 MYSQL table, one is for storing student information and another one is for "counter table". The purpose of student table is to store the information and the "counter table" is to store a number of how many student we have. After that, i have created a method to get the integer value from the "counter table" and want to send to another class method to increase the value by 1 in order to insert to a new row in student table when register. For example , my student table has 5 students, thus my counter table will have the value of 5. When a student want to register a new account, he or she can only fill in the STD_NAME , STD_TEL and the NO will come from a method to get the integer value from table "counter table" and send to another method to add on by 1. So the new student will have NO 6 row in student table.

CLASS FOR COUNTER TABLE


public CounterTable(){

}

public int getCurrentNumber(Connection conn, String table_name){
    int current_no = 0;
    String QUERY = "SELECT * FROM counter_table WHERE TABLE_NAME='" + table_name + "'";
    Statement stmt = null;
    ResultSet rs = null;        
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(QUERY);
        rs.next();
        current_no = rs.getInt("TABLE_NO");
    }       
    catch(SQLException ex){
        ex.printStackTrace();
    }           
    catch( Exception ex){
        ex.printStackTrace();
    }   
    finally{
        try{
            stmt.close();
            rs.close();
        }
        catch(Exception ex){
            ex.printStackTrace();
            ex.getMessage();
        }   
    }
    return current_no;
}

public void updateCounterNumber(Connection conn, String table_name){
    int current_no = 0;
    String QUERY = "UPDATE table_no WHERE TABLE_NAME='" + table_name + "'";
    Statement stmt = null;
    ResultSet rs = null;        
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(QUERY);
        rs.next();
    }       
    catch(SQLException ex){
        ex.printStackTrace();
    }           
    catch( Exception ex){
        ex.printStackTrace();
    }   
    finally{
        try{
            stmt.close();
            rs.close();
        }
        catch(Exception ex){
            ex.printStackTrace();
            ex.getMessage();
        }   
    }

}

}

CLASS FOR STUDENT REGISTER




String std_name = "";
String std_icno = "";
String std_mobile = "";
String std_tel = "";
String std_address = "";
String std_gender = "";
String no = "";
String table_name = "";


public void init(ServletConfig config) throws ServletException {
    super.init(config);
}


public void InsertData(){
    Connection conn = new Database().getConnection();
     PreparedStatement pstmt = null;
     //ResultSet rs = null;      
     try {
          String student ="";
          conn = new Database().getConnection();
          String query = "INSERT INTO student(NO, STD_NAME, STD_ICNO, STD_MOBILE, STD_TEL, STD_ADDRESS, STD_GENDER) values(?,?,?,?,?,?,?)";
          SetNewNumberPlusOne(conn, student, no);
          pstmt = conn.prepareStatement(query); 
          pstmt.setString(1, no);
          pstmt.setString(2, std_name); 
          pstmt.setString(3, std_icno); 
          pstmt.setString(4, std_mobile);
          pstmt.setString(5, std_tel); 
          pstmt.setString(6, std_address); 
          pstmt.setString(7, std_gender);
          pstmt.executeUpdate(); 
          updateCounterNumber(conn, student, no);
       } 

        catch( SQLException ex){
            ex.printStackTrace();
        }
        catch( Exception ex){
            ex.printStackTrace();
        }

        finally{
            try{
                pstmt.close();
                if(conn != null){
                    conn.close();
                }
            }
            catch( SQLException ex){
                ex.printStackTrace();
                ex.getMessage();
            }   
      }
}




public void SetNewNumberPlusOne(Connection conn, String student, String no){
}


private void updateCounterNumber(Connection conn, String student, String no2){

}

}

I have the problem of passing the current_no value from counter table to method of SetNewNumberPlusOne in class for student register

Upvotes: 1

Views: 108

Answers (3)

Bharat Ranpariya
Bharat Ranpariya

Reputation: 1253

If you wish to call CounterTable class method from student class, first you need to create instance of that class to student class, and then you can access methods of CounterTable class, which will return counter and you can use it.

Upvotes: 1

Paul Weiland
Paul Weiland

Reputation: 737

If your project has just 1 variable for this number of students you can create it as a public static variable and as a class variable to get access to it from any class within the namespace by typing NameOfClassThisVariableIsLocated.NameOfVariable or create a Singleton-class with this variable as a property. Advantage of Singleton-class is, that you can create just 1 instance of this class and therefore you can use this variable and be sure that you use the right value at any point in your code. If you decide using one of those 2 methods you do not need to pass the variable as a parameter because you can access it from anywhere in your namespace.

Upvotes: 1

user3982619
user3982619

Reputation:

Create a getter method in counter class. Then call that method in register class when you need number of students.

Upvotes: 1

Related Questions