Java database connectivity with MYSQL 8.0

Java database connectivity with the latest version of MYSQL. In this example, we are going to discuss the following points step by step to perform Java database connectivity with MYSQL 8.0

Create a Database in MYSQL.

Design a registration form using JSP or HTML.

Database connectivity to insert data in the database.

In this example, we are using the latest version of MYSQL 8.0 and the user name and password will be “root” and “root” for MySQL.

Note: There are only two changes for MYSQL 8.0 the first one is maven dependencies for MySQL 8 and the driver name”com.mysql.cj.jdbc.Driver”. the driver name is changed from MYSQL 8.0.

Java database connectivity with MYSQL 8.0

 Below is the line by line step to create database connectivity using MySQL in java

  • Load MYSQL driver
  • Create a connection with URL, Username, and password
  • Write a MySQL Query.
  • Prepare a statement
  • Pass the data according to the column
  • Execute and close the connection.
// Get the data request

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

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

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

//Database connectivity

try {

//Load MySQL Driver

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

//URL, Username and password to create connection

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javawebtraining","root","root");

//Write a sql query to insert data in register table

String query = "insert into register (username, email, password) values (?, ?, ?)";



//Create a statement on the connection

PreparedStatement stmt = con.prepareStatement(query);



//Pass data to statement.......... accordingly in column 1,2 and 3

stmt.setString(1, username);

stmt.setString(2, email);

stmt.setString(3, Password);



//Execute prepare statement

stmt.execute();



//Close the connection

con.close();

out.print("Data is instered successfully");

} catch (ClassNotFoundException | SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}



Let’s see the complete example

Create a database name as “javawebtraining” in MySQL

CREATE SCHEMA `javawebtraining` ;

Create a Table name as “Register”.

CREATE TABLE `javawebtraining`.`register` (

`uid` INT NOT NULL AUTO_INCREMENT,

`username` VARCHAR(45) NULL,

`email` VARCHAR(45) NULL,

`password` VARCHAR(45) NULL,

PRIMARY KEY (`uid`));

Create a maven project and add the dependency for mysql 8

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.11</version>

</dependency>

Design a simple registration form using HTML and CSS.

 <!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<style>

body {

font-family: Arial, Helvetica, sans-serif;

background-color: black;

}



* {

box-sizing: border-box;

}



.container {

padding: 16px;

background-color: white;

}



input[type=text], input[type=password] {

width: 100%;

padding: 15px;

margin: 5px 0 22px 0;

display: inline-block;

border: none;

background: #f1f1f1;

}



input[type=text]:focus, input[type=password]:focus {

background-color: #ddd;

outline: none;

}



.registerbtn {

background-color: #4CAF50;

color: white;

padding: 16px 20px;

margin: 8px 0;

border: none;

cursor: pointer;

width: 100%;

opacity: 0.9;

}

</style>

</head>



<body>

<form action="Register">

<div class="container">

<h1>Registeration form</h1>

<h3>Example to insert data in mysql 8.0</h3>

<label for="uname"><b>User Name :</b></label>

<input type="text" placeholder="Enter User Name" name="uname" required>



<label for="psw"><b>Email</b></label>

<input type="password" placeholder="Enter Password" name="email" required>



<label for="psw-repeat"><b>Password</b></label>

<input type="password" placeholder="Repeat Password" name="password" required>



<button type="submit" class="registerbtn">Register</button>

</div>



</form>



</body>

</html>

Create a servlet name as Register.java

Servlet will get the request from the client and pass it to the database

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



/**

* Servlet implementation class Register

*/

public class Register extends HttpServlet {

private static final long serialVersionUID = 1L;



/**

* @see HttpServlet#HttpServlet()

*/

public Register() {

super();

// TODO Auto-generated constructor stub

}



/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

PrintWriter out = response.getWriter();

// Get the data request

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

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

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



//Database connectivity

try {

//Load MySQL Driver

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

//URL, Username and password to create connection

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javawebtraining","root","root");

//Write a sql query to insert data in register table

String query = "insert into register (username, email, password) values (?, ?, ?)";



//Create a statement on the connection

PreparedStatement stmt = con.prepareStatement(query);



//Pass data to statement.......... accordingly in column 1,2 and 3

stmt.setString(1, username);

stmt.setString(2, email);

stmt.setString(3, Password);



//Execute prepare statement

stmt.execute();



//Close the connection

con.close();

out.print("Data is instered successfully");

} catch (ClassNotFoundException | SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}



}



/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}



}

 

Add a Comment

Your email address will not be published. Required fields are marked *

three + 5 =