You are on page 1of 57

First class

/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package finalyearproject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.swing.JOptionPane;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.sql.Statement;
/**
*
* @author Prof Oyondi
*/
public class FinalYearProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
Splash spl = new Splash();
spl.setVisible(true);
for(int i = 0; i <= 100; i++){
//set the sleep time of the loader
Thread.sleep(45);
spl.loadingNum.setText("Loading..."+Integer.toString(i) +"
%");
spl.jProgressBarload.setValue(i);
//open grading panel if loading in the progress bar is over
if(i == 100){

JOptionPane.showMessageDialog(null, "Welcome");
MainApp app = new MainApp();
app.setVisible(true);
spl.setVisible(false);
}else if(i == 45){
spl.guides_add.setText("Please wait while setting things in
order");
Connection conn = null;
try {
String dbName = "studentsdb";
String userName = "root";
String password = "";

String url =
"jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertTo
Null";
Connection connection =
DriverManager.getConnection(url,userName, password);

String sql = "CREATE DATABASE IF NOT EXISTS " + dbName;

Statement statement = connection.createStatement();


statement.executeUpdate(sql);
statement.close();
spl.guides_add.setText("Database created successfully");
// JOptionPane.showMessageDialog(null, databaseName + "
Database has been created successfully", "System Message",
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
e.printStackTrace();
}
}else if(i == 90){
try {
// create table
String que = "CREATE TABLE IF NOT EXISTS students( \n" +
" id INT NOT NULL AUTO_INCREMENT, \n" +
" username VARCHAR(32),\n" +
" HomeWork INT(64), \n" +
" TEST INT(64), \n" +
" total INT(64), \n" +
" grade varchar(32), \n" +
" PRIMARY KEY (`id`))";
Connection conntable =
DriverManager.getConnection("jdbc:mysql://localhost:3306/studentsd
b", "root", "");
Statement Stmt = conntable.createStatement();
Stmt.executeUpdate(que);
Stmt.close();
//JOptionPane.showMessageDialog(null, " Table has been
created successfully");
} catch (Exception e) {
e.printStackTrace();
}
spl.guides_add.setText("All tables created.");
}else if(i == 99){
spl.guides_add.setText("Enjoy out service! Thanks!!");
}
}
}catch (Exception e){

}
}

}
Second class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package finalyearproject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
*
* @author Prof Oyondi
*/
public class GradesTable extends javax.swing.JFrame {

/**
* Creates new form GradesTable
*/
public GradesTable() {
initComponents();
show_Student_Details();
}
public Connection createConnection()
{

Connection conn = null;


//try to connect to the studentsdb
try{

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/studentsdb";
String user = "root";
String password = "";
conn = DriverManager.getConnection(url,user ,password);

return conn;
}catch(Exception e){
//print error message if not connected
e.printStackTrace();
return null;
}
}

public ArrayList<StudentInfo> getstudentsList()


{
ArrayList<StudentInfo> sList = new ArrayList<StudentInfo>();
Connection connect = createConnection();
String query = "SELECT * FROM `students`";
Statement stmt;
ResultSet res;
try{
stmt = connect.createStatement();
res = stmt.executeQuery(query);
StudentInfo stud;
while(res.next())
{
stud = new
StudentInfo(res.getInt("id"),res.getString("username"),res.getInt("Hom
eWork"),res.getInt("TEST"),res.getInt("total"),res.getString("grade"));
sList.add(stud);
}
}catch(Exception ex){
ex.printStackTrace();
}
return sList;
}
//show student details on the jTable
public void show_Student_Details()
{
ArrayList<StudentInfo> list = getstudentsList();
DefaultTableModel model =
(DefaultTableModel)students_table.getModel();
Object[] data = new Object[6];
for(int i = 0; i < list.size(); i++)
{
data[0] = list.get(i).getSID();
data[1] = list.get(i).getSName();
data[2] = list.get(i).getHScore();
data[3] = list.get(i).getSTest();
data[4] = list.get(i).getTotals();
data[5] = list.get(i).getSGrade();

model.addRow(data);
}
}
/**
* This method is called from within the constructor to initialize the
form.
* WARNING: Do NOT modify this code. The content of this method is
always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();


jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
students_table = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLO
SE);

jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); //


NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("GRADES TABLE");

students_table.setModel(new
javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"Student_ID", "Name", "Homework Score", "Test Score",
"Totals in %", "Grades"
}
));
students_table.addMouseListener(new
java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
students_tableMouseClicked(evt);
}
});
jScrollPane1.setViewportView(students_table);

jButton1.setBackground(new java.awt.Color(0, 153, 204));


jButton1.setFont(new java.awt.Font("Times New Roman", 1, 14));
// NOI18N
jButton1.setText("GO TO ADD");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new


javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment
.LEADING)
.addComponent(jScrollPane1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(106, Short.MAX_VALUE)
.addComponent(jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE, 663,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA
TED)
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 120,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(79, 79, 79))
);
jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment
.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayou
t.Alignment.LEADING)
.addComponent(jLabel1,
javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
.addComponent(jButton1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addComponent(jPanel1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addComponent(jPanel1,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void
students_tableMouseClicked(java.awt.event.MouseEvent evt) {
MainApp app = new MainApp();
app.setVisible(true);
//SEt row values to be editable by the textfields in mainapp jframe
int i = students_table.getSelectedRow();
TableModel model = students_table.getModel();
app.stud_id.setText(model.getValueAt(i, 0).toString());
app.stud_name.setText(model.getValueAt(i, 1).toString());
app.stud_hwScore.setText(model.getValueAt(i, 2).toString());
app.stud_tScore.setText(model.getValueAt(i, 3).toString());
setVisible(false);
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent


evt) {
// TODO add your handling code here:
MainApp app = new MainApp();
app.setVisible(true);
setVisible(false);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel
setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the
default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.
html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GradesTable.class.getName()).log(jav
a.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(GradesTable.class.getName()).log(jav
a.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(GradesTable.class.getName()).log(jav
a.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(GradesTable.class.getName()).log(jav
a.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GradesTable().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
public javax.swing.JTable students_table;
// End of variables declaration
}

Third class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package finalyearproject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
*
* @author Prof Oyondi
*/
public class MainApp extends javax.swing.JFrame {
private int shomeworks;
private int stestmarks;
private String grade;
private String resultID;
/**
* Creates new form MainApp
*/
public MainApp() {
initComponents();
stud_id.setEnabled(false);
}

/**
* This method is called from within the constructor to initialize the
form.
* WARNING: Do NOT modify this code. The content of this method is
always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
stud_id = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
stud_name = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
stud_hwScore = new javax.swing.JTextField();
stud_tScore = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLO
SE);
setBackground(new java.awt.Color(204, 204, 255));

jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); //


NOI18N

jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("STUDENT GRADING SYSTEM");

jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); //


NOI18N
jLabel2.setText("Student ID");

jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); //


NOI18N
jLabel3.setText("Student Name");
jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14)); //
NOI18N
jLabel4.setText("Home work Score not exceeding 30%");

jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); //


NOI18N
jLabel5.setText("Test score not exceeding 70%");

jButton1.setBackground(new java.awt.Color(0, 255, 0));


jButton1.setFont(new java.awt.Font("Times New Roman", 1, 14));
// NOI18N
jButton1.setText("UPDATE");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setBackground(new java.awt.Color(255, 51, 51));


jButton2.setFont(new java.awt.Font("Times New Roman", 1, 14));
// NOI18N
jButton2.setText("DELETE");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton4.setBackground(new java.awt.Color(51, 153, 255));


jButton4.setFont(new java.awt.Font("Times New Roman", 1, 14));
// NOI18N
jButton4.setText("ADD STUDENT");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton3.setFont(new java.awt.Font("Times New Roman", 3, 18));


// NOI18N
jButton3.setText("VIEW GRADES");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(305, 305, 305)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.LEADING)
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.LEADING)
.addComponent(jLabel5,
javax.swing.GroupLayout.PREFERRED_SIZE, 227,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addGap(157, 157, 157)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.LEADING)
.addComponent(stud_tScore,
javax.swing.GroupLayout.PREFERRED_SIZE, 136,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(stud_hwScore,
javax.swing.GroupLayout.PREFERRED_SIZE, 136,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton4,
javax.swing.GroupLayout.PREFERRED_SIZE, 186,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(218, 218, 218)
.addComponent(jButton2,
javax.swing.GroupLayout.PREFERRED_SIZE, 119,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 128,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.LEADING)
.addComponent(jLabel2,
javax.swing.GroupLayout.PREFERRED_SIZE, 227,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3,
javax.swing.GroupLayout.PREFERRED_SIZE, 227,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(162, 162, 162)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.LEADING)
.addComponent(stud_name,
javax.swing.GroupLayout.PREFERRED_SIZE, 136,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(stud_id,
javax.swing.GroupLayout.PREFERRED_SIZE, 136,
javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGroup(layout.createSequentialGroup()
.addGap(196, 196, 196)
.addComponent(jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE, 774,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(465, 465, 465)
.addComponent(jButton3,
javax.swing.GroupLayout.PREFERRED_SIZE, 169,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(227, Short.MAX_VALUE))
);
layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE, 52,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(59, 59, 59)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jLabel2,
javax.swing.GroupLayout.PREFERRED_SIZE, 40,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(stud_id,
javax.swing.GroupLayout.PREFERRED_SIZE, 40,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(21, 21, 21)
.addComponent(stud_name,
javax.swing.GroupLayout.PREFERRED_SIZE, 39,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA
TED)
.addComponent(stud_hwScore,
javax.swing.GroupLayout.PREFERRED_SIZE, 40,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(stud_tScore,
javax.swing.GroupLayout.PREFERRED_SIZE, 46,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(120, 120, 120)
.addComponent(jLabel3,
javax.swing.GroupLayout.PREFERRED_SIZE, 39,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(jLabel4,
javax.swing.GroupLayout.PREFERRED_SIZE, 40,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE
LATED)
.addComponent(jLabel5,
javax.swing.GroupLayout.PREFERRED_SIZE, 46,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 56,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4,
javax.swing.GroupLayout.PREFERRED_SIZE, 56,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jButton3,
javax.swing.GroupLayout.PREFERRED_SIZE, 45,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(67, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton2,
javax.swing.GroupLayout.PREFERRED_SIZE, 56,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(132, 132, 132))
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

private void jButton3ActionPerformed(java.awt.event.ActionEvent


evt) {
GradesTable tab = new GradesTable();
tab.setVisible(true);
setVisible(false);
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent


evt) {
if(stud_name.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Please fill the student
Name");
}else if(stud_hwScore.getText().equals("")){

JOptionPane.showMessageDialog(null, "Homework score must


not be empty");

}else if(stud_tScore.getText().equals("")){
//check if marks is an integer
JOptionPane.showMessageDialog(null, "Test marks must not
be empty");

}else{

//Calculate the total score


//check if value entered is an integer
Connection connect = createConnection();
String homework = stud_hwScore.getText();
String db_ID = "SELECT `id` FROM `students`";
Statement stmt;
ResultSet result;
String ID = stud_id.getText();
try{
stmt = connect.createStatement();
result = stmt.executeQuery(db_ID);
StudentInfo stud;
while(result.next())
{
String resultID = result.getString("id");
}

if(ID == resultID){
JOptionPane.showMessageDialog(null, "Sudent ID already
exists");
//System.exit(1);
}
}catch(Exception ex){
ex.printStackTrace();
}

//validate home work to be only number


try {
shomeworks = Integer.parseInt(homework);
}
catch (NumberFormatException e) {
//if Not an integer output this error
JOptionPane.showMessageDialog(null, "Homework mark
must be an integer value");

}
//validate test score to be only number
String test = stud_tScore.getText();
try {
stestmarks = Integer.parseInt(test);

}
catch (NumberFormatException e) {
//Not an integer
JOptionPane.showMessageDialog(null, "Test marks must be
an integer value");
}
int total;
total = shomeworks + stestmarks;
total = shomeworks + stestmarks;
if (shomeworks > 30){
//JOptionPane.showMessageDialog(null, "Home work
should not exceed score of 30%!");
JOptionPane.showMessageDialog(null , "Homework score
must not exceed 30%" , "Homework" ,
JOptionPane.INFORMATION_MESSAGE);
}else if(stestmarks > 70){
JOptionPane.showMessageDialog(null, "Test score must
not exceed 70%","testmarks", JOptionPane.INFORMATION_MESSAGE);
//System.exit(0);
}else{
if(total > 89 ){
grade = "A";
}else if(total > 79 && total < 90){
grade = "B";
}else if(total > 69 && total <= 80){
grade = "C";
}else if(total > 59 && total <= 70){
grade = "D";
}else if(total < 60){
grade = "F";
}else if(total > 100 || total < 0){
JOptionPane.showMessageDialog(null, "Total Can not
exceed 100!","total", JOptionPane.INFORMATION_MESSAGE);
}
String query = "INSERT INTO `students`(`username`,
`HomeWork`, `TEST`, `total`, `grade`) VALUES
('"+stud_name.getText()+"','"+stud_hwScore.getText()+"','"+stud_tScor
e.getText()+"','"+total+"','"+grade+"')";

CommandHelper(query," added ");


//clear all fields after insertion
stud_id.setText("");
stud_name.setText("");
stud_hwScore.setText("");
stud_tScore.setText("");

}
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent


evt) {
//query to delete student data
String query = "DELETE FROM `students` WHERE
`id`="+stud_id.getText();
CommandHelper(query," Deleted");
GradesTable tab = new GradesTable();
tab.setVisible(true);
setVisible(false);
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent


evt) {
if(stud_name.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Please fill the student
Name");
}else if(stud_hwScore.getText().equals("")){

JOptionPane.showMessageDialog(null, "Homework score must


not be empty");

}else if(stud_tScore.getText().equals("")){
//check if marks is an integer
JOptionPane.showMessageDialog(null, "Test marks must not
be empty");

}else{
//Calculate the total SCORE
//check if value entered is an integer
Connection connect = createConnection();
String homework = stud_hwScore.getText();
String db_ID = "SELECT `id` FROM `students`";
Statement stmt;
ResultSet result;
String ID = stud_id.getText();
try{
stmt = connect.createStatement();
result = stmt.executeQuery(db_ID);
StudentInfo stud;
while(result.next())
{
String resultID = result.getString("id");
}

if(ID == resultID){
JOptionPane.showMessageDialog(null, "Sudent ID already
exists");
//System.exit(1);
}
}catch(Exception ex){
ex.printStackTrace();
}

//validate home work to be only number


try {
shomeworks = Integer.parseInt(homework);
}
catch (NumberFormatException e) {
//if Not an integer output this error
JOptionPane.showMessageDialog(null, "Homework mark
must be an integer value");

}
//validate test score to be only number
String test = stud_tScore.getText();
try {
stestmarks = Integer.parseInt(test);

}
catch (NumberFormatException e) {
//Not an integer
JOptionPane.showMessageDialog(null, "Test marks must be
an integer value");
}
int total;
total = shomeworks + stestmarks;
total = shomeworks + stestmarks;
if (shomeworks > 30){
//JOptionPane.showMessageDialog(null, "Home work
should not exceed score of 30%!");
JOptionPane.showMessageDialog(null , "Homework score
must not exceed 30%" , "Homework" ,
JOptionPane.INFORMATION_MESSAGE);
}else if(stestmarks > 70){
JOptionPane.showMessageDialog(null, "Test score must
not exceed 70%","testmarks", JOptionPane.INFORMATION_MESSAGE);
//System.exit(0);
}else{
if(total > 89 ){
grade = "A";
}else if(total > 79 && total < 90){
grade = "B";
}else if(total > 69 && total <= 80){
grade = "C";
}else if(total > 59 && total <= 70){
grade = "D";
}else if(total < 60){
grade = "F";
}else if(total > 100 || total < 0){
JOptionPane.showMessageDialog(null, "Total Can not
exceed 100!","total", JOptionPane.INFORMATION_MESSAGE);
}
String query = "UPDATE `students` SET
`username`='"+stud_name.getText()+"',`HomeWork`='"+stud_hwScore.
getText()+"',`TEST`='"+stud_tScore.getText()+"',`total`='"+total+"',`Grad
e`='"+grade+"' WHERE `id` = "+stud_id.getText();

CommandHelper(query," updated ");


GradesTable tab = new GradesTable();
tab.setVisible(true);
setVisible(false);
}
}
}
// all functions, add, update, delete works under the operation of
this command
public void CommandHelper(String sql,String result)
{
Connection con = createConnection();
Statement stmt;
try{
stmt = con.createStatement();
if(stmt.executeUpdate(sql) == 1)
{
//refresh table automatically
GradesTable tbl = new GradesTable();
DefaultTableModel model = (DefaultTableModel)
tbl.students_table.getModel();
model.setRowCount(0);
tbl.show_Student_Details();
JOptionPane.showMessageDialog(null,"Student data " + result
+" successfully");
}else{
JOptionPane.showMessageDialog(null,"Student data not " +
result);
}
}catch(Exception exp){
exp.printStackTrace();
}
}
public Connection createConnection()
{

Connection conn = null;


//try to connect to the studentsdb
try{

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/studentsdb";
String user = "root";
String password = "";
conn = DriverManager.getConnection(url,user ,password);

return conn;
}catch(Exception e){
//print error message if not connected
e.printStackTrace();
return null;
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel
setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the
default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.
html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainApp.class.getName()).log(java.u
til.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(MainApp.class.getName()).log(java.u
til.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(MainApp.class.getName()).log(java.u
til.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(MainApp.class.getName()).log(java.u
til.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainApp().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
public javax.swing.JTextField stud_hwScore;
public javax.swing.JTextField stud_id;
public javax.swing.JTextField stud_name;
public javax.swing.JTextField stud_tScore;
// End of variables declaration
}

Fourth class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package finalyearproject;

/**
*
* @author Prof Oyondi
*/
public class Splash extends javax.swing.JFrame {

/**
* Creates new form Splash
*/
public Splash() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the
form.
* WARNING: Do NOT modify this code. The content of this method is
always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();


jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
loadingNum = new javax.swing.JLabel();
jProgressBarload = new javax.swing.JProgressBar();
guides_add = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLO
SE);
setUndecorated(true);

jPanel1.setBackground(new java.awt.Color(153, 255, 255));


jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 24)); //
NOI18N

jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel5.setText(" STUDENT GRADING SYSTEM");

jLabel6.setFont(new java.awt.Font("Times New Roman", 3, 24)); //


NOI18N

jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel6.setText("WELLCOME TO CS DEPARTMENT");

loadingNum.setFont(new java.awt.Font("Times New Roman", 2,


18)); // NOI18N

loadingNum.setHorizontalAlignment(javax.swing.SwingConstants.CENT
ER);
loadingNum.setText("99%");

javax.swing.GroupLayout jPanel1Layout = new


javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment
.LEADING)
.addComponent(jProgressBarload,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(guides_add,
javax.swing.GroupLayout.PREFERRED_SIZE, 539,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(21, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel5,
javax.swing.GroupLayout.PREFERRED_SIZE, 447,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(112, 112, 112)
.addComponent(loadingNum,
javax.swing.GroupLayout.PREFERRED_SIZE, 314,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jLabel6,
javax.swing.GroupLayout.PREFERRED_SIZE, 400,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment
.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5,
javax.swing.GroupLayout.PREFERRED_SIZE, 40,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(39, 39, 39)
.addComponent(jLabel6,
javax.swing.GroupLayout.PREFERRED_SIZE, 57,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addComponent(loadingNum,
javax.swing.GroupLayout.PREFERRED_SIZE, 50,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(jProgressBarload,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA
TED)
.addComponent(guides_add,
javax.swing.GroupLayout.PREFERRED_SIZE, 49,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addComponent(jPanel1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addComponent(jPanel1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel
setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the
default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.
html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.l
ogging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.l
ogging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.l
ogging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.l
ogging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Splash().setVisible(true);
}
});
}

// Variables declaration - do not modify


public javax.swing.JLabel guides_add;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
public javax.swing.JProgressBar jProgressBarload;
public javax.swing.JLabel loadingNum;
// End of variables declaration
}

Five th class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package finalyearproject;

/**
*
* @author Prof Oyondi
*/
public class StudentInfo {
private int id;
private String name;
private int hwScore;
private int tScore;

private int sTotal;


private String sGrade;
//constructor to get student data values from the database
StudentInfo(int stude_id, String sName, int homework, int test, int
Total, String Grades) {
this.id = stude_id;
this.name = sName;
this.hwScore = homework;
this.tScore = test;
this.sTotal = Total;
this.sGrade = Grades;
}
//Encapsulation applied here
public int getSID()
{
//returns student id
return id;
}
public String getSName()
{
//returns student name
return name;
}
public int getHScore()
{
//returns homework score
return hwScore;
}
public int getSTest()
{
//returns student test score
return tScore;
}
public int getTotals()
{
return sTotal;
}
public String getSGrade()
{
return sGrade;
}
}

You might also like