Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.98 KB | None | 0 0
  1. import java.util.*;
  2. import java.sql.*;
  3. public class MarkEntry {
  4. public static Connection con;
  5. public static Statement stmt;
  6. public static ResultSet rs;
  7. public static String Username;
  8. public static String Password;
  9. public static String Role;
  10. public static String Name;
  11. public static String Grade;
  12. public static String Section;
  13. public static String Subject;
  14. public static boolean temp = false;
  15. public static int reportChoice;
  16. public static String studentChoice;
  17. public static int studentRollNo;
  18.  
  19. public static Scanner s = new Scanner(System.in);
  20.  
  21. public static void main(String args[]){
  22. //Connextion
  23. try{
  24. Class.forName("com.mysql.jdbc.Driver");
  25. con=DriverManager.getConnection("jdbc:mysql://localhost/harssha_gowtham","root","");
  26. stmt=con.createStatement();
  27. Login();
  28. // ResultSet rs=stmt.executeQuery("select * from student_data");
  29. // while(rs.next())
  30. // System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3) + " " + rs.getString(4));
  31. // con.close();
  32. }catch(Exception e)
  33. {
  34. System.out.println(e);
  35. }
  36. }
  37.  
  38. public static void Login(){
  39. //Lagin
  40. try {
  41. System.out.println("Welcome to the Mark Entry System ");
  42. System.out.println("Username: ");
  43. Username = s.nextLine();
  44. System.out.println("Password: ");
  45. Password = s.nextLine();
  46. //Fetching the name with the user name.
  47. rs = stmt.executeQuery("select Name from teachers_login where Username = '" + Username + "'" + "AND Password = '" + Password +"'");
  48. if(rs.next()){
  49. Name = rs.getString(1);
  50. }
  51. else{
  52. //Incorrect Login
  53. System.out.println("Incorrect Username or Password");
  54. Login();
  55. }
  56. rs.beforeFirst(); //reinitializing result set after looping through the table
  57. System.out.println();
  58. //Displaying all possible roles with class and section and letting user choose one
  59. System.out.println("Possible Roles:");
  60. System.out.println();
  61. System.out.println("Role Grade Section");
  62. rs = stmt.executeQuery("select Role, Class, Section from teachers_roles WHERE Name= '" + Name +"'");
  63. while(rs.next()){
  64. System.out.println(rs.getString(1) + " " + rs.getInt(2) + " " + rs.getString(3)) ;
  65. }
  66. //Asking for a role
  67. System.out.println("Login As?");
  68. Role = s.nextLine();
  69. rs.beforeFirst();
  70. if((Role.equals("ST") || Role.equals("HOD")) && ((checkRole() == true))){
  71. //Ask for the class and the section as there can be many
  72. System.out.print("Grade: ");
  73. Grade = s.nextLine();
  74. System.out.println();
  75. System.out.print("Section:");
  76. Section = s.nextLine();
  77. //Have to insert checkGrade()
  78. //Get subject Automatically
  79. rs = stmt.executeQuery("SELECT Subject FROM teachers_roles WHERE teachers_roles.Class = '" + Grade + "' AND teachers_roles.Section = '" + Section +"' AND teachers_roles.Name = '" + Name +"'");
  80. if(rs.next())
  81. Subject = rs.getString(1);
  82. }
  83. else if(Role.equals("CT") && ((checkRole() == true)))
  84. {
  85. //Ask for the class and the section as there can be many
  86. rs = stmt.executeQuery("SELECT class , Section from teachers_roles WHERE Name = '" + Name + "' AND Role = '" + Role + "'");
  87. while(rs.next()){
  88. Grade = rs.getString(1);
  89. Section = rs.getString(2);
  90. }
  91. //Debug info
  92. System.out.println(Grade);
  93. System.out.println(Section);
  94. System.out.println("Choose the Student:");
  95. studentChoice = s.nextLine();
  96. rs = stmt.executeQuery("select student_data.Roll_No FROM student_data WHERE student_data.Name ='"+studentChoice+"'");
  97. studentRollNo = rs.getInt(1);
  98. perStudent();
  99. }
  100. else if(Role.equals("COD") && ((checkRole() == true)))
  101. {
  102. //There is nothing for a COD
  103.  
  104. }
  105. else
  106. {
  107. System.out.println("Error.");
  108. Login();
  109. }
  110.  
  111.  
  112. //Plug in incorrect role code when done
  113.  
  114. //Asking for the type of Report
  115. System.out.println("Choose Reports");
  116. if(Role.equals("ST")){
  117. System.out.println("1) Per Subject Report : Students Vs. Assesment Types ");
  118. reportChoice = s.nextInt();
  119. }
  120. else if(Role.equals("CT")){
  121. System.out.println("1) Per Student Report : Subject Vs. Assesment Types ");
  122. System.out.println("2) All Student Report : Students Vs. Assesment Types ");
  123. reportChoice = s.nextInt();
  124. }
  125. else if(Role.equals("HOD")){
  126. System.out.println("1) Per Class Report : Students Vs. Assessment Types ");
  127. reportChoice = s.nextInt();
  128. }
  129. else if(Role.equals("COD"))
  130. {
  131. System.out.println("1) Per Student Report : Subject Vs. Assesment Types ");
  132. System.out.println("2) All Student Report : Students Vs. Assesment Types ");
  133. System.out.println("3) Per Class Report : Students Vs. Assessment Types ");
  134. System.out.println("4) Per Subject Report : Students Vs. Assesment Types ");
  135. reportChoice = s.nextInt();
  136. }
  137. else
  138. {
  139. System.out.println("Error");
  140.  
  141. }
  142.  
  143. //Corresponding function calls
  144. if(reportChoice == 1 ) {
  145. if(Role.equals("ST"))
  146. perSubject();
  147. else if(Role.equals("CT"))
  148. perStudent();
  149. else if(Role.equals("HOD"))
  150. perClass();
  151. else if(Role.equals("COD"))
  152. perStudent();
  153. }
  154. else if(reportChoice == 2){
  155. if(Role.equals("CT"))
  156. allStudent();
  157. else if(Role.equals("COD"))
  158. allStudent();
  159. }
  160. else if(reportChoice == 3){
  161. if(Role.equals("COD"))
  162. perClass();
  163. }
  164. else if(reportChoice == 4){
  165. if(Role.equals("COD"))
  166. perSubject();
  167. }
  168. else {
  169. System.out.println("Error.");
  170. Login();
  171. }
  172.  
  173. }
  174.  
  175. catch(Exception e)
  176. {
  177. System.out.println(e);
  178. }
  179.  
  180. }
  181.  
  182. public static boolean checkRole() {
  183. //To check if its a valid role or not
  184. try
  185. {
  186. while(rs.next()){
  187. if(Role.equals(rs.getString(1)))
  188. temp = true;
  189. }
  190. }
  191. catch(Exception e)
  192. {
  193. System.out.println(e);
  194. }
  195. finally
  196. {
  197. return temp;
  198. }
  199. }
  200.  
  201.  
  202.  
  203. public static void perSubject() {
  204. try{
  205. System.out.println("Generating Per Subject report...");
  206. System.out.println("Subject Chosen: " + Subject);
  207. 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");
  208.  
  209. WriteToFile.Write(reportChoice,Role); /Here is the error
  210. }
  211. catch(Exception e)
  212. {
  213. System.out.println(e);
  214.  
  215. }
  216. }
  217.  
  218. public static void perStudent() {
  219. System.out.println("Generating Per Student Report...");
  220. System.out.println("Student Chosen:" + studentChoice);
  221.  
  222.  
  223.  
  224. }
  225.  
  226. public static void perClass() {
  227. System.out.println("Per Class");
  228.  
  229.  
  230. }
  231.  
  232. public static void allStudent() {
  233. System.out.println("all stud");
  234.  
  235.  
  236.  
  237. }
  238. }
  239.  
  240. import java.io.BufferedWriter;
  241. import java.io.FileWriter;
  242. import java.io.IOException;
  243. import java.sql.*;
  244.  
  245. public class WriteToFile extends MarkEntry {
  246.  
  247. public static final String FILENAME = "Report.html";
  248. public static BufferedWriter bw = null;
  249. public static FileWriter fw = null;
  250. public static void Write(int n, String role) {
  251. try {
  252. fw = new FileWriter(FILENAME);
  253. bw = new BufferedWriter(fw);
  254. if(n==1 && role.equals("ST")){
  255.  
  256. 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>");
  257.  
  258. while(rs.next()){
  259. bw.write("<tr>");
  260. bw.write("<td>"+rs.getString(1)+"</td>");
  261. bw.write("<td>"+rs.getInt(2)+"</td>");
  262. bw.write("<td>"+rs.getInt(3)+"</td>");
  263. bw.write("<td>"+rs.getInt(4)+"</td>");
  264. bw.write("<td>"+rs.getInt(5)+"</td>");
  265. bw.write("<td>"+rs.getInt(6)+"</td>");
  266. bw.write("</tr>");
  267.  
  268. }
  269. //Getting average and Stddev seperately and displaying it through html
  270. rs = stmt.executeQuery("select AVG("+Subject+".UT1) , AVG("+Subject+".UT2) , AVG("+Subject+".PRJ) , AVG("+Subject+".TE) , AVG("+Subject+".Final) from "+Subject+";");
  271. bw.write(" <tr><td> <b>Average</b> </td>");
  272.  
  273. while(rs.next()){
  274.  
  275. bw.write("<td><b>"+rs.getInt(1)+"</b></td>");
  276. bw.write("<td><b>"+rs.getInt(2)+"</b></td>");
  277. bw.write("<td><b>"+rs.getInt(3)+"</b></td>");
  278. bw.write("<td><b>"+rs.getInt(4)+"</b></td>");
  279. bw.write("<td><b>"+rs.getInt(5)+"</b></td>");
  280.  
  281. }
  282.  
  283. bw.write("</tr>");
  284. rs = stmt.executeQuery("select STDDEV("+Subject+".UT1),STDDEV("+Subject+".UT2),STDDEV("+Subject+".PRJ),STDDEV("+Subject+".TE),STDDEV("+Subject+".Final) from "+Subject+" ");
  285. bw.write("<tr><td> <b>Standard Deviation</b> </td>");
  286.  
  287. while(rs.next()){
  288.  
  289. bw.write("<td><b>"+rs.getInt(1)+"</b></td>");
  290. bw.write("<td><b>"+rs.getInt(2)+"</b></td>");
  291. bw.write("<td><b>"+rs.getInt(3)+"</b></td>");
  292. bw.write("<td><b>"+rs.getInt(4)+"</b></td>");
  293. bw.write("<td><b>"+rs.getInt(5)+"</b></td>");
  294. }
  295. bw.write("</tr>");
  296.  
  297. bw.write("</table> </BODY> </HTML>");
  298. System.out.println("Done");
  299. }
  300. else if(n==1&& Role.equals("CT")){
  301. bw.write("<!DOCTYPE html><html><head><title>Per Student</title><meta charset="utf"/></head><body> Student Chosen:"+studentChoice);
  302. 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>");
  303. //displaying marks for inividual subjects
  304. //fetching data from sql
  305. //Subject Wise:
  306. //English
  307. bw.write("<tr> <td><b>English></b></td>");
  308. rs=stmt.executeQuery("select UT1 , UT2, PRJ, TE, Final from english where Roll_No ="+studentRollNo);
  309. while(rs.next()){
  310. bw.write("<td><b>"+rs.getInt(1)+"</b></td>");
  311. bw.write("<td><b>"+rs.getInt(2)+"</b></td>");
  312. bw.write("<td><b>"+rs.getInt(3)+"</b></td>");
  313. bw.write("<td><b>"+rs.getInt(4)+"</b></td>");
  314. bw.write("<td><b>"+rs.getInt(5)+"</b></td>");
  315.  
  316. }
  317. bw.write("/tr");
  318.  
  319. bw.write("</body></html>");
  320.  
  321. }
  322. } catch (IOException e) {
  323.  
  324. e.printStackTrace();
  325.  
  326. }
  327. catch (SQLException e){
  328. System.out.println(e);
  329.  
  330. }
  331. finally {
  332.  
  333. try {
  334.  
  335. if (bw != null)
  336. bw.close();
  337.  
  338. if (fw != null)
  339. fw.close();
  340.  
  341. } catch (IOException ex) {
  342.  
  343. ex.printStackTrace();
  344.  
  345. }
  346.  
  347.  
  348.  
  349. }
  350.  
  351. }
  352.  
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement