using insert and delete in a single page in jsp
I am using the following code in jsp to declare two javascript functions to insert and delete in a single jsp page but insert function doesn't works only delete function works even if the insert button is clicked.. So what is wrong with the code?.. If there is some other way than please tell me.
function insert()
{
<%
Connection con = null;
cat=request.getParameter("cat");
code=request.getParameter("code");
det=request.getParameter("det");
charge=request.getParameter("charge");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
con = DriverManager.getConnection("jdbc:odbc:Hospital","Santosh"," ");
PreparedStatement psadd=null;
String sqladdRecord=null;
try
{
sqladdRecord="insert into wards values(?,?,?,?)";
psadd=con.prepareStatement(sqladdRecord);
psadd.setString(1,cat);
psadd.setString(2,code);
psadd.setString(3,det);
psadd.setString(4,charge);
psadd.executeUpdate();
}
catch(Exception e)
{
response.sendRedirect("Wards.jsp");
}
try{
if(psadd!=null)
{
psadd.close();
}
if(con!=null)
{
con.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
function Delete()
{
<%
Connection conn = null;
cat=request.getParameter("cat");
code=request.getParameter("code");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
con = DriverManager.getConnection("jdbc:odbc:Hospital","Santosh"," ");
PreparedStatement psdel=null;
String sqldelRecord=null;
try
{
sqldelRecord="delete from wards where category='"+cat+"' and code='"+code+"'";
psdel=con.prepareStatement(sqldelRecord);
psdel.executeUpdate();
}
catch(Exception e)
{
response.sendRedirect("Wards.jsp");
}
try{
if(psdel!=null)
{
psdel.close();
}
if(conn!=null)
{
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
View Answers
February 18, 2011 at 11:15 AM
JSP Insert and Delete in single page
1)application.jsp:
<%@ page import="java.sql.*" %>
<html>
<head>
<script language="javascript">
function editRecord(id){
var f=document.form;
f.method="post";
f.action='edit.jsp?id='+id;
f.submit();
}
function deleteRecord(id){
var f=document.form;
f.method="post";
f.action='delete.jsp?id='+id;
f.submit();
}
</script>
</head>
<body>
<br><br>
<form method="post" name="form">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
<%
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String userName ="root";
String password="root";
int sumcount=0;
Statement st;
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db,userName,password);
String query = "select * from employee";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
%>
<%
while(rs.next()){
%>
<tr><td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=rs.getString(1)%>);" ></td>
<td><input type="button" name="delete" value="Delete" style="background-color:#ff0000;font-weight:bold;color:#ffffff;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>
<%
}
catch(Exception e){
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>
2)edit.jsp:
<%@page language="java"%>
<%@page import="java.sql.*"%>
<form method="post" action="update.jsp">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String query = "select * from employee where id='"+no+"'";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()){
%>
<tr>
<td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
<td><input type="text" name="address" value="<%=rs.getString("address")%>"></td>
<td><input type="text" name="contact" value="<%=rs.getInt("contactNo")%>"></td>
<td><input type="text" name="email" value="<%=rs.getString("email")%>"></td>
<td><input type="hidden" name="id" value="<%=rs.getString(1)%>"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td>
</tr>
<%
}
}
catch(Exception e){}
%>
</table>
</form>
</form>
February 18, 2011 at 11:16 AM
continue..
3)update.jsp:
<%@page import="java.sql.*"%>
<%
String ide=request.getParameter("id");
int num=Integer.parseInt(ide);
String name=request.getParameter("name");
String address=request.getParameter("address");
int contact=Integer.parseInt(request.getParameter("contact"));
String email=request.getParameter("email");
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
Statement st=null;
st=conn.createStatement();
st.executeUpdate("update employee set name='"+name+"',address='"+address+"',contactNo="+contact+",email='"+email+"' where id='"+num+"'");
response.sendRedirect("/examples/jsp/application.jsp");
}
catch(Exception e){
System.out.println(e);
}
%>
4)delete.jsp:
<%@page import="java.sql.*"%>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = conn.createStatement();
st.executeUpdate("DELETE FROM employee WHERE id = '"+no+"'");
response.sendRedirect("application.jsp");
}
catch(Exception e){}
%>
Related Tutorials/Questions & Answers:
using insert and delete in a single page in jspusing insert and
delete in a
single page in jsp I am
using... in a
single jsp page but
insert function doesn't works only
delete function works even...
Insert and
Delete in
single page
1)application.jsp:
<%@
page import="java.sql.
insert , edit , and delete button in one jsp pageinsert , edit , and
delete button in one
jsp page hello
I want to ask about the way of creating a
jsp page contains
insert , edit , and
delete buttons and manipulate data in database directly.
any help please or hints
Advertisements
Uploading a single file by using JSpUploading a
single file by
using JSp u have said about submit button...;%@
page language="java" %>
<HTML>
<FORM ENCTYPE="multipart/form-data...;table>
</center>
</FORM>
</HTML>
2)upload.jsp:
<%@
page Uploading a single file by using JSpUploading a
single file by
using JSp u have said about submit button..but in program u have not used submit button..and where file will be stored..where should we specify the output folder name..
Visit Here
how to insert data into database using jsp & retrivehow to
insert data into database
using jsp & retrive Hello,
I have created 1 html
page which contain username, password & submit button. in my...; password is correct or not...if correct then it goes to another
page Delete and edit data in xml file using JSPDelete and edit data in xml file
using JSP I want to know how to
delete and edit data from an XML file by use of
JSP.
I have XML file having tasks... in the xml file,I want to
delete and edit some tasks
using task id then how can i do
multiple dropdowns in single page - JSP-Servletmultiple dropdowns in
single page i have a
jsp page having drop down... should be upadated in the database
intially for viewing the request iam
using the below servlet
my
jsp
Untitled Document
jsp page authentication panel using jsp/servlet?jsp page authentication panel
using jsp/servlet? I have 10
jsp jsp forms and 7 users and i want to grant variour permission like edit,
delete
and save for users dynamically on forms.So please refer me code
how to insert checkbox value into database using jsphow to
insert checkbox value into database
using jsp How to
insert check box value to the oracle database
using jsp?
I want to create hotel's...;
Here is a simple
jsp code that
insert the selected checkbox values
how to insert data in database using html+jsphow to
insert data in database
using html+jsp anyone know what...";
// declare a connection by
using Connection interface...").newInstance();
/* Create a connection by
using getConnection() method
how to insert checkbox value into database using jsphow to
insert checkbox value into database
using jsp How to
insert check box value to the oracle database
using jsp?
I want to create hotel's package. where the admin will select the activities to
insert in the PACKAGE table. I
insert and delete data in databaseinsert and
delete data in database
insert and
delete data in database from servlets through JDBC
Hi Friend,
Please visit the following links:ADS_TO_REPLACE_1
Insert Data
Delete Data
ThanksADS_TO_REPLACE_2
How we delete a data of database from front end jsp page How we
delete a data of database from front end
jsp page I make a website and featch a data from data base and now i want that a
delete button put... deleted from
jsp page as well as from database.I used mysql and
jsp. Please help me
Uploading Single File by Using JSP Uploading
Single File by
Using JSP
... to understand how you can upload a file by
using the
Jsp.
As
Jsp is mainly used for the presentation logic, we should avoid to write a
code in the
jsp page how to insert array data into sql server using jsphow to
insert array data into sql server
using jsp hello,
i have problem to
insert array data into my sql server from
jsp form. beloW is my code... from form, but not
insert into my sql database.
try {
//String
Insert a row in 'Mysql' table using JSP CodeInsert a row in 'Mysql' table
using JSP Code
In this section, we will discuss about how to
insert data in Mysql database
using JSP code.
Query...
between your Tomcat server & Mysql database Table.
Code to
insert row in Mysql
delete jspdelete jsp <%@
page language="java" contentType="text/html...;/Controller">
<input type="hidden" name="
page" value="
delete"/>
<...; charset=ISO-8859-1">
<title>
Delete Student</title>
</head>
Insert Blob(Image) in Mysql table using JSPInsert Blob(Image) in Mysql table
using JSP
In this Section, we will
insert blob data(image) in Mysql database table
using JSP code.
A Blob stores a binary...
bits).
EXAMPLE :ADS_TO_REPLACE_2
insertblob.jsp
<%@
page Uploading Single File by Using JSP Uploading
Single File by
Using JSP
... to understand how you can upload a file by
using the
Jsp.
As
Jsp is mainly used for the presentation logic, we should avoid to write a
code in the
jsp page Register page using servlets,jsp and java beansRegister
page using servlets,
jsp and java beans i want code for register
page using jsp,serlets and java beans.iam getting error for my code in java...://www.roseindia.net/
jsp/user-registration-form-
using-jsp.shtml
Thanks
code in single jsp - JSP-Servlet in
single jsp it would be useful
the code which u gave is
1)click.jsp...code in
single jsp hi the code is good but for my application i need to click a link and it has to go to the specified
page and also count
How to design https connection page using JSPHow to design https connection
page using JSP Hi,
I have a project in which a
page requires https secure design to process the payment. Since... a secure
page. Please kindly provide me a solution on how to design a https
page using login page using jsp servlrt with mysql database?login
page using jsp servlrt with mysql database? Description... table login.
After successfully login
user1 see only index
page,if user2 login then he see only visiter
page and so on.
please gine me any suggession.
Thanks