using insert and delete in a single page in jsp

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 jsp
using 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 page
insert , 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
How to insert or delete records in MS access database using jsp - JSP-Servlet
How to insert or delete records in MS access database using jsp  Hi friends please provide me a solution that i insert or delete record from a database using java server pages. I used the microsoft access 2003 database. PlZ
How to insert a single page PDF file into another multipage PDF file using iText? - Java Beginners
How to insert a single page PDF file into another multipage PDF file using.... PDF-B.pdf is a single page PDF file. I want to insert PDF-B into PDF-A only after every odd page. Any idea? Thanks
Using insert update and delete in the same servlet
Using insert update and delete in the same servlet  How to write insert, update and delete coding in the same servlet
how to insert multiple columns of a single row into my sql database using jsp
how to insert multiple columns of a single row into my sql database using jsp  hi sir, how to insert multiple columns of a single row into my sql database using jsp. when i click ADD ROW,rows are added.when i click submit
upload file and insert other details to database using a single form
upload file and insert other details to database using a single form   hi.. I have problem with uploading a file and insert other user datas together which I retrieved from another jsp/html page. Here i was able to upload file
Uploading a single file by using JSp
Uploading 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 JSp
Uploading 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 & retrive
how 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
single query to insert the data
single query to insert the data  How to insert data for all HTML fields in single MYSQL query
Delete and edit data in xml file using JSP
Delete 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-Servlet
multiple 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
insert and delete a row programmatically
insert and delete a row programmatically  How to insert and delete a row programmatically ? (new feature in JDBC 2.0
how to insert checkbox value into database using jsp
how 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+jsp
how 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 jsp
how to insert checkbox value into database using jsp  how to insert check box value to the database using jsp my code is <link href="font&colors.css" rel="stylesheet" type="text/css"> <p></p>
how to insert checkbox value into database using jsp
how 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
How to insert multiple drop down list data in single column in sql database using servlet
How to insert multiple drop down list data in single column in sql database using servlet  i want to insert date of birth of user by using separate drop down list box for year,month and day into dateofbirth column in sql server
how to display image and text in single jsp page from the mysql database
how to display image and text in single jsp page from the mysql database  hello please help me to display the image and text in single jsp page from mysql database if have any reference code please send me Thanks in advance
how insert multiple images in mysql database with use of struts 1.3.8 or java method, with single bean,or using array
how insert multiple images in mysql database with use of struts 1.3.8 or java method, with single bean,or using array  i am using netbeans 7.0 ,with struts 1.3.8 and i want to insert multiple images in mysql database ,with use
insert name city and upload image in database using mysql and jsp
insert name city and upload image in database using mysql and jsp   insert name city and upload image in database using mysql and jsp
insert and delete data in database
insert 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 to insert data from a combobox and textbox values into DB using JSP?
How to insert data from a combobox and textbox values into DB using JSP?  hi, How to insert a comb-box and a text box values in to DB using JSP? @DB:student; @table:stu_info; Combobox values:(class1,class2,class3); textbox1
create,edit and delete in JSP using struts and SQL server2005 as database in jdeveloper?
create,edit and delete in JSP using struts and SQL server2005 as database in jdeveloper?  I have a project ie create an application for users to create,edit and delete tasks: taskid, taskname, date, project in JSP and struts
create,edit and delete in JSP using struts and SQL server2005 as database in jdeveloper?
create,edit and delete in JSP using struts and SQL server2005 as database... to create,edit and delete tasks: taskid, taskname, date, project in JSP and struts... example using struts.if anyone knows how to do it..please help me.. thanks a lot
insert name city image in database using mysql and jsp
insert name city image in database using mysql and jsp  how to insert name ,city and image in database in mysql and jsp   Here is an example in jsp that insert name, city and image to database. <%@ page import
fetch and insert multiple rows into mysql database using jsp servlet
fetch and insert multiple rows into mysql database using jsp servlet  ... a problem to insert multiple rows into database using a single insert query here is the code of both jsp and servlet <%@page contentType="text/html
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
Insert value of dynamic generated text box in jsp using javascript
Insert value of dynamic generated text box in jsp using javascript  hello sir , i want to generate dynamic text box at run time and i did that using... reloading the page what i want is i can insert the multiple columns value generated
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 jsp
how 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 Code
Insert 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
how to insert the bulk data into the data base from the table of jsp page to another jsp page
how to insert the bulk data into the data base from the table of jsp page to another jsp page  pls help i'm doing the project called centralized... to get values in array in next jsp page and insert into the row by row Please do
include a delete option in every row of table in a JSP page
include a delete option in every row of table in a JSP page  I have the following code of a JSP page........... <blockquote> <p>&lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> &lt
delete jsp
delete 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 JSP
Insert 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
login page with mysql using jsp
login page with mysql using jsp  pls i need a sample of login page to check username and password in mysql database. thanks
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
User Registration Form Using JSP(JspBeans) after that how i can insert in database
User Registration Form Using JSP(JspBeans) after that how i can insert in database   User Registration Form Using JSP(JspBeans) after that how i can insert in database
insert code jsp to access
insert code jsp to access   insert code jsp to access
Insert data in mysql database through jsp using prepared statement
Insert data in mysql database through jsp using prepared statement...; This is detailed jsp code that how to insert data into database by using prepared statement...*" %>  <HTML> <HEAD> <TITLE>insert data using
Register page using servlets,jsp and java beans
Register 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
How to insert data into databse by JSP form registration page in netbeans
How to insert data into databse by JSP form registration page in netbeans  ... a student. I have been given a task to create registration page with username &... of data for login purpose. I have created 1 form in JSP netbeans with both username
delete an entry using JavaScript
delete an entry using JavaScript  How to delete an entry using JavaScript
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
Using javabeans to connect mySQL database on a jsp page - JSP-Interview Questions
...; Connecting a JSP page to MYSQL Database using JavaBean File Name: beancode.java   Connecting a JSP page to MYSQL Database using JavaBeanFile Name...Using javabeans to connect mySQL database on a jsp page  Hi, Am doing
How to design https connection page using JSP
How 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

Ads