You are on page 1of 24

JSP WITH MYSQL

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

<%

try

Class.forName("com.mysql.jdbc.Driver"); //load driver

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","admin");
//create connection

if(request.getParameter("btn_add")!=null) //check button click event not null

String name,owner;

name=request.getParameter("txt_name"); //txt_name

owner=request.getParameter("txt_owner"); //txt_owner

PreparedStatement pstmt=null; //create statement

1
pstmt=con.prepareStatement("insert into person(name,owner)values(?,?)"); //sql
insert query

pstmt.setString(1,name);

pstmt.setString(2,owner);

pstmt.executeUpdate(); //execute query

con.close(); //close connection

out.println("Insert Successfully...! Click Back link.");// after insert record


successfully message

catch(Exception e)

out.println(e);

%>

<html>

<head>

2
<title>JSP:Insert, Update, Delete using MySQL</title>

<!--css for div main class and table -->

<style type="text/css">

.main

width:700px;;

margin-left:250px;

padding: 10px;

border: 5px solid grey;

table

font-family: arial, sans-serif;

border-collapse: collapse;

width: 600px;

td

border: 5px solid silver;

3
text-align: left;

padding: 8px;

</style>

<!-- javascript for form validation-->

<script>

function validate()

var name = document.myform.txt_name;

var owner = document.myform.txt_owner;

if (name.value == "")

window.alert("please enter name ?");

name.focus();

return false;

if (owner.value == "")

window.alert("please enter owner ?");

owner.focus();

4
return false;

</script>

</head>

<body>

<div class="main">

<form method="post" name="myform" onsubmit="return validate();">

<center>

<h1>Insert Record</h1>

</center>

<table>

<tr>

<td>Name</td>

5
<td><input type="text" name="txt_name"></td>

</tr>

<tr>

<td>Owner</td>

<td><input type="text" name="txt_owner"></td>

</tr>

<tr>

<td><input type="submit" name="btn_add"


value="Insert"></td>

</tr>

</table>

<center>

<h1><a href="index.jsp">Back</a></h1>

</center>

</form>

</div>

6
<center>

</center>

</body>

</html>

ADD.JSP

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

<%

try

Class.forName("com.mysql.jdbc.Driver"); //load driver

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","admin");
//create connection

if(request.getParameter("btn_add")!=null) //check button click event not null

{
7
String name,owner;

name=request.getParameter("txt_name"); //txt_name

owner=request.getParameter("txt_owner"); //txt_owner

PreparedStatement pstmt=null; //create statement

pstmt=con.prepareStatement("insert into person(name,owner)values(?,?)"); //sql


insert query

pstmt.setString(1,name);

pstmt.setString(2,owner);

pstmt.executeUpdate(); //execute query

con.close(); //close connection

out.println("Insert Successfully...! Click Back link.");// after insert record


successfully message

catch(Exception e)

out.println(e);

8
}

%>

<html>

<head>

<title>JSP:Insert, Update, Delete using MySQL</title>

<!--css for div main class and table -->

<style type="text/css">

.main

width:700px;;

margin-left:250px;

padding: 10px;

border: 5px solid grey;

table

9
font-family: arial, sans-serif;

border-collapse: collapse;

width: 600px;

td

border: 5px solid silver;

text-align: left;

padding: 8px;

</style>

<!-- javascript for form validation-->

<script>

function validate()

var name = document.myform.txt_name;

var owner = document.myform.txt_owner;

if (name.value == "")

window.alert("please enter name ?");

10
name.focus();

return false;

if (owner.value == "")

window.alert("please enter owner ?");

owner.focus();

return false;

</script>

</head>

<body>

<div class="main">

<form method="post" name="myform" onsubmit="return validate();">

<center>

<h1>Insert Record</h1>

11
</center>

<table>

<tr>

<td>Name</td>

<td><input type="text" name="txt_name"></td>

</tr>

<tr>

<td>Owner</td>

<td><input type="text" name="txt_owner"></td>

</tr>

<tr>

<td><input type="submit" name="btn_add"


value="Insert"></td>

</tr>

</table>

<center>

12
<h1><a href="index.jsp">Back</a></h1>

</center>

</form>

</div>

<center>

</center>

</body>

</html>

UPDATE.JSP

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

<%

try

Class.forName("com.mysql.jdbc.Driver"); //load driver

13
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","admin"); //
create connection

if(request.getParameter("btn_update")!=null) //check button click event not null

int hide;

String name_up,owner_up;

hide=Integer.parseInt(request.getParameter("txt_hide")); //it is hidden id get for


update record

name_up=request.getParameter("txt_name"); //txt_name

owner_up=request.getParameter("txt_owner"); //txt_owner

PreparedStatement pstmt=null; //create statement

pstmt=con.prepareStatement("update person set name=?, owner=? where id=?");


//sql update query

pstmt.setString(1,name_up);

pstmt.setString(2,owner_up);

pstmt.setInt(3,hide);

pstmt.executeUpdate(); //execute query

14
con.close(); //connection close

out.println("Update Successfully...! Click Back link."); //after update record


successfully message

catch(Exception e)

out.println(e);

%>

<html>

<head>

<title>JSP:Insert, Update, Delete using MySQL</title>

<!--css for div main class and table-->

<style type="text/css">

15
.main

width:700px;;

margin-left:250px;

padding: 10px;

border: 5px solid grey;

table

font-family: arial, sans-serif;

border-collapse: collapse;

width: 600px;

td

border: 5px solid silver;

text-align: left;

padding: 8px;

</style>

<!-- javascript for form validation-->

16
<script>

function validate()

var name = document.myform.txt_name;

var owner = document.myform.txt_owner;

if (name.value == "")

window.alert("please enter name ?");

name.focus();

return false;

if (owner.value == "")

window.alert("please enter owner ?");

owner.focus();

return false;

</script>

17
</head>

<body>

<div class="main">

<form method="post" name="myform" onsubmit="return validate();">

<center>

<h1>Update Record</h1>

</center>

<table>

<%

try

Class.forName("com.mysql.jdbc.Driver"); //load driver

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","admin"); //
create connection

if(request.getParameter("edit")!=null)

18
int id=Integer.parseInt(request.getParameter("edit"));

String name,owner;

PreparedStatement pstmt=null; // create statement

pstmt=con.prepareStatement("select * from person where id=?"); //


sql select query

pstmt.setInt(1,id);

ResultSet rs=pstmt.executeQuery(); // execute query store in


resultset object rs.

while(rs.next())

id=rs.getInt(1);

name=rs.getString(2);

owner=rs.getString(3);

%>

<tr>

<td>Name</td>

<td><input type="text" name="txt_name"


value="<%=name%>"></td>

</tr>

19
<tr>

<td>Owner</td>

<td><input type="text" name="txt_owner"


value="<%=owner%>"></td>

</tr>

<tr>

<td><input type="submit" name="btn_update"


value="Update"></td>

</tr>

<input type="hidden" name="txt_hide" value="<%=id%>">

<%

catch(Exception e)

out.println(e);

%>

</table>

<center>

20
<h1><a href="index.jsp">Back</a></h1>

</center>

</form>

</div>

<center>

<h2><a href="">tutorial link</a></h2>

</center>

</body>

</html>

DATABASE
CREATE TABLE `person` (

`id` int(11) NOT NULL,

`name` varchar(15) NOT NULL,

`owner` varchar(20) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--

-- Dumping data for table `person`

21
--

INSERT INTO `person` (`id`, `name`, `owner`) VALUES

(2, 'Syed', 'Apple'),

(3, 'HeroBala', 'Microsoft'),

(4, 'Noor Fathima', 'Facebook'),

(5, 'Kumar', 'Google');

--

-- Indexes for dumped tables

--

--

-- Indexes for table `person`

--

ALTER TABLE `person`

ADD PRIMARY KEY (`id`);

ADD LIBRARY

22
23
24