JSP MySQL Eclipse JEE Development Tutorial



Eclipse JEE IDE development tutorial is for absolute beginners.
Prerequisite for JSP/MySQL Application with Eclipse JEE Kepler.
Download and Install MySQL Server 5.5 on your Windows Machine
Install Tomcat Server6.0( 32-bit windows zip)
Download MySQL Connector/j for connecting JSP to 
MySQL Server.(mysql-connector-java-5.1.25.zip, Platform Independent (Architecture Independent))
Download Eclipse IDE for Java EE Developers 32 bit windows And unzip it
JSP MySQL Eclipse JEE Development Tutorial

Create a database and Table using MySQL : Open cmd prompt (Windows+R and Type 'cmd') or use

MySQL 5.5 Command line client. Change directory to MySQL Server 5.5 installed Path. (In this

example path looks like : C:\mysql\MySQL Server 5.5\bin>)
Create a new user in MYSQL Sever
mysql -u <user-name> -p
enter password: *****
 You can use root if you have super user privilege, or create a new user in MySQL, to create a new user:
CREATE USER 'nikisurf'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'nikisurf'@'localhost';
Login as a new user
mysql -u nikisurf -p
Create a new Database db and Selecting Database
CREATE DATABASE db;
USE db;
Create a new Table in MySQL called 'info'
CREATE TABLE info(username varchar(25),password varchar(25),
firstname varchar(25),lastname varchar(25),
address varchar(25),pincode int,phonenumber int);



Open Eclipse
Click eclipse.exe file from unzipped folder and set a work-space(location of projects)
Create a new Dynamic Web Project
File => New => Dynamic Web Project, and give Project name as ‘Info’ click Next
 again Next Finish.
Connect MySQL to JSP with Eclipse Kepler
Add Library : MySQL Connector/j
 Unzip mysql-connector-java-5.1 and select mysql-connector-java-5.1.23-bin.jar file and drag and drop into the Project explorer of the Eclipse(info => WebContent => WEB-INF => lib).
Start Coding
 Right Click on Info from Project Explorer => New => HTML File (Give Name as index.html)
index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Add Info</h1>
<form action="add.jsp" method="post">
User name : <input type="text" name="username" /><br />
password : <input type="password" name="password" /><br />
First name :<input type="text" name="firstname" /><br />
Last name :<input type="text" name="lastname" /><br />
address :<textarea cols="10" rows="15" name="address">
</textarea><br />
Pin code <input type="text" name="pincode" /><br /> <br />
phone number <input type="text" name="phonenumber" /><br />
<input type="submit" value="Add Details" /><br />
</form>
</body>
</html>


 Create a new JSP file : Right Click on Info from Project Explorer => New => JSP File (Give Name as add.jsp) Insert the codes between body tag of the document.
add.jsp

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

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

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

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

<%

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

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

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

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

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

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

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

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

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

Statement st=con.createStatement();

String sql="insert into info
(username,password,firstname,lastname,address,pincode,phonenumber)

values('"+username+"','"+password+"','"+firstname+"','"+lastname+"',
'"+address+"','"+pincode+"','"+phonenumber+"')";

int flag=st.executeUpdate(sql);

if(flag==1)

{

out.println("Added!");

}

else

{

out.println("Failed");

}

response.sendRedirect("show.html");

%>

Create delete.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="delete.jsp" method="post" >
Enter First name to delete:<input type="text" name="username" />
<input type="submit" value="delete" />
</form>
</body>
</html>

delete.jsp

<%@page import="java.sql.*" %>
<%@page import="java.io.*" %>
<%@page import="javax.sql.*" %>
<%@page import="java.sql.Connection" %>
<%
String username=request.getParameter("username");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/db",
"nikisurf","nikisurf");
Statement st=con.createStatement();
try
{
st.executeUpdate("DELETE FROM info WHERE username = '"+username+"'");
boolean flag=true;
if(flag==true)
{
out.println("Deleted");
}
else
{
out.println("Not possible : error!");
}
}
catch(Exception e)
{
out.println("Exception");
}
%>

show.html

<form action="show.jsp" method="post">
Enter User name
<input type="text" name="username" />
<input type="submit" value="show details" />

show.jsp

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

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

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

<%

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

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

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

Statement st=con.createStatement();

String query="select * from info where username='"+username+"'";

ResultSet rs=st.executeQuery(query);

%>

<%

while(rs.next())

{

%>

<table border="2" bordercolor="#2494b7">

<tr>

<th>UserName</th>

<th>Password</th>

<th>First Name</th>

<th>Last Name</th>

<th>Address</th>

<th>Pin code</th>

<th>Mobile Phone</th>

</tr>

<tr>

<td><%=rs.getString(1)%></td>

<td><%=rs.getString(2)%></td>

<td><%=rs.getString(3)%></td>

<td><%=rs.getString(4)%></td>

<td><%=rs.getString(5)%></td>

<td><%=rs.getString(6)%></td>

<td><%=rs.getString(7)%></td>

</tr>

</table>

<%

}

%>
Run JEE Eclipse Info Project
To run tomcat server with Eclipse : Right Click on Info from Project Explorer =>Run as => Run on Server


JSP MySQL Eclipse JEE Development Tutorial JSP MySQL Eclipse JEE Development Tutorial Reviewed by Nikin on 19 January Rating: 5
Powered by Blogger.