For this Program, please use eclips, Java language thank you CIS355A Week 6 Lab—
ID: 3814443 • Letter: F
Question
For this Program, please use eclips, Java language
thank you
CIS355A Week 6 Lab—Database Connectivity
OBJECTIVES
Programmatic access to a MySQL database to add and display records
PROBLEM: Student Management System
A teacher needs the ability to store and retrieve student data. This includes
student name;
three test scores;
average; and
letter grade.
FUNCTIONAL REQUIREMENTS
You can code the GUI by hand or use NetBeans GUI builder interface.
Create a GUI which allows for input and display of student data.
It should include buttons to save a record, display all records.
Create a database and table to store student name and three test scores. (Note that average and grade are calculated by app.)
Student class
Create a Student class to manage the student data. It should have private instance variables of
student name; and
three test scores.
The class must have the following methods.
A default and parameterized constructor
Sets/gets for all instance variables
A get method to calculate and return the average
A get method to calculate and return the letter grade
toString to display the name of the student
StudentDB class
Create a StudentDB class that is used to create a connection and interface with the database.
This class should have two methods.
getAll—reads data from database, returns data in an arraylist of student objects
add—writes a record to the database
GUI class
Insert button will take the info from the GUI (student name and three test scores) and insert a record into the table. Input should be cleared from the textboxes. Display button will read the data from the database and creates a report in Console window, sample format below.
Name Test1 Test2 Test3 Avg Grade
Bruce Wayne 90 95 98 94.3 A
Clark Kent 65 70 90 75.0 C
Sample GUI
RUBRIC
Student class
Has all required functionality
10
GUI class
Student record can be saved
All student data can be displayed
15
StudentDB class
add method inserts a record into db.
get method reads all records and returns in arraylist.
15
Code style
5
Lab Report
10
TOTAL
55
CODE STYLE REQUIREMENTS
Include meaningful comments throughout your code.
Use meaningful names for variables.
Code must be properly indented.
Include a comment header at beginning of each file, example below.
/****************************************************
Program Name: ProgramName.java
Programmer's Name: Student Name
Program Description: Describe here what this program will do
***********************************************************/
DELIVERABLES
Submit as a SINGLE zip folder
all java files; and
the Lab report.
Follow assignment specification regarding class/method names.
Note that your Java file name must match class name (DO NOT rename).
Explanation / Answer
First of all, we don't do projects. We only help with assignments. Anyways, I have completed all your code and GUI design. Please upvote
------------------------------------------------------------------
-- SQL SCRIPTS
CREATE DATABASE titans;
USE titans;
CREATE TABKE student(name varchar(255) primary key not null, test_score_one double default 0, test_score_two double default 0, test_score_three double default 0);
------------------------------------------------------------------
public class Student {
private String name;
private double testScoreOne;
private double testScoreTwo;
private double testScoreThree;
public Student() {
super();
}
public Student(String name, double testScoreOne, double testScoreTwo, double testScoreThree) {
this.name = name;
this.testScoreOne = testScoreOne;
this.testScoreTwo = testScoreTwo;
this.testScoreThree = testScoreThree;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getTestScoreOne() {
return testScoreOne;
}
public void setTestScoreOne(double testScoreOne) {
this.testScoreOne = testScoreOne;
}
public double getTestScoreTwo() {
return testScoreTwo;
}
public void setTestScoreTwo(double testScoreTwo) {
this.testScoreTwo = testScoreTwo;
}
public double getTestScoreThree() {
return testScoreThree;
}
public void setTestScoreThree(double testScoreThree) {
this.testScoreThree = testScoreThree;
}
public double getAverage() {
return (testScoreOne + testScoreTwo + testScoreThree)/3;
}
public String getGrade() {
/**
* TODO : If you have any other specific implementation for grades
* calculation, please use it
*/
double averageScore = getAverage();
if (averageScore >= 90) {
return "A";
}
else if (averageScore <90 && averageScore >=80) {
return "B";
}
else if (averageScore <80 && averageScore >=60) {
return "C";
}
else if (averageScore <60 && averageScore >=40) {
return "D";
}
else {
return "E";
}
}
@Override
public String toString() {
return "name=" + name + ", testScoreOne=" + testScoreOne + ", testScoreTwo=" + testScoreTwo + ", testScoreThree=" + testScoreThree +", Average = "+getAverage() +", grade = "+getGrade();
}
}
--------------------------------------------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class StudentDB {
public ArrayList<Student> getAllStudentData() throws SQLException, ClassNotFoundException {
Connection connection = getConnection();
Statement statement=connection.createStatement();
String sqlQuery = "SELECT * FROM student;";
ResultSet result = statement.executeQuery(sqlQuery);
ArrayList<Student> studentList = new ArrayList<>();
while (result.next()) {
Student student = new Student(result.getString("name"),
result.getDouble("test_score_one"), result.getDouble("test_score_two"), result.getDouble("test_score_three"));
studentList.add(student);
}
return studentList;
}
public void addStudentData(Student student)
throws SQLException, ClassNotFoundException {
Statement statement;
try (Connection connection = getConnection()) {
statement = connection.createStatement();
/*
* Insert one record at a time
*/
String sqlQuery = "INSERT INTO student(name, test_score_one, test_score_two, test_score_three) "
+ "VALUES('" + student.getName()+"', " + student.getTestScoreOne()
+", " + student.getTestScoreTwo() + ", " + student.getTestScoreThree() + ");";
statement.executeUpdate(sqlQuery);
statement.close();
connection.close();
}
}
public Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/titans","root","root");
return connection;
}
}
--------------------------------------------------------------------------
import java.sql.SQLException;
public class InsertPanel extends javax.swing.JPanel {
StudentDB studentDB = new StudentDB();
/**
* Creates new form InsertPanel
*/
public InsertPanel() {
initComponents();
warningLabel.setVisible(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() {
textFieldStudentName = new javax.swing.JTextField();
textFieldScore1 = new javax.swing.JTextField();
textFieldScore2 = new javax.swing.JTextField();
textFieldScore3 = new javax.swing.JTextField();
insertDataButton = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
warningLabel = new javax.swing.JLabel();
insertDataButton.setText("Insert Data");
insertDataButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
insertDataButtonActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jLabel1.setText("Student Name");
jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jLabel2.setText("Test-1 Score");
jLabel3.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jLabel3.setText("Test-3 Score");
jLabel4.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
jLabel4.setText("Test-2 Score");
warningLabel.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
warningLabel.setForeground(new java.awt.Color(230, 17, 17));
warningLabel.setText("Error while inserting the data");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textFieldScore2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textFieldStudentName, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(insertDataButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textFieldScore3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
.addComponent(textFieldScore1, javax.swing.GroupLayout.Alignment.LEADING)))
.addGap(30, 30, 30))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(warningLabel)
.addGap(61, 61, 61))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textFieldStudentName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textFieldScore1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textFieldScore2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textFieldScore3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(30, 30, 30)
.addComponent(insertDataButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(warningLabel)
.addContainerGap())
);
}// </editor-fold>
private void insertDataButtonActionPerformed(java.awt.event.ActionEvent evt) {
warningLabel.setVisible(false);
String name = textFieldStudentName.getText();
double testScore1 = Double.valueOf(textFieldScore1.getText());
double testScore2 = Double.valueOf(textFieldScore2.getText());
double testScore3 = Double.valueOf(textFieldScore3.getText());
Student student = new Student(name, testScore1, testScore2, testScore3);
try {
studentDB.addStudentData(student);
} catch (SQLException | ClassNotFoundException ex) {
System.out.println("Error while inserting the data into the database");
warningLabel.setVisible(true);
}
/* Clear all fields */
textFieldStudentName.setText("");
textFieldScore1.setText("");
textFieldScore1.setText("");
textFieldScore1.setText("");
}
// Variables declaration - do not modify
private javax.swing.JButton insertDataButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField textFieldScore1;
private javax.swing.JTextField textFieldScore2;
private javax.swing.JTextField textFieldScore3;
private javax.swing.JTextField textFieldStudentName;
private javax.swing.JLabel warningLabel;
// End of variables declaration
}
-----------------------------------------------------------------------------------
import java.sql.SQLException;
import java.util.ArrayList;
public class DisplayPanel extends javax.swing.JPanel {
StudentDB studentDB = new StudentDB();
/**
* Creates new form DisplayPanel
*/
public DisplayPanel() {
initComponents();
setData();
}
/**
* 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() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 20)); // NOI18N
jLabel1.setText("Student's Data");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(122, 122, 122)
.addComponent(jLabel1)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>
public void setData() {
ArrayList<Student> studentList = null;
try {
studentList = studentDB.getAllStudentData();
} catch (SQLException | ClassNotFoundException e) {
System.out.println("Error while getting the data from the database");
e.printStackTrace();
}
String outputString = "";
for (Student student : studentList) {
outputString += student.toString()+" ";
}
jTextArea1.setText(outputString);
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
-----------------------------------------------------------------------------
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class MainPanel extends javax.swing.JPanel {
InsertPanel insertPan = new InsertPanel();
DisplayPanel displayPan = new DisplayPanel();
/**
* Creates new form MainPanel
*/
public MainPanel() {
initComponents();
JFrame jFrame = new JFrame();
jFrame.setLayout(new BorderLayout());
jFrame.add(this);
jFrame.pack();
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jPanel1.setLayout(new BorderLayout());
jPanel1.setVisible(true);
displayPan.setVisible(false);
jPanel1.remove(displayPan);
insertPan.setVisible(true);
jPanel1.add(insertPan);
}
/**
* 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();
addData = new javax.swing.JButton();
displayData = new javax.swing.JButton();
jPanel1.setBackground(new java.awt.Color(244, 175, 106));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 276, Short.MAX_VALUE)
);
addData.setText("Add Data");
addData.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addDataActionPerformed(evt);
}
});
displayData.setText("Display Data");
displayData.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
displayDataActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(addData, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
.addComponent(displayData, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(68, 68, 68))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(addData)
.addComponent(displayData))
.addGap(9, 9, 9)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>
private void addDataActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
displayPan.setVisible(false);
jPanel1.remove(displayPan);
insertPan.setVisible(true);
jPanel1.add(insertPan);
}
private void displayDataActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
insertPan.setVisible(false);
jPanel1.remove(insertPan);
jPanel1.add(displayPan);
displayPan.setVisible(true);
}
public static void main(String args[]) {
new MainPanel();
}
// Variables declaration - do not modify
private javax.swing.JButton addData;
private javax.swing.JButton displayData;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}