DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   JSP (http://www.daniweb.com/forums/forum24.html)
-   -   I can't insert data into database (http://www.daniweb.com/forums/thread196346.html)

vandana88 Jun 7th, 2009 2:51 pm
I can't insert data into database
 
Hi I have a difficulty in insertion data. I don't get any error message but it doesn't insert data to database. My code is below. Where am I wrong?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <HR noshade size="5" width="50%" align="center">               
    <title>ZONE</title>
    </head>
    <body bgcolor="Silver">
   
    <%@ page language="java" import = "java.sql.*,java.util.*,java.text.*,java.io.*" %>
  <CENTER>
    <h4><font size="4" face="Verdana">ZONE MASTER</font> </h4>
    <BR></BR>
    <h4><font size="2" face="Verdana">ZONE CODE <input type="text" name="ZONE_CODE" value="" width="06" /> </h4></font>
    <h4><font size="2" face="Verdana">ZONE NAME <input type="text" name="ZONE_NAME" value="" width="06" /> </h4></font>
    </CENTER>
   
   
    <%
                 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:oraclexe","hr","hr");
     
    String zone_code=request.getParameter("ZONE_CODE");
    String zone_name=request.getParameter("ZONE_NAME");
   
    if(request.getParameter("INSERT") != null) {
            try{
    String ins=  "insert into ni_zone_master (zone_code,zone_name  ) values ('"+zone_code+"', '"+zone_name+"')";
 
            PreparedStatement st1 = con.prepareStatement(ins);
          st1.executeUpdate();
           
          con.close();
        }
        catch(SQLException sqle)
        {
                out.println("ERROR IN PAGE"+sqle);
        } 
    }
    if(request.getParameter("UPDATE") != null) {
              try{
    String ins=  "UPDATE ni_zone_master SET zone_code = '"+zone_code+"', zone_name = '"+zone_name+"' WHERE zone_code = '"+zone_code+"' AND zone_name = '"+zone_name+"' ";
 
          PreparedStatement st1 = con.prepareStatement(ins);
          st1.executeUpdate();
     
                 
                con.close();
        }
        catch(SQLException sqle)
        {
                out.println("ERROR IN PAGE"+sqle);
        } 
    }
    if(request.getParameter("DELETE") != null)
              try{
    String ins =  "DELETE FROM ni_zone_master WHERE zone_code = '"+zone_code+"' AND zone_name = '"+zone_name+"'";
 
          PreparedStatement st1 = con.prepareStatement(ins);
            st1.executeUpdate();
              con.close();
        }
        catch(SQLException sqle)
        {
                out.println("ERROR IN PAGE"+sqle);
        } 
 
    %>
 
    <CENTER>
    <form name="INSERT" method="POST">
    <input type="submit" value="ADD" name="ADD" onclick="InsertZoneInfo()"/></form>
    <form name="CHANGE" method="POST"> 
    <input type="submit" value="UPDATE" name="UPDATE" onclick="UpdateZoneInfo()"/></form>
    <form name="DELETE" method="POST"> 
    <input type="submit" value="DELETE" name="DELETE" onclick="DeleteZoneInfo()"/></form>
    </CENTER>
    </body>
    <HR noshade size="5" width="50%" align="center"> 
</html>

peter_budo Jun 7th, 2009 3:40 pm
Re: I can't insert data into database
 
You posted in wrong section, check out our Web Development for JSP section (I already made request for post to be moved)
Also in mean time you can have look at this post JSP database connectivity according to Model View Controller (MVC) Model 2

vandana88 Jun 9th, 2009 12:49 pm
Re: I can't insert data into database
 
i'm so sorry as i was very new to this community and now what should i do shall i remove this thread and paste it somewhere else?
and plz do solve my problem. Is my code correct or i should do any modifications?It's very urgent......thanks in advance

stephen84s Jun 10th, 2009 3:28 am
Re: I can't insert data into database
 
Well syntactically your JSP page is correct, although the approach of firing queries directly from inside the JSP page (that too using SQL Statements constructed on the fly) is not only bad but dangerous.

Also you need some brushing up on your concepts of HTML and Javascript. The <input> tags need to be nested inside a corresponding <form> tag, if you want the data from the <input> tag to be posted to the JSP page mentioned in the "action" attribute of <form> tag.
Also I do not see any Javascript functions by the name of InsertZoneInfo(),UpdateZoneInfo() or DeleteZoneInfo()

If you are using Firefox you could go to Tools ->Error Console to see the various Javascript errors or alternatively you could install the Firebug extension for more comprehensive debugging info.
Anyways the situation as I see demands that you learn not only about JSP and MVC[1,2,3] but also about HTML and JavaScript right now.

CoSIS1 Jun 29th, 2009 4:19 am
Re: I can't insert data into database
 
double check on database schema and tables and columns

are they exactly what you have in the code?

irfannagaria Jul 2nd, 2009 9:09 am
Re: I can't insert data into database
 
You can make two changes:
1: from oracle database homepage login as administrator then unlock the hr (may be it work)
2: try this url: to
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");

hope this will works.........

bye


All times are GMT -4. The time now is 10:24 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC