├── .github
└── workflows
│ └── maven-publish.yml
├── .gitignore
├── .idea
├── encodings.xml
├── misc.xml
├── vcs.xml
└── workspace.xml
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── _no_code
└── db
│ └── Basic_SQL_Commands.md
├── pom.xml
└── src
└── main
└── java
└── org
└── aswinbenny
└── Main.java
/.github/workflows/maven-publish.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a package using Maven and then publish it to GitHub packages when a release is created
2 | # For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path
3 |
4 | name: Maven Package
5 |
6 | on:
7 | release:
8 | types: [created]
9 |
10 | jobs:
11 | build:
12 |
13 | runs-on: ubuntu-latest
14 | permissions:
15 | contents: read
16 | packages: write
17 |
18 | steps:
19 | - uses: actions/checkout@v3
20 | - name: Set up JDK 11
21 | uses: actions/setup-java@v3
22 | with:
23 | java-version: '11'
24 | distribution: 'temurin'
25 | server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
26 | settings-path: ${{ github.workspace }} # location for the settings.xml file
27 |
28 | - name: Build with Maven
29 | run: mvn -B package --file pom.xml
30 |
31 | - name: Publish to GitHub Packages Apache Maven
32 | run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml
33 | env:
34 | GITHUB_TOKEN: ${{ github.token }}
35 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 | !**/src/main/**/target/
4 | !**/src/test/**/target/
5 |
6 | ### IntelliJ IDEA ###
7 | .idea/modules.xml
8 | .idea/jarRepositories.xml
9 | .idea/compiler.xml
10 | .idea/libraries/
11 | *.iws
12 | *.iml
13 | *.ipr
14 |
15 | ### Eclipse ###
16 | .apt_generated
17 | .classpath
18 | .factorypath
19 | .project
20 | .settings
21 | .springBeans
22 | .sts4-cache
23 |
24 | ### NetBeans ###
25 | /nbproject/private/
26 | /nbbuild/
27 | /dist/
28 | /nbdist/
29 | /.nb-gradle/
30 | build/
31 | !**/src/main/**/build/
32 | !**/src/test/**/build/
33 |
34 | ### VS Code ###
35 | .vscode/
36 |
37 | ### Mac OS ###
38 | .DS_Store
39 |
40 | qodana.yaml
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | {
26 | "associatedIndex": 5
27 | }
28 |
29 |
30 |
31 |
32 |
33 |
34 | {
35 | "keyToString": {
36 | "RunOnceActivity.OpenProjectViewOnStart": "true",
37 | "RunOnceActivity.ShowReadmeOnStart": "true",
38 | "SHARE_PROJECT_CONFIGURATION_FILES": "true",
39 | "WebServerToolWindowFactoryState": "false",
40 | "git-widget-placeholder": "master",
41 | "node.js.detected.package.eslint": "true",
42 | "node.js.detected.package.tslint": "true",
43 | "node.js.selected.package.eslint": "(autodetect)",
44 | "node.js.selected.package.tslint": "(autodetect)",
45 | "project.structure.last.edited": "Modules",
46 | "project.structure.proportion": "0.0",
47 | "project.structure.side.proportion": "0.0",
48 | "vue.rearranger.settings.migration": "true"
49 | }
50 | }
51 |
52 |
53 |
54 |
55 | 1693332517631
56 |
57 |
58 | 1693332517631
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 | 1693416613491
69 |
70 |
71 |
72 | 1693416613491
73 |
74 |
75 |
76 | 1693417871193
77 |
78 |
79 |
80 | 1693417871193
81 |
82 |
83 |
84 | 1693418327396
85 |
86 |
87 |
88 | 1693418327396
89 |
90 |
91 |
92 | 1693418714827
93 |
94 |
95 |
96 | 1693418714827
97 |
98 |
99 |
100 | 1693420032010
101 |
102 |
103 |
104 | 1693420032010
105 |
106 |
107 |
108 | 1693420396537
109 |
110 |
111 |
112 | 1693420396537
113 |
114 |
115 |
116 | 1693498393273
117 |
118 |
119 |
120 | 1693498393274
121 |
122 |
123 |
124 | 1693500993880
125 |
126 |
127 |
128 | 1693500993880
129 |
130 |
131 |
132 | 1693501754925
133 |
134 |
135 |
136 | 1693501754925
137 |
138 |
139 |
140 | 1693504183571
141 |
142 |
143 |
144 | 1693504183571
145 |
146 |
147 |
148 | 1693504691402
149 |
150 |
151 |
152 | 1693504691402
153 |
154 |
155 |
156 | 1693505235636
157 |
158 |
159 |
160 | 1693505235636
161 |
162 |
163 |
164 | 1693506302169
165 |
166 |
167 |
168 | 1693506302169
169 |
170 |
171 |
172 | 1693585232308
173 |
174 |
175 |
176 | 1693585232308
177 |
178 |
179 |
180 | 1693585997169
181 |
182 |
183 |
184 | 1693585997169
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | ### Contributing to Student-Mark-Management
2 |
3 | First off, thank you for considering contributing to Student-Mark-Management. It's people like you that make Snippet Vault such a great tool.
4 | Where to start?
5 |
6 | Issues that are labeled 'help wanted' are a great place to start as they are specifically flagged as areas where help is needed. If you decide to fix an issue, please comment on the issue to let others know you are working on it.
7 |
8 | #### How to contribute?
9 |
10 | - Fork the repository on GitHub.
11 | - Clone the forked repository to your local machine.
12 | - Make your changes and commit them to your forked repository.
13 | - Submit a pull request with your changes to the main branch.
14 |
15 | Please note that the pull request should include a description of the changes and why you think they should be included.
16 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Aswin Benny
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Student Mark Management System
2 |
3 | The Student Mark Management System is a simple Java project that allows you to manage student marks using Maven and JDBC. This project provides functionality to create student classes, store their marks, edit marks, and display the mark sheet and more.
4 |
5 | ## Features
6 | - Show saved marksheets
7 | - Create a new marksheet.
8 | - Delete marksheet
9 | - Store marks for students.
10 | - Edit existing marks.
11 | - Delete a student and his marks
12 | - Display the mark sheet.
13 |
14 | ## Prerequisites
15 | - Java Development Kit (JDK) 8 or higher installed.
16 | - Maven build tool installed.
17 | - MySQL database installed and running.
18 |
19 | ## POM dependecy to be added
20 | - add the following dependency in pom.xml
21 | ```xml
22 |
23 | mysql
24 | mysql-connector-java
25 | 8.0.33
26 |
27 | ```
28 | ## MySQL Password
29 | - Either add your password in the string **password** in connectDb() function.
30 | - or add the following line in .bashrc file in home folder of your linux distro.
31 | ```bash
32 | export MYSQL_PASSKEY=""
33 | ```
34 |
35 | ## Contributing
36 | Contributions are welcome! If you find any issues or would like to enhance the functionality, feel free to create a pull request.
37 |
38 | Clone the repo by
39 | ```bash
40 | git clone https://github.com/aswinbennyofficial/Student-Mark-Management.git
41 |
42 | ```
43 |
--------------------------------------------------------------------------------
/_no_code/db/Basic_SQL_Commands.md:
--------------------------------------------------------------------------------
1 |
2 | ## INTRO
3 |
4 | - DDL - Data Defintion Language
5 | - DML - Data Manipulation Language
6 | - DQL - Data Query Language
7 |
8 | 
9 |
10 |
11 | ---
12 |
13 | ## DDL
14 |
15 | #### 1. CREATE
16 | ```SQL
17 |
18 | CREATE TABLE Student (Rollno int, Name varchar(25), Age int)
19 |
20 | ```
21 |
22 | ```SQL
23 |
24 | CREATE TABLE CopyOfStudents AS SELECT rollno, age
25 | FROM Students
26 |
27 | ```
28 |
29 |
30 | #### 2. ALTER
31 | ```SQL
32 |
33 | ALTER TABLE Student ADD age int
34 |
35 | ```
36 |
37 | ```SQL
38 |
39 | ALTER TABLE Student DROP age
40 |
41 | ```
42 |
43 | ```SQL
44 |
45 | ALTER TABLE Student RENAME age TO ages
46 |
47 | ```
48 |
49 | ```SQL
50 |
51 | ALTER TABLE Customers MODIFY age varchar(20)
52 |
53 | ```
54 |
55 |
56 | #### 3. DELETE
57 | ```SQL
58 |
59 | DROP TABLE Students
60 |
61 | -- Delete the whole table incl schema
62 | ```
63 |
64 |
65 | #### 4. TRUNCATE
66 | ```SQL
67 |
68 | TRUNCATE TABLE Students
69 |
70 | -- Delete without erasing schema
71 | ```
72 |
73 |
74 | ---
75 |
76 | ## DML
77 |
78 | #### 1. INSERT
79 | ```SQL
80 |
81 | INSERT INTO Students (Rollno,Age) VALUES (12,18)
82 |
83 | ```
84 |
85 | ```SQL
86 |
87 | INSERT INTO Student2 select Rollno,age from Student1;
88 |
89 | ```
90 |
91 |
92 | #### 2. UPDATE
93 | ```SQL
94 |
95 | UPDATE Students SET Age=13 WHERE rollno>4
96 |
97 | ```
98 |
99 | #### 3. DELETE
100 | ```SQL
101 |
102 | DELETE FROM Students WHERE Rollno>3
103 |
104 | ```
105 | ```SQL
106 |
107 | DELETE FROM Students WHERE Rollno IN (3,9,5)
108 |
109 | ```
110 |
111 | ```SQL
112 |
113 | DELETE FROM Students
114 | -- Delete all rows
115 |
116 | ```
117 |
118 |
119 | ---
120 | ## DQL
121 |
122 | #### 1. SELECT
123 | ```SQL
124 |
125 | SELECT * FROM Students
126 |
127 | ```
128 |
129 | ```SQL
130 |
131 | SELECT age,name FROM Student
132 |
133 | ```
134 |
135 | ```SQL
136 |
137 | SELECT age,name FROM Students WHERE rollno>4
138 |
139 | ```
140 |
141 | ```SQL
142 |
143 | SELECT DISTINCT * FROM Students
144 | -- outputs distinct elements only
145 |
146 | ```
147 |
148 | ```SQL
149 |
150 | SELECT Emp_id AS ID , Name FROM Company
151 | --- Gives alias ID to emp_id of table
152 |
153 | ```
154 |
155 |
156 | ### Contraints
157 | ```sql
158 |
159 | ALTER TABLE Student ADD CONSTRAINT chk_contraint CHECK(age>18) ---can drop contraints also
160 |
161 | ALTER TABLE Student DROP CONSTRAINT chk_contraint
162 |
163 | ```
164 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.aswinbenny
8 | Class_MarkSheet
9 | 1.0-SNAPSHOT
10 |
11 |
12 | 19
13 | 19
14 | UTF-8
15 |
16 |
17 |
18 |
19 | mysql
20 | mysql-connector-java
21 | 8.0.33
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/main/java/org/aswinbenny/Main.java:
--------------------------------------------------------------------------------
1 | package org.aswinbenny;
2 |
3 | import java.sql.*;
4 | import java.util.Scanner;
5 |
6 |
7 | public class Main {
8 | public static void main(String[] args) {
9 | try{
10 | // connects to the mysql database and conn is the connection object returned
11 | Connection conn=connectDb();
12 |
13 | //user defined function that displays the list of marksheet in the school
14 | showClasses(conn);
15 |
16 | Scanner sc_obj6=new Scanner(System.in);
17 | int choice; // variable to saving users choice
18 | String marksheet_name; // variable to store the name of marksheet on which CRUD functions are to be done
19 | System.out.println("\nSelect a choice:\n1. Choose an existing marksheet\n2. Create a new marksheet\n3. Quit program");
20 | choice=sc_obj6.nextInt();
21 | if(choice==1){
22 | System.out.println("Enter the name of marksheet or class: ");
23 | marksheet_name=sc_obj6.next();
24 | dashboard(conn,marksheet_name); // goes to dashboard function
25 | }
26 | else if(choice==2){
27 | createNewClass(conn); // goes to create a new marksheet/class
28 | }
29 | else{
30 | System.out.println("Wrong input. Program will now exit");
31 | conn.close();
32 | }
33 |
34 |
35 | }catch (Exception e){
36 | System.out.println("Error in main() :"+e);
37 | }
38 | }
39 |
40 | /*
41 | dashboard function is the dashboard where users get to choose to do show, delete, create marksheet or do CRUD with student details on it
42 | */
43 | public static void dashboard(Connection conn, String marksheet_name){
44 |
45 |
46 | Scanner sc_obj5=new Scanner(System.in);
47 | int choice; //variable to store choice of user
48 |
49 |
50 | System.out.println("\n\nEnter the choice");
51 | System.out.println("1. Show available Marksheets\n2. Create New Marksheet\n3. Delete marksheet\n\n4. Add Student Details\n5. Display Marksheet\n6. Edit Student Details \n7. Delete student Details\n\n8. Quit program\n");
52 | choice=sc_obj5.nextInt();
53 |
54 |
55 |
56 | if(conn!=null){
57 | if(choice==1){
58 | showClasses(conn);
59 | dashboard(conn,marksheet_name);
60 | }
61 | else if(choice==2) {
62 | createNewClass(conn);
63 | dashboard(conn,marksheet_name);
64 |
65 | }
66 | else if(choice==3) {
67 | deleteClass(conn);
68 | }
69 | else if(choice==4) {
70 | addStudentDetails(conn,marksheet_name);
71 | dashboard(conn,marksheet_name);
72 |
73 | }
74 | else if(choice==5) {
75 | displayTable(conn,marksheet_name);
76 | dashboard(conn,marksheet_name);
77 |
78 | }
79 | else if(choice==6) {
80 | editMarkSheet(conn,marksheet_name);
81 | dashboard(conn,marksheet_name);
82 |
83 | }
84 | else if(choice==7){
85 | deleteStudent(conn, marksheet_name);
86 | dashboard(conn,marksheet_name);
87 | }
88 | else if(choice==8){
89 | try {
90 | conn.close();
91 | }catch (Exception e){
92 | System.out.println("Error occured while closing connection: "+e);
93 | }
94 | }
95 | else {
96 | System.out.println(" Wrong selection; Try again\n\n\n");
97 | dashboard(conn,marksheet_name);
98 | }
99 | }
100 | }
101 |
102 | /*
103 | deleteStudent function is used to delete a student details on basis of their rollno
104 | */
105 | public static void deleteStudent(Connection conn,String marksheet_name){
106 | try{
107 | Scanner sc_obj6=new Scanner(System.in);
108 | int rollno; //variable to store roll no
109 | System.out.println("Enter the roll no of student to delete: ");
110 | rollno=sc_obj6.nextInt();
111 |
112 | String query="DELETE FROM "+marksheet_name+" WHERE Rollno=?"; //sql query to delete a student row of particular rollno
113 | PreparedStatement pstmt6= conn.prepareStatement(query);
114 | pstmt6.setInt(1,rollno);
115 |
116 | pstmt6.executeUpdate();
117 | System.out.println("Student details deleted successfully");
118 |
119 |
120 | }catch (Exception e){
121 | System.out.println("Error in deleteStudent(): "+e);
122 | }
123 | }
124 |
125 | /*
126 | editMarkSheet function is used to edit the marks and name of the student by entering their rollno
127 | */
128 | public static void editMarkSheet(Connection conn, String marksheet_name){
129 | try{
130 | String query="UPDATE "+marksheet_name+" SET Name=?, Physics=?, Maths=?, Chemistry=?, Biology=? WHERE Rollno=?";
131 |
132 | PreparedStatement pstmt3=conn.prepareStatement(query);
133 |
134 | Scanner sc_obj3=new Scanner(System.in);
135 | int rollno,physics,maths,chemistry,biology;
136 | String name;
137 | System.out.println("Enter the Rollno whose details has to be changed: ");
138 | rollno=sc_obj3.nextInt();
139 | sc_obj3.nextLine(); //eliminating newline
140 | System.out.println("Enter new name: ");
141 | name=sc_obj3.nextLine();
142 | System.out.println("Enter new physics marks: ");
143 | physics=sc_obj3.nextInt();
144 | System.out.println("Enter new maths marks: ");
145 | maths=sc_obj3.nextInt();
146 | System.out.println("Enter new chemistry marks: ");
147 | chemistry=sc_obj3.nextInt();
148 | System.out.println("Enter new biology marks: ");
149 | biology=sc_obj3.nextInt();
150 |
151 | pstmt3.setString(1,name); // 1, 2 etc are parameter index ie index of '?' in the query index starts from 1
152 | pstmt3.setInt(2,physics);
153 | pstmt3.setInt(3,maths);
154 | pstmt3.setInt(4,chemistry);
155 | pstmt3.setInt(5,biology);
156 | pstmt3.setInt(6,rollno);
157 |
158 | pstmt3.executeUpdate();
159 | System.out.println("Student details updated.");
160 |
161 | }catch (Exception e){
162 | System.out.println("Error in editMarkSheet(): "+e);
163 | }
164 | }
165 |
166 |
167 | /*
168 | deleteClass function is used to delete a marksheet or a class in the school
169 | */
170 | public static void deleteClass(Connection conn){
171 | try {
172 | Scanner sc_ob4=new Scanner(System.in);
173 | String name_of_marksheet;
174 | System.out.println("Enter the name of marksheet to delete: ");
175 | name_of_marksheet=sc_ob4.next();
176 |
177 | String query="DROP TABLE "+name_of_marksheet;
178 |
179 | PreparedStatement pstmt4= conn.prepareStatement(query);
180 | pstmt4.executeUpdate();
181 |
182 | System.out.println("Marksheet deleted successfully ");
183 | }catch (Exception e){
184 | System.out.println("Error in deleteClass: "+e);
185 | }
186 | }
187 |
188 | /*
189 | showClasses function displays all the marksheets that are available in the school
190 | */
191 | public static void showClasses(Connection conn){
192 | try{
193 | String query="SHOW TABLES";
194 | PreparedStatement pstmt3= conn.prepareStatement(query);
195 | ResultSet set=pstmt3.executeQuery(); //return value of excecuteQuery gets saved in a Resultset called set
196 |
197 | String table_names;
198 | System.out.println("Here are the list of Marksheets in our school: ");
199 | while(set.next()){
200 | table_names=set.getString(1);
201 | System.out.println(table_names);
202 | }
203 | }catch (Exception e){
204 | System.out.println("Error in showClasses(): "+e);
205 | }
206 |
207 | }
208 |
209 | /*
210 | createNewClass is used to create a new class or a new marksheet for the school
211 | */
212 | public static void createNewClass(Connection conn){
213 | try {
214 | String name_of_class;
215 |
216 | Scanner sc_ob2=new Scanner(System.in);
217 | System.out.println("Enter the name of new Class: ");
218 | name_of_class=sc_ob2.next();
219 |
220 | String query="CREATE TABLE "+name_of_class+" (Rollno int primary key auto_increment, Name varchar(35), Physics int, Maths int, Chemistry int, Biology int)";
221 |
222 | PreparedStatement pstmt2=conn.prepareStatement(query);
223 | pstmt2.executeUpdate();
224 | System.out.println("Class "+name_of_class+" added successfully");
225 | dashboard(conn,name_of_class);
226 |
227 | }catch (Exception e){
228 | System.out.println("Error in createNewClass(): "+e);
229 | }
230 |
231 | }
232 |
233 |
234 | /*
235 | addStudentDetails is used to add the new student details to the marksheet
236 | */
237 | public static void addStudentDetails(Connection conn, String marksheet_name){
238 | try{
239 | String query=" INSERT INTO "+marksheet_name+" (Name,Physics,Maths,Chemistry,Biology) VALUES(?,?,?,?,?)";
240 | PreparedStatement pstmt=conn.prepareStatement(query);
241 |
242 | Scanner sc_ob1=new Scanner(System.in);
243 | //initialising variables
244 | String name;
245 | int rollno,physics,maths,chemistry,biology,no_of_students;
246 |
247 | System.out.println("Enter the number of students to add: ");
248 | no_of_students=sc_ob1.nextInt();
249 |
250 | while (no_of_students > 0) {
251 | //taking input from user
252 | //
253 | sc_ob1.nextLine(); // consumes newline character
254 | System.out.println("Enter student name: ");
255 | name = sc_ob1.nextLine();
256 | System.out.println("Enter marks of Physics: ");
257 | physics = sc_ob1.nextInt();
258 | System.out.println("Enter marks of Maths: ");
259 | maths = sc_ob1.nextInt();
260 | System.out.println("Enter marks of Chemisitry: ");
261 | chemistry = sc_ob1.nextInt();
262 | System.out.println("Enter marks of Biology: ");
263 | biology = sc_ob1.nextInt();
264 | System.out.println();//adding an extra line for looks
265 |
266 | // pstmt.setInt(1, rollno);
267 | pstmt.setString(1, name);
268 | pstmt.setInt(2, physics);
269 | pstmt.setInt(3, maths);
270 | pstmt.setInt(4, chemistry);
271 | pstmt.setInt(5, biology);
272 |
273 | pstmt.executeUpdate();
274 |
275 | no_of_students--;
276 | }
277 | System.out.println("Student details added successfully. ");
278 |
279 | }catch(Exception e){
280 | System.out.println("Error occured at addStudentDetails: "+e);
281 | }
282 |
283 | }
284 |
285 |
286 | /*
287 | displayTable will display the marksheet containing rollno, name and subject marks
288 | */
289 | public static void displayTable(Connection conn, String marksheet_name){
290 | try{
291 | String query="SELECT * FROM "+marksheet_name; //query to select all from table
292 | PreparedStatement pstmt=conn.prepareStatement(query);
293 | ResultSet set= pstmt.executeQuery(); //stores the out in a Resultset called set
294 |
295 |
296 | System.out.println("RollNo Name Physics Chemistry Maths Biology");
297 | while (set.next()) {
298 | int id = set.getInt(1); // 1 is column index
299 | String name = set.getString(2);
300 | int physics = set.getInt(3);
301 | int maths = set.getInt(4);
302 | int chemistry = set.getInt(5);
303 | int biology = set.getInt(6);
304 |
305 | String formattedId = String.format("%-9d", id);
306 | String formattedName = String.format("%-15s", name);
307 | String formattedPhysics = String.format("%-10d", physics);
308 | String formattedChemistry = String.format("%-12d", chemistry);
309 | String formattedMaths = String.format("%-8d", maths);
310 | String formattedBiology = String.format("%-8d", biology);
311 |
312 | System.out.println(formattedId + formattedName + formattedPhysics + formattedChemistry + formattedMaths + formattedBiology);
313 | }
314 |
315 |
316 | }catch (Exception e){
317 | System.out.println("Error occured in displayTable(): "+e);
318 | }
319 |
320 |
321 | }
322 |
323 |
324 | /*
325 | connectDB is used to establish connection with mysql database
326 | */
327 | public static Connection connectDb(){
328 | try{
329 |
330 | String dbUrl="jdbc:mysql://localhost:3306/school"; //db selected is school
331 | String driver="com.mysql.cj.jdbc.Driver";
332 | String userName="root"; //username of db
333 | String password = System.getenv("MYSQL_PASSKEY"); //password of db taken from environment variable MYSQL_PASSKEY, replace string with your mysql password
334 | //export MYSQL_PASSKEY="" insert this line in .bashrc file in home of linux distro to save environment variable
335 |
336 | //load the mysql jbdc driver to memory
337 | Class.forName(driver);
338 |
339 | Connection conn=DriverManager.getConnection(dbUrl,userName,password);
340 |
341 | if(conn.isClosed()){
342 | System.out.println("Connection is closed");
343 | }else{
344 | //System.out.println("Database connected");
345 | return conn;
346 | }
347 |
348 |
349 | }catch (Exception e){
350 | System.out.println("Error occured: "+e);
351 | }
352 | return null;
353 | }
354 | }
355 |
--------------------------------------------------------------------------------