0% found this document useful (0 votes)
9 views5 pages

Questions and Answers

The document outlines the design of a database application for student registration using Java and MySQL. It includes steps to create a MySQL database and table, as well as a Java GUI form with fields for user input. The application connects to the database using JDBC to store and retrieve registration data, including handling picture uploads.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Questions and Answers

The document outlines the design of a database application for student registration using Java and MySQL. It includes steps to create a MySQL database and table, as well as a Java GUI form with fields for user input. The application connects to the database using JDBC to store and retrieve registration data, including handling picture uploads.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Questions and Answers

Question:
Design a database application with the information below
Create a form (registration form) in Java with the following fields: Reg number, Surname,
First name, middle name, matric number, dob, picture, and course.
Design a database with MySQL with the fields stated above, hence or otherwise connect the
frontend to the backend using the JDBC driver.
NB: you are to fill the respective field with information and the information entered should
be displayed on the MySQL Database.

Answer:
To create the required database application with the registration form in Java and connect it
to a MySQL database using JDBC, we will break the task into several parts:

Step 1: Set up MySQL Database

First, we need to create a MySQL database and a table to store the registration data.

Create Database and Table in MySQL

CREATE DATABASE IF NOT EXISTS registration_db;

USE registration_db;

CREATE TABLE IF NOT EXISTS student_registration (


reg_number VARCHAR(50) PRIMARY KEY,
surname VARCHAR(50),
first_name VARCHAR(50),
middle_name VARCHAR(50),
matric_number VARCHAR(50),
dob DATE,
picture BLOB,
course VARCHAR(50)
);

Step 2: Frontend Registration Form in Java


You will need a Java GUI with the necessary fields and a button to submit the form. We will
use JDBC to connect the Java application to the MySQL database.

Java Program for Registration Form:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;

public class RegistrationForm extends JFrame {


// Define the form fields
private JTextField txtRegNumber, txtSurname, txtFirstName, txtMiddleName,
txtMatricNumber, txtCourse;
private JDateChooser txtDob; // Date picker for DOB
private JLabel lblPicture;
private JButton btnSubmit, btnUpload;
private byte[] pictureData;

// Constructor to set up the GUI components


public RegistrationForm() {
setTitle("Student Registration Form");
setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

setLayout(new GridLayout(9, 2));

// Initialize components
txtRegNumber = new JTextField();
txtSurname = new JTextField();
txtFirstName = new JTextField();
txtMiddleName = new JTextField();
txtMatricNumber = new JTextField();
txtCourse = new JTextField();
txtDob = new JDateChooser();
lblPicture = new JLabel("No picture selected");
btnUpload = new JButton("Upload Picture");
btnSubmit = new JButton("Submit");

// Add components to the form


add(new JLabel("Reg Number:"));
add(txtRegNumber);
add(new JLabel("Surname:"));
add(txtSurname);
add(new JLabel("First Name:"));
add(txtFirstName);
add(new JLabel("Middle Name:"));
add(txtMiddleName);
add(new JLabel("Matric Number:"));
add(txtMatricNumber);
add(new JLabel("DOB:"));
add(txtDob);
add(new JLabel("Course:"));
add(txtCourse);
add(lblPicture);
add(btnUpload);
add(btnSubmit);

// Action listeners for buttons


btnUpload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
lblPicture.setText(file.getName());
pictureData = new byte[(int) file.length()];
try {
FileInputStream fis = new FileInputStream(file);
fis.read(pictureData);
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});

btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Get user inputs
String regNumber = txtRegNumber.getText();
String surname = txtSurname.getText();
String firstName = txtFirstName.getText();
String middleName = txtMiddleName.getText();
String matricNumber = txtMatricNumber.getText();
java.util.Date dob = txtDob.getDate();
String course = txtCourse.getText();
Date sqlDob = new java.sql.Date(dob.getTime());

// Database connection and insert query


String dbURL = "jdbc:mysql://localhost:3306/registration_db";
String dbUsername = "root"; // Your MySQL username
String dbPassword = ""; // Your MySQL password

try {
Connection connection = DriverManager.getConnection(dbURL, dbUsername,
dbPassword);
String query = "INSERT INTO student_registration (reg_number, surname,
first_name, middle_name, matric_number, dob, picture, course) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pst = connection.prepareStatement(query);
pst.setString(1, regNumber);
pst.setString(2, surname);
pst.setString(3, firstName);
pst.setString(4, middleName);
pst.setString(5, matricNumber);
pst.setDate(6, sqlDob);
pst.setBytes(7, pictureData);
pst.setString(8, course);

// Execute the query


int rowsAffected = pst.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(null, "Registration Successful!");
} else {
JOptionPane.showMessageDialog(null, "Error in registration.");
}

connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Database connection failed!");
}
}
});
}
// Main method to run the registration form
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new RegistrationForm().setVisible(true);
}
});
}
}

You might also like