Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views10 pages

JSP For Mysql Database

This document outlines the steps to connect a Java application with a MySQL database using JSP. It includes details on setting up the database, creating necessary files, and writing the source code for form submission and data retrieval. Additionally, it provides execution procedures for running the application on a Tomcat server.

Uploaded by

starannu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views10 pages

JSP For Mysql Database

This document outlines the steps to connect a Java application with a MySQL database using JSP. It includes details on setting up the database, creating necessary files, and writing the source code for form submission and data retrieval. Additionally, it provides execution procedures for running the application on a Tomcat server.

Uploaded by

starannu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

JSP Programs Database Connectivity with

MySQL
To connect Java application with the MySQL database, we need to follow 5 following
steps.

In this example we are using MySql as the database. So we need to know following
informations for the mysql database:

1. Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
2. Connection URL: The connection URL for the mysql database
is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the
database, localhost is the server name on which mysql is running, we may also
use IP address, 3306 is the port number and sonoo is the database name. We
may use any database, in such case, we need to replace the cse with our
database name.
3. Username: The default username for the mysql database is root.
4. Password: It is the password given by the user at the time of installing the
mysql database. In this example, we are going to use root as the password.

Let's first create a table in the mysql database, but before creating table, we need to
create database first.

1. create database cse;


2. use cse;
3. create table studentname (firstname varchar(80), lastname varchar(80));

Example to Connect Java Application with mysql database

Files creation

Step 1: Open the path C:\xampp\tomcat\webapps\ROOT

Step 2: Create One folder Name Ex; csea

Step 3: open C:\xampp\tomcat\webapps\ROOT\csea

Dr Ravi Babu, CSE, GST, GITAM HYD


Step 4: Here create our files example

form.jsp and reg.jsp

Database Setup Connection with Tomcat Server


To connect JSP (java) application with the mysql database, mysqlconnector.jar file is
required to be loaded.

download the jar file mysql-connector.jar

C:\xampp\tomcat\lib

Copy mysql-connector.jar file to above lib folder

Dr Ravi Babu, CSE, GST, GITAM HYD


Source code:

form.jsp
<html>

<body>

<form action = "reg.jsp" method = "GET">

first name: <input type = "text" name = "fname">

<br/>

lastname: <input type = "text" name = "lname" > <br/>

<input type = "Submit" value = "insert" />

</form>

</body>

</html>

reg.jsp
<%@ page import ="java.sql.*" %>

Dr Ravi Babu, CSE, GST, GITAM HYD


<%

String first_name = request.getParameter("fname");

String last_name = request.getParameter("lname");

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

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

Statement st = con.createStatement();

//ResultSet rs;

st.executeUpdate("insert into studentname(firstname,lastname)values('" + first_name + "','" +


last_name + "')");

out.print("Registration Successfull!"+"<a href='loginform.jsp'>Go to insert again</a>");

%>

Execution Procedure

Step 1: Open xampp control panel

Run

Dr Ravi Babu, CSE, GST, GITAM HYD


Apache Press Start button

Similary MySql and Tomcat

Dr Ravi Babu, CSE, GST, GITAM HYD


Now Press Admin button for Tomcat

Type the path in the above browser

Dr Ravi Babu, CSE, GST, GITAM HYD


localhost:8080/csea/form.jsp

Enter data to the files and submit button

Dr Ravi Babu, CSE, GST, GITAM HYD


JSP program to retrieve data from MYSQL database table

display.jsp

<%@page import="java.sql.*" %>


<%@page import="java.io.*" %>
<html>
<body>
</br>

Dr Ravi Babu, CSE, GST, GITAM HYD


</br>
<h1 align='center' style="color:red">Data</h1>

<form action='display.jsp'>
<table align='center' cellpadding='10' border='1'>

<%
try
{

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cse", "root", "");

Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select firstname,lastname from studentname");
ResultSetMetaData rsmd = rs.getMetaData();

for(int i=1 ; i<=rsmd.getColumnCount() ; i++)


{
out.print("<th>"+rsmd.getColumnName(i)+"</th>");
}

while(rs.next())
{

out.print("<tr>");

for(int i=1 ; i<=rsmd.getColumnCount() ; i++)


{
out.print("<td>"+rs.getString(i)+"</td>");
}
out.print("</tr>");
}
}
catch(Exception e)
{
out.print(e);
}
%>
</table>

</form>
</body>
</html>

localhost:8080/csea/display.jsp

Dr Ravi Babu, CSE, GST, GITAM HYD


Dr Ravi Babu, CSE, GST, GITAM HYD

You might also like