0% found this document useful (0 votes)
1 views

Java Database Connectivity

JDBC code

Uploaded by

shahbharat2609
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java Database Connectivity

JDBC code

Uploaded by

shahbharat2609
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

JDBC ASSIGNMENT

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

class studentForm extends JFrame {


private JTextField rollNoField, classField, nameField,
fatherNameField, contactField, hobbiesField;
private JComboBox<String> genderComboBox;

public studentForm() {
setTitle("Student Information Form");
setSize(450, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel(new GridBagLayout());


GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 10, 5, 10);

// Create form labels and fields


addFormField(panel, "Roll No:", gbc);
addFormField(panel, "Class:", gbc);
addFormField(panel, "Name:", gbc);
addFormField(panel, "Father's Name:", gbc);
addGenderFormField(panel, "Gender:", gbc);
addMobileNumberField(panel, "Contact:", gbc);
addFormField(panel, "Hobbies (comma-separated):",
gbc);

// Submit button
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
insertStudentInfo();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}
});
gbc.gridx = 1;
gbc.gridy++;
panel.add(submitButton, gbc);

add(panel);
}

private void addFormField(JPanel panel, String label,


GridBagConstraints gbc) {
JLabel jLabel = new JLabel(label);
gbc.gridx = 0;
gbc.gridy++;
panel.add(jLabel, gbc);

JTextField field = new JTextField(25);


field.setPreferredSize(new Dimension(300, 30));
gbc.gridx = 1;
panel.add(field, gbc);

// Assign the field to the corresponding instance


variable
if (label.equals("Roll No:")) {
rollNoField = field;
} else if (label.equals("Class:")) {
classField = field;
} else if (label.equals("Name:")) {
nameField = field;
} else if (label.equals("Father's Name:")) {
fatherNameField = field;
} else if (label.equals("Hobbies (comma-separated):"))
{
hobbiesField = field;
}
}

private void addGenderFormField(JPanel panel, String


label, GridBagConstraints gbc) {
JLabel jLabel = new JLabel(label);
gbc.gridx = 0;
gbc.gridy++;
panel.add(jLabel, gbc);

String[] genderOptions = {"Male", "Female", "Other"};


genderComboBox = new JComboBox<>(genderOptions);

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
panel.add(genderComboBox, gbc);
}

private void addMobileNumberField(JPanel panel, String


label, GridBagConstraints gbc) {
JLabel jLabel = new JLabel(label);
gbc.gridx = 0;
gbc.gridy++;
panel.add(jLabel, gbc);

JTextField field = new JTextField(25);


field.setPreferredSize(new Dimension(300, 30));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
panel.add(field, gbc);

contactField = field;
}

private void insertStudentInfo() throws


ClassNotFoundException {
String rollNo = rollNoField.getText();
String className = classField.getText();
String name = nameField.getText();
String fatherName = fatherNameField.getText();
String gender = (String)
genderComboBox.getSelectedItem();
String contact = contactField.getText();
String hobbies = hobbiesField.getText();

if (contact.length() > 10) {


JOptionPane.showMessageDialog(this, "Error: Mobile
number should not exceed 10 digits.");
return; // Exit the method if the mobile number is
invalid
}

try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/stude
nt", "root", "shahbharat7");
String insertQuery = "INSERT INTO student_info
(roll_no, class, student_name, father_name, gender, contact,
hobbies) VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStatement =
connection.prepareStatement(insertQuery);
preparedStatement.setString(1, rollNo);
preparedStatement.setString(2, className);
preparedStatement.setString(3, name);
preparedStatement.setString(4, fatherName);
preparedStatement.setString(5, gender);
preparedStatement.setString(6, contact);
preparedStatement.setString(7, hobbies);
preparedStatement.executeUpdate();
JOptionPane.showMessageDialog(this, "Student
information added to the database.");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: Unable
to insert data into the database.");
}
}

public static void main(String[] args) {


new studentForm().setVisible(true);
}
}

Student Information Form:


Data entered into the database:

You might also like