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

Assignment 1

The document contains Java code for a user registration GUI application that allows users to enter their first name, last name, email, username, password, and mobile number. It connects to a PostgreSQL database called "swing_demo" to insert the user details into a table called "account" when the register button is clicked. The SQL code to create the database and account table is also included.

Uploaded by

kazisaklain4
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)
13 views

Assignment 1

The document contains Java code for a user registration GUI application that allows users to enter their first name, last name, email, username, password, and mobile number. It connects to a PostgreSQL database called "swing_demo" to insert the user details into a table called "account" when the register button is clicked. The SQL code to create the database and account table is also included.

Uploaded by

kazisaklain4
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/ 9

ASSIGNMENT 1

Java Code:-
package net.javaguides.swing;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class UserRegistration extends JFrame {


private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField firstname;
private JTextField lastname;
private JTextField email;
private JTextField username;
private JTextField mob;
private JPasswordField passwordField;
private JButton btnNewButton;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserRegistration frame = new UserRegistration();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/

public UserRegistration() {
setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\panda.jpg"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(450, 190, 1014, 597);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblNewUserRegister = new JLabel("New User Register");


lblNewUserRegister.setFont(new Font("Times New Roman", Font.PLAIN, 42));
lblNewUserRegister.setBounds(362, 52, 325, 50);
contentPane.add(lblNewUserRegister);

JLabel lblName = new JLabel("First name");


lblName.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblName.setBounds(58, 152, 99, 43);
contentPane.add(lblName);

JLabel lblNewLabel = new JLabel("Last name");


lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblNewLabel.setBounds(58, 243, 110, 29);
contentPane.add(lblNewLabel);

JLabel lblEmailAddress = new JLabel("Email\r\n address");


lblEmailAddress.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblEmailAddress.setBounds(58, 324, 124, 36);
contentPane.add(lblEmailAddress);

firstname = new JTextField();


firstname.setFont(new Font("Tahoma", Font.PLAIN, 32));
firstname.setBounds(214, 151, 228, 50);
contentPane.add(firstname);
firstname.setColumns(10);

lastname = new JTextField();


lastname.setFont(new Font("Tahoma", Font.PLAIN, 32));
lastname.setBounds(214, 235, 228, 50);
contentPane.add(lastname);
lastname.setColumns(10);

email = new JTextField();

email.setFont(new Font("Tahoma", Font.PLAIN, 32));


email.setBounds(214, 320, 228, 50);
contentPane.add(email);
email.setColumns(10);

username = new JTextField();


username.setFont(new Font("Tahoma", Font.PLAIN, 32));
username.setBounds(707, 151, 228, 50);
contentPane.add(username);
username.setColumns(10);

JLabel lblUsername = new JLabel("Username");


lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblUsername.setBounds(542, 159, 99, 29);
contentPane.add(lblUsername);

JLabel lblPassword = new JLabel("Password");


lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblPassword.setBounds(542, 245, 99, 24);
contentPane.add(lblPassword);

JLabel lblMobileNumber = new JLabel("Mobile number");


lblMobileNumber.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblMobileNumber.setBounds(542, 329, 139, 26);
contentPane.add(lblMobileNumber);

mob = new JTextField();


mob.setFont(new Font("Tahoma", Font.PLAIN, 32));
mob.setBounds(707, 320, 228, 50);
contentPane.add(mob);
mob.setColumns(10);

passwordField = new JPasswordField();


passwordField.setFont(new Font("Tahoma", Font.PLAIN, 32));
passwordField.setBounds(707, 235, 228, 50);
contentPane.add(passwordField);

btnNewButton = new JButton("Register");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String firstName = firstname.getText();
String lastName = lastname.getText();
String emailId = email.getText();
String userName = username.getText();
String mobileNumber = mob.getText();
int len = mobileNumber.length();
String password = passwordField.getText();

String msg = "" + firstName;


msg += " \n";
if (len != 10) {
JOptionPane.showMessageDialog(btnNewButton, "Enter a valid mobile
number");
}
try {
Connection connection =
DriverManager.getConnection("jdbc:postgresql://localhost:7777/swing_demo", "postgres",
"12345");

String query = "INSERT INTO account values('" + firstName + "','" + lastName


+ "','" + userName + "','" +
password + "','" + emailId + "','" + mobileNumber + "')";

Statement sta = connection.createStatement();


int x = sta.executeUpdate(query);
if (x == 0) {
JOptionPane.showMessageDialog(btnNewButton, "This is alredy exist");
} else {
JOptionPane.showMessageDialog(btnNewButton,
"Welcome, " + msg + "Your account is sucessfully created");
}
connection.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
});
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 22));
btnNewButton.setBounds(399, 447, 259, 74);
contentPane.add(btnNewButton);
}
}
Output :-
SQL Code: -

For Creating Database: -


create database swing_demo;
For Creating Table:-
CREATE TABLE account
( first_name varchar(250) NOT NULL,
last_name varchar(250) NOT NULL,
user_name varchar(250) NOT NULL,
password varchar(250),
email_id varchar(250) NOT NULL,
mobile_number varchar(250) NOT NULL
);

Output:-
Final Output: -

You might also like