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

Homework # 2 I. Tutorial On How To Install Mysql: Ryle Ibañez 2itb

The document provides instructions on setting up a MySQL database to use for a Java web application project. It includes steps to install MySQL, create a database and table using MySQL Workbench, configure the MySQL driver in Eclipse, integrate the JDBC driver, and setup a servlet to connect the Java application to the database. Code examples are provided for a database manager class and Java servlet to validate user logins by querying the database table. Screenshots confirm the creation of the database, tables, and login functionality.

Uploaded by

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

Homework # 2 I. Tutorial On How To Install Mysql: Ryle Ibañez 2itb

The document provides instructions on setting up a MySQL database to use for a Java web application project. It includes steps to install MySQL, create a database and table using MySQL Workbench, configure the MySQL driver in Eclipse, integrate the JDBC driver, and setup a servlet to connect the Java application to the database. Code examples are provided for a database manager class and Java servlet to validate user logins by querying the database table. Screenshots confirm the creation of the database, tables, and login functionality.

Uploaded by

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

Ryle Ibañez

2ITB

HOMEWORK # 2

I. Tutorial on how to install MySQL

STEP 1:

- Download MySQL Installer 8.0.28 from


https://dev.mysql.com/downloads/windows/installer/8.0.html
- Download the second one on the list.
STEP 2: INSTALLATION OF MYSQL
STEP 3: SETTING UP YOUR DATABASE USING MYSQL WORKBENCH

SELECT LOCAL INSTANCE

ENTER YOUR PASSWORD (ROOT)


CLICK ON SCHEMAS VIEW

Right click  Create Schema


Name your schema then click apply

PRESS F5 ON YOUR KEYBOARD TO REFRESH THE VIEW

GO TO DATABASE  TABLES  RIGHT CLICK  CREATE TABLE


NAME YOUR TABLE

CONFIGURE THE COLUMN AND ITS DATA TYPES

CLICK APPLY
RIGHT CLICK ON THE TABLE THEN CLICK ON SELECT ROWS TO VIEW AND EDIT THE CONTENTS OF THE
TABLE.

EDIT THE VALUES INSIDE THE USERNAME AND PASSWORD  CLICK APPLY
STEP 4: CONFIGURE MYSQL DRIVER TO ECLIPSE

CREATE A NEW DYNAMIC WEB PROJECT

CLICK ON WINDOW  SHOW VIEW  DATA SOURCE EXPLORER

IN THE DATA SOURCE EXPLORER, RIGHT CLICK ON THE DATABSE CONNECTIONS THEN SELECT NEW
FROM THE LIST, SELECT MYSQL THEN INPUT YOUR PREFERRED NAME  CLICK NEXT

CHECK YOUR MYSQL WORKBENCH FOR THE CORRECT TITLE OF YOUR SCHEMA AND YOUR TABLE

ENTER THOSE DATA HERE  CLICK NEXT


URL  REMOVE THE LAST PART TO FIX PING FAILED ISSUE
WHEN ITS DONE THEN YOU WILL FINALLY SEE IT HERE.

STEP 5: INTEGRATION OF JDBC DRIVER (PART 2)

DOWNLOAD THE FILES UNDER WEBAPP  WEB-INF  LIB

PLACE ALL THOSE FILES INSIDE OF THAT FOLDER/DIRECTORY


CREATE A CLASS FOR THE DATABASE MANAGER

INSERT THIS CODE:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import net.Javaloginbean;

public class Database {

public boolean validate(Javaloginbean login) throws ClassNotFoundException {

boolean status = false;

Class.forName("com.mysql.jdbc.Driver");

try (Connection connection = DriverManager

.getConnection("jdbc:mysql://localhost:3306/yourdatabase?
useSSL=false", "root", "yourpassword");

// Step 2:Create a statement using connection object

PreparedStatement ps = connection

.prepareStatement("select * from NAMEOFTHETABLE where COLUMNNAME1 = ? and


COLUMNNAME2 = ? ")) {

ps.setString(1, login.getUsername());
ps.setString(2, login.getPassword());

System.out.println(ps);

ResultSet resultset = ps.executeQuery();

status = resultset.next();

} catch (SQLException e) {

// process sql exception

printSQLException(e);

return status;

private void printSQLException(SQLException ex) {

for (Throwable e : ex) {

if (e instanceof SQLException) {

e.printStackTrace(System.err);

System.err.println("SQLState: " + ((SQLException) e).getSQLState());

System.err.println("Error Code: " + ((SQLException) e).getErrorCode());

System.err.println("Message: " + e.getMessage());

Throwable err = ex.getCause();

while (err != null) {

System.out.println("Cause: " + err);

err = err.getCause();

}
STEP 6: SETUP THE SERVELT AND USE IT TO YOUR PROJECT.

II. THE HOMEWORK

Figure 1: PROJECT FILES

B. JAVA SERVLET FILE

package net;

import java.io.IOException;

import jakarta.servlet.RequestDispatcher;

import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;
import net.Database;

@WebServlet("/login")

public class javaservlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private Database database;

public void init() {

database = new Database();

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String username = request.getParameter("username");

String password = request.getParameter("password");

Javaloginbean loginBean = new Javaloginbean();

loginBean.setUsername(username);

loginBean.setPassword(password);

try {

if (database.validate(loginBean)) {

response.sendRedirect("loginsuccess.jsp");

if (username.equals(null) || username.equals("") || password.equals(null) ||


password.equals("")){

request.setAttribute("error", "No Input");

RequestDispatcher rs = request.getRequestDispatcher("login.jsp");
rs.include(request, response);

else if (username.matches("^(?![_ -])(?:(?![_ -]{2})[\\w -]){5,16}(?<![_ -])$") ||


username.length()>8 ) {

request.setAttribute("error", "Invalid username format! ");

RequestDispatcher rs = request.getRequestDispatcher("login.jsp");

rs.include(request, response);

else {

request.setAttribute("error", "Invalid login Credentials");

RequestDispatcher rs = request.getRequestDispatcher("login.jsp");

rs.include(request, response);

} catch (ClassNotFoundException e) {

e.printStackTrace();

C. JAVA LOGIN BEAN

package net;

import java.io.Serializable;

public class Javaloginbean implements Serializable {


/**

*/

private static final long serialVersionUID = 1L;

private String username;

private String password;

public String getUsername() {

return username;

public void setUsername(String username) {

this.username = username;

public String getPassword() {

return password;

public void setPassword(String password) {

this.password = password;

}
D. SCREENSHOTS OF THE PROJECT

A. LOGIN PAGE

B. LOGIN SUCCESS
C. MYSQL DATABASE

D. DATABASE TEST

You might also like