jeudi 20 avril 2017

Not able to access one class's function from another class in java

I am currently working on a program which display's student's marks in a formatted way with the help of Java,SQL,and HTML.
The aim of the program is to display reports according to the person logged in. My code consists of 2 classes: MarkEntry and WriteToFile. MarkEntry is the java program which gets all the input like the name of the teacher,username,password and the type of report to be generated and then passes this info to the other class: WriteToFile. This WriteToFile then writes a html report and tabulates the data and displays it in a nice way.

I am currently stuck on a JAVA part. I am currently trying to access the method Write in WriteToFile. It compiles with no errors in Bluej. But it doesn't do so in cmd. This is what it says in cmd: 1The error in cmd. Thanks in advance for the help and sorry for posting so much code. :) And for reference purposes, here is the code(both the classes):

    import java.util.*;
import java.sql.*;  
public class MarkEntry {  
    public static Connection con;
    public static Statement stmt;
    public static ResultSet rs;
    public static String Username;
    public static String Password;
    public static String Role;
    public static String Name;
    public static String Grade;
    public static String Section;
    public static String Subject;
    public static boolean temp = false;
    public static int reportChoice;
    public static String studentChoice;
    public static int studentRollNo;

     public static Scanner s = new Scanner(System.in);

     public static void main(String args[]){  
         //Connextion
    try{  
         Class.forName("com.mysql.jdbc.Driver"); 
         con=DriverManager.getConnection("jdbc:mysql://localhost/harssha_gowtham","root","");  
         stmt=con.createStatement();  
         Login();
             //         ResultSet rs=stmt.executeQuery("select * from student_data");  
             //         while(rs.next())  
             //         System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getInt(3) + " " + rs.getString(4));  
         //         con.close();  
    }catch(Exception e)
    {
        System.out.println(e);
    }  
    }  

    public static void Login(){
        //Lagin
       try {
        System.out.println("Welcome to the Mark Entry System ");
        System.out.println("Username: ");
        Username = s.nextLine();
        System.out.println("Password: ");
        Password = s.nextLine();
        //Fetching the name with the user name.
        rs = stmt.executeQuery("select Name from teachers_login where Username = '" + Username + "'" + "AND Password = '" + Password +"'");
        if(rs.next()){
            Name = rs.getString(1);
        }
        else{
            //Incorrect Login
            System.out.println("Incorrect Username or Password");
            Login();
        }
        rs.beforeFirst(); //reinitializing result set after looping through the table
        System.out.println();
        //Displaying all possible roles with class and section and letting user choose one 
        System.out.println("Possible Roles:");
        System.out.println();
        System.out.println("Role    Grade    Section");
        rs = stmt.executeQuery("select Role, Class, Section from teachers_roles WHERE Name= '" + Name +"'");
        while(rs.next()){
            System.out.println(rs.getString(1) + "      " + rs.getInt(2) + "        " + rs.getString(3)) ;
        }
        //Asking for a role
        System.out.println("Login As?");
        Role = s.nextLine();
        rs.beforeFirst();
        if((Role.equals("ST") || Role.equals("HOD")) && ((checkRole() == true))){
            //Ask for the class and the section as there can be many
            System.out.print("Grade:   ");
            Grade = s.nextLine();
            System.out.println();            
            System.out.print("Section:");
            Section = s.nextLine();
            //Have to insert checkGrade()
            //Get subject Automatically
            rs = stmt.executeQuery("SELECT Subject FROM teachers_roles WHERE teachers_roles.Class = '" + Grade + "' AND teachers_roles.Section = '" + Section +"' AND teachers_roles.Name = '" + Name +"'");
            if(rs.next())
                Subject = rs.getString(1);
        }
        else if(Role.equals("CT") && ((checkRole() == true)))
        {
            //Ask for the class and the section as there can be many
            rs = stmt.executeQuery("SELECT class , Section from teachers_roles WHERE Name = '" + Name + "' AND Role = '" + Role + "'");
            while(rs.next()){
                Grade = rs.getString(1);
                Section = rs.getString(2);               
            }
            //Debug info
            System.out.println(Grade);
            System.out.println(Section);
            System.out.println("Choose the Student:");
            studentChoice = s.nextLine();
            rs = stmt.executeQuery("select student_data.Roll_No FROM student_data WHERE student_data.Name ='"+studentChoice+"'");
            studentRollNo = rs.getInt(1);
            perStudent();
        }
        else if(Role.equals("COD") && ((checkRole() == true)))
        {
            //There is nothing for a COD

        }
        else
        {
            System.out.println("Error.");
            Login();
        }


        //Plug in incorrect role code when done

        //Asking for the type of Report
        System.out.println("Choose Reports");
        if(Role.equals("ST")){
            System.out.println("1) Per Subject Report :  Students Vs. Assesment Types ");
            reportChoice = s.nextInt();
        }
        else if(Role.equals("CT")){
            System.out.println("1) Per Student Report :  Subject Vs. Assesment Types ");
            System.out.println("2) All Student Report :  Students Vs. Assesment Types ");
            reportChoice = s.nextInt();
        }
        else if(Role.equals("HOD")){
            System.out.println("1) Per Class Report : Students Vs. Assessment Types ");
            reportChoice = s.nextInt();
        }
        else if(Role.equals("COD"))
        {
            System.out.println("1) Per Student Report :    Subject Vs. Assesment Types ");
            System.out.println("2) All Student Report :    Students Vs. Assesment Types ");
            System.out.println("3) Per Class Report   :    Students Vs. Assessment Types ");
            System.out.println("4) Per Subject Report :    Students Vs. Assesment Types ");
            reportChoice = s.nextInt();
        }
        else
        {
            System.out.println("Error");

        }

        //Corresponding function calls
        if(reportChoice == 1 ) {
            if(Role.equals("ST"))
                perSubject();
            else if(Role.equals("CT"))
                perStudent();
            else if(Role.equals("HOD"))
                perClass();
            else if(Role.equals("COD"))
                perStudent();
            }
            else if(reportChoice == 2){
            if(Role.equals("CT"))
                allStudent();
            else if(Role.equals("COD"))
                allStudent();
            }
            else if(reportChoice == 3){
            if(Role.equals("COD"))
                perClass();
            }
        else if(reportChoice == 4){
            if(Role.equals("COD"))
                perSubject();
            }
            else {
                System.out.println("Error.");
                Login();
            }

    }

        catch(Exception e)
    {
        System.out.println(e);
    }  

    }

    public static boolean checkRole()  {
     //To check if its a valid role or not
    try
    {        
        while(rs.next()){
            if(Role.equals(rs.getString(1)))
                temp =  true;
            }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
    finally 
    {
        return temp;
    }
    }   



    public static void perSubject() {
        try{
        System.out.println("Generating Per Subject report...");
        System.out.println("Subject Chosen: " + Subject);
        rs = stmt.executeQuery("SELECT student_data.Name , "+Subject+".UT1 , "+Subject+".UT2 , "+Subject+".PRJ , "+Subject+".TE ,"+Subject+".Final FROM "+Subject+" INNER JOIN student_data ON "+Subject+".Roll_No = student_data.Roll_No AND student_data.Class ="+Grade+" AND student_data.Section = '"+Section+"' GROUP BY student_data.Name");        

        WriteToFile.Write(reportChoice,Role); /Here is the error
    }
    catch(Exception e)
    {
        System.out.println(e);

    }
    }

    public static void perStudent() {
               System.out.println("Generating Per Student Report...");
               System.out.println("Student Chosen:" + studentChoice);



    }

    public static void perClass() {
                System.out.println("Per Class");


    }

    public static void allStudent() {
                System.out.println("all stud");



    }
         }

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;

public class WriteToFile extends MarkEntry {

public static final String FILENAME = "Report.html";
public static  BufferedWriter bw = null;
public static FileWriter fw = null;
  public static void Write(int n, String role) {
    try {
         fw = new FileWriter(FILENAME);
         bw = new BufferedWriter(fw);
        if(n==1 && role.equals("ST")){

            bw.write("<!DOCTYPE html><HTML> <HEAD>  <meta charset=\"utf-8\"/> Report 1: Subject Teacher Report  </HEAD> <style> table, th, td {    border: 1px solid black; }</style> <h1 > Subject: " + Subject+ " </h1> <h2 > Class:"+Grade+ "  Section:"+ Section+" </h2> <BODY> <table style=width:50%>  <tr>    <th>Name</th>    <th>UT1</th>     <th>UT2</th>      <th>PRJ</th>        <th>TE</th> <th>Final</th>   </tr>");

        while(rs.next()){
                bw.write("<tr>");
                bw.write("<td>"+rs.getString(1)+"</td>");
                bw.write("<td>"+rs.getInt(2)+"</td>"); 
                bw.write("<td>"+rs.getInt(3)+"</td>"); 
                bw.write("<td>"+rs.getInt(4)+"</td>"); 
                bw.write("<td>"+rs.getInt(5)+"</td>"); 
                bw.write("<td>"+rs.getInt(6)+"</td>"); 
                bw.write("</tr>");

        }
            //Getting average and Stddev seperately and displaying it through html
        rs = stmt.executeQuery("select AVG("+Subject+".UT1) , AVG("+Subject+".UT2) , AVG("+Subject+".PRJ) , AVG("+Subject+".TE) , AVG("+Subject+".Final) from "+Subject+";");
        bw.write(" <tr><td> <b>Average</b> </td>"); 

             while(rs.next()){

            bw.write("<td><b>"+rs.getInt(1)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(2)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(3)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(4)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(5)+"</b></td>");

        }

        bw.write("</tr>");
        rs = stmt.executeQuery("select STDDEV("+Subject+".UT1),STDDEV("+Subject+".UT2),STDDEV("+Subject+".PRJ),STDDEV("+Subject+".TE),STDDEV("+Subject+".Final) from "+Subject+" ");
        bw.write("<tr><td> <b>Standard Deviation</b> </td>"); 

             while(rs.next()){

            bw.write("<td><b>"+rs.getInt(1)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(2)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(3)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(4)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(5)+"</b></td>");
        }
                 bw.write("</tr>");   

                bw.write("</table> </BODY> </HTML>");  
            System.out.println("Done");
    }
    else if(n==1&& Role.equals("CT")){
        bw.write("<!DOCTYPE html><html><head><title>Per Student</title><meta charset=\"utf\"/></head><body> Student Chosen:"+studentChoice);
        bw.write("<table style=width:50%>  <tr>    <th>Subject</th>    <th>UT1</th>     <th>UT2</th>      <th>PRJ</th>        <th>TE</th> <th>Final</th>   </tr>");
        //displaying marks for inividual subjects
        //fetching data from sql
        //Subject Wise:
        //English
        bw.write("<tr> <td><b>English></b></td>");
        rs=stmt.executeQuery("select UT1 , UT2, PRJ, TE, Final from english where Roll_No ="+studentRollNo);
        while(rs.next()){
            bw.write("<td><b>"+rs.getInt(1)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(2)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(3)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(4)+"</b></td>");
            bw.write("<td><b>"+rs.getInt(5)+"</b></td>");

        }
        bw.write("/tr");

        bw.write("</body></html>");

    }
        } catch (IOException e) {

            e.printStackTrace();

        }
        catch (SQLException e){
                System.out.println(e);

            }
       finally {

            try {

                if (bw != null)
                    bw.close();

                if (fw != null)
                    fw.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }



        }

    }

}






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire