IV Reportfinal1
IV Reportfinal1
COLLEGE,VENNIKULAM
Submitted by
NANDHU RAJ
S5 COMPUTER ENGINEERING
REGISTER NO: 19130964
COLLEGE,VENNIKULAM
CERTIFICATE
Certified that this Industrial Training report is the bonafied work of NANDHU
RAJ (REG NO: 19130964) who carried out the work at SPECTRUM SOFTTECH
SOLUTIONS PVT.LTD MAHAKAVI G ROAD, KOCHI.
The completion of this work could have been possible with continued and dedicated
efforts and guidance of large number of faculty and staff members of the institute. I
acknowledge my gratitude to all of them.
Our Principal Mr. Biju George for giving me an opportunity to go for an Industrial Visit.
Our HOD Mrs. BEENA L S for her technical support, guidance and co-operation for the
successful completion of this study.
Our tutor Mrs. Asha Sukumaran for her mental support and guidance. I would like to
say that I am indebted to my parents, my friends and well-wishers for everything that they
have done for me. All of this would have been impossible without their constant support.
And I also thank God being kind to me and driving me through this journey.
Last but not the least I would like to express my sincere thanks to every one and apologies
to any one who deserve name fails to appear in the list above.
NANDHU RAJ
INDEX
1.INTRODUCTION.........................................................................................1
1.1 WHAT IS CRUD...............................................................................1
1.2 WHAT IS JDBC................................................................................1
1.3 FUNDATMENTAL STEPS IN JDBC..............................................1
1.4 JDBC API...........................................................................................2
2.DAILY REPORT...........................................................................................3
2.1 FIRST DAY.......................................................................................3
2.2 SECOND DAY..................................................................................6
2.2.1 SOURCE CODE....................................................................7
2.2.2 OUTPUT..............................................................................21
2.3 THIRD DAY....................................................................................25
3.CONCLUSION...........................................................................................26
4.CERTIFICATE...........................................................................................27
1. INTRODUCTION
WHAT IS CRUD?
CRUD is an acronym for CREATE, READ, UPDATE, and DELETE which are basic
functions of persistent storage. CRUD operations can use forms or an interface view to
retrieve and return data from a database.
WHAT IS JDBC?
Java Database Connectivity or JDBC API provides industry-standard and database-
independent connectivity between the Java applications and relational database servers
(relational databases, spreadsheets, and flat files). To keep it simple, JDBC allows a
Java application to connect to a relational database. The major databases are supported
such as Oracle, Microsoft SQL Server, DB2 and many others .
The fundamental steps involved in the process of connecting to a database and executing
a query consist of the following:
JDBC API
• java.sql
• javax.sql
JDBC API consists of two parts – the first part is the JDBC API to be used by the
application programmers. The second part is the low-level API to connect to a database
server.
The first part of JDBC API is part of standard java packages in java.sql package. We
use java.sql package API for accessing and processing data stored in a data source (usually
a relational database) using the Java programming language.
For the second part is the JDBC driver(there are four different types of JDBC drivers) A
JDBC driver is a set of Java classes that implement the JDBC interfaces, targeting a
specific database. The JDBC interfaces come with standard Java, but the implementation
of these interfaces is specific to the database you need to connect to. Such an
implementation is called a JDBC driver.
Page |2
2. DAILY REPORT
FIRST DAY
The First Day was a fresh experience to us. We all were welcomed and greeted in a very
well manner. The class was really interesting. The class started with an introduction
about SPECTRUM SOFTTECH SOLUTIONS PVT.LTD by its Manager Mr. Jibu John
who is a IT and Health Care Training. Rest of the session was about CRUD operations
in Java conducted by the Android Developer Cum Trainer Mrs. Sreelakshmi Sajeevan.
The CRUD stands for Create, Read/Retrieve, Update and Delete. These are the four
basic functions of the persistence storage. CRUD operations can use forms or an
interface view to retrieve and return data from a database.
Procedures Function
CREATE This is a form view to create a new
record in the database.
Page |3
The following tools are used for the development:
4. MySQL Connector for Java:- MySQL provides connectivity for client applications
developed in the Java programming language with MySQL Connector/J, a driver that
implements the Java Database Connectivity (JDBC) API. MySQL Connector/J is a
JDBC Type 4 driver. Different versions are available that are compatible with the JDBC
3.0 and JDBC 4.
5. JDBC:- JDBC stands for Java Database Connectivity. JDBC is a Java API to connect
and execute the query with the database. It is a part of JavaSE (Java Standard Edition).
JDBC API uses JDBC drivers to connect with the database.
Page |4
6. SQLyog:- SQLyog is a powerful MySQL development and administration solution,
trusted by 2.5 million users around the world. With SQLyog, organizations have a tool
that enables database developers, administrators, and architects to visually compare,
optimize, and document schemas.
7. XAMPP:- XAMPP is a software distribution which provides the Apache web server,
MySQL database (actually MariaDB), PHP and Perl (as command-line executable and
Apache modules) all in one package. It is available for Windows, MAC and Linux
systems.
Page |5
SECOND DAY :
In the second day we were moved to the next step, designing. Here we know about
different control and their important properties that we used for designing.
Page |6
SOURCE CODE
HomePage.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home</title>
</head>
<body>
<h1>Add New Employee</h1>
<form action="SaveServlet" method="post">
<table>
<tr><td>Name:</td><td><input type="text" name="name"/></td></tr>
<tr><td>Password:</td><td><input type="password" name="password"/
></td></tr>
<tr><td>Email:</td><td><input type="email" name="email"/></td></tr>
<tr><td>Country:</td><td>
<select name="country" style="width:150px">
<option>India</option>
<option>USA</option>
<option>UK</option>
<option>Other</option>
</select>
</td></tr>
<tr><td colspan="2"><input type="submit" value="Save Employee"/></td>
</tr>
</table> </form>
Page |7
<br/>
<a href="ViewServlet">view employees</a>
</body>
</html>
Emp.java:
Page |8
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
EmpDao.java:
import java.util.*;
import java.sql.*;
public class EmpDao {
Page |9
}catch(Exception e){System.out.println(e);}
return con;
}
public static int save(Emp e){
int status=0;
try{
Connection con=EmpDao.getConnection();
PreparedStatement ps=con.prepareStatement(
"insert into user905(name,password,email,country) values (?,?,?,?)");
ps.setString(1,e.getName());
ps.setString(2,e.getPassword());
ps.setString(3,e.getEmail());
ps.setString(4,e.getCountry());
status=ps.executeUpdate();
con.close();
}catch(Exception ex){ex.printStackTrace();}
return status;
}
public static int update(Emp e){
int status=0;
try{
Connection con=EmpDao.getConnection();
PreparedStatement ps=con.prepareStatement(
"update user905 set name=?,password=?,email=?,country=? where
id=?");
P a g e | 10
ps.setString(1,e.getName());
ps.setString(2,e.getPassword());
ps.setString(3,e.getEmail());
ps.setString(4,e.getCountry());
ps.setInt(5,e.getId());
status=ps.executeUpdate();
con.close();
}catch(Exception ex){ex.printStackTrace();}
return status;
}
public static int delete(int id){
int status=0;
try{
Connection con=EmpDao.getConnection();
PreparedStatement ps=con.prepareStatement("delete from user905 where
id=?");
ps.setInt(1,id);
status=ps.executeUpdate();
con.close();
}catch(Exception e){e.printStackTrace();}
return status;
}
public static Emp getEmployeeById(int id){
P a g e | 11
Emp e=new Emp();
try{
Connection con=EmpDao.getConnection();
PreparedStatement ps=con.prepareStatement("select * from user905 where
id=?");
ps.setInt(1,id);
ResultSet rs=ps.executeQuery();
if(rs.next()){
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setPassword(rs.getString(3));
e.setEmail(rs.getString(4));
e.setCountry(rs.getString(5));
}
con.close();
}catch(Exception ex){ex.printStackTrace();}
return e;
}
public static List<Emp> getAllEmployees(){
List<Emp> list=new ArrayList<Emp>();
try{
Connection con=EmpDao.getConnection();
PreparedStatement ps=con.prepareStatement("select * from user905");
ResultSet rs=ps.executeQuery();
while(rs.next()){
P a g e | 12
Emp e=new Emp();
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setPassword(rs.getString(3));
e.setEmail(rs.getString(4));
e.setCountry(rs.getString(5));
list.add(e);
}
con.close();
}catch(Exception e){e.printStackTrace();}
return list;
}
}
After we create our Servlet classes, each servlet class for the Create user, Update
user, Read user and Delete user from the database. Here are the classes
SaveServlet.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/SaveServlet")
P a g e | 13
public class SaveServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String name=request.getParameter("name");
String password=request.getParameter("password");
String email=request.getParameter("email");
String country=request.getParameter("country");
int status=EmpDao.save(e);
if(status>0){
out.print("<p>Record saved successfully!</p>");
request.getRequestDispatcher("index.html").include(request, response);
}else{
out.println("Sorry! unable to save record");
}
out.close();
}
}
P a g e | 14
EditServlet.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/EditServlet")
public class EditServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<h1>Update Employee</h1>");
String sid=request.getParameter("id");
int id=Integer.parseInt(sid);
Emp e=EmpDao.getEmployeeById(id);
P a g e | 15
value='"+e.getPassword()+"'/>
</td></tr>");
out.print("<tr><td>Email:</td><td><input type='email' name='email'
value='"+e.getEmail()+"'/></td></tr>");
out.print("<tr><td>Country:</td><td>");
out.print("<select name='country' style='width:150px'>");
out.print("<option>India</option>");
out.print("<option>USA</option>");
out.print("<option>UK</option>");
out.print("<option>Other</option>");
out.print("</select>");
out.print("</td></tr>");
out.print("<tr><td colspan='2'><input type='submit' value='Edit & Save
'/></td></tr>");
out.print("</table>");
out.print("</form>");
out.close();
}
}
EditServelet2.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
P a g e | 16
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/EditServlet2")
public class EditServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String sid=request.getParameter("id");
int id=Integer.parseInt(sid);
String name=request.getParameter("name");
String password=request.getParameter("password");
String email=request.getParameter("email");
String country=request.getParameter("country");
int status=EmpDao.update(e);
if(status>0){
response.sendRedirect("ViewServlet");
}else{
P a g e | 17
out.println("Sorry! unable to update record");
}
out.close();
}
DeleteServelet.java:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sid=request.getParameter("id");
int id=Integer.parseInt(sid);
EmpDao.delete(id);
response.sendRedirect("ViewServlet");
}
}
P a g e | 18
ViewServlet.java:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ViewServlet")
public class ViewServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<a href='index.html'>Add New Employee</a>");
out.println("<h1>Employees List</h1>");
List<Emp> list=EmpDao.getAllEmployees();
out.close();
}
}
P a g e | 20
Output
First page will look like this, fill the form and submit it.
Figure 2.1
Figure 2.2
P a g e | 21
Click on the View Employees link to see the total employees list
Figure 2.3
Figure 2.4
P a g e | 22
After changing information, submit button. You will see that information is
changed
Figure 2.5
Figure 2.6
P a g e | 23
THIRD DAY:
The final day of the Industrial visit. We successfully perform the CRUD operations and
tested the web application using Apache Tomcat Server and XAMPP.
A viva examination was conducted based on the topics that we are taught. It was led by
Sreelakshmi Sajeevan.
P a g e | 24
3. CONCLUSION
P a g e | 25
P a g e | 26