i am having problem in this code, i have written //ERROR, where i am getting error....
PLEASE HELP
BASICALLUY, WHAT I AM DOING IS:-
I HAVE A GENERATE AND OK BUTTON, WHENEVER I CLICK GENERATE BUTTON, A NEW SET OF INFO IS RETRIEVED FROM THE DATABASE AND THE TEXT FIELDS ARE FILLED ACCORDINGLY, BUT THATS NOT HAPPENING :-(
PLEASE PLEASE PLEASE HELP......

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

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script type="text/javascript">
            
            function pat()
            {
    <%    response.sendRedirect("#");
    
        java.util.Date date=new java.util.Date();
        String format="mm-dd-yyyy";
        SimpleDateFormat sdf=new SimpleDateFormat();
        String d=sdf.format(date);

     String nm;
         int ag;
         String gen;
         String des;

    try
            {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
    catch(Exception e)
    {
        
    }
        Connection con=DriverManager.getConnection("jdbc:odbc:hospdsn");


        PreparedStatement ps=con.prepareStatement("select pname,age,gender,dscrptn from patient where docref=? and curdate=? and meet=? LIMIT 1");
        ps.setString(1, (String)session.getAttribute("name"));
        ps.setString(2,d);
        ps.setString(3,"no");
        ResultSet rs=ps.executeQuery();
        rs.next();

         nm=rs.getString("pname");
         ag=rs.getInt(2);
         gen=rs.getString(3);
         des=rs.getString(4);

          %>
    }
        </script>
    </head>
    <body bgcolor="black"><center>
            Hello <%= session.getAttribute("name") %>
            <form action="pres.jsp">
                <%
  /*               String nm;
         int ag;
         String gen;
         String des;
 java.util.Date date=new java.util.Date();
        String format="mm-dd-yyyy";
        SimpleDateFormat sdf=new SimpleDateFormat();
        String d=sdf.format(date);

    try
            {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
    catch(Exception e)
    {
    }
        Connection con=DriverManager.getConnection("jdbc:odbc:hospdsn");
       

        PreparedStatement ps=con.prepareStatement("select pname,age,gender,dscrptn from patient where docref=? and curdate=? and meet=? LIMIT 1");             
                                                            
                                                                        //ERROR


        ps.setString(1, (String)session.getAttribute("name"));
        ps.setString(2,d);
        ps.setString(3,"no");
        ResultSet rs=ps.executeQuery();
        rs.next();

         nm=rs.getString(1);
         ag=rs.getInt(2);
         gen=rs.getString(3);
         des=rs.getString(4);

 */
                %>


    <h1><b><font color="red"><u>PATIENT'S INFORMATION</u></font></b></h1></center>
        <table align=center border=0 bgcolor="black" cellspacing=2 cellpadding=2>
    
            <br><br><tr><td><b><font color="red">Patient's Name:</font></b></td><td><input type="text" name="a1" value="<%=nm %>"></td></tr>
<br>
<tr><td><b><font color="red">Gender:</font></b></td><td><input type="text" name="gender" value="<%=gen%>">
</td></tr>
<tr><td><b><font color="red">Age:</font></b></td><td><input type="password" name="a2" value="<%=ag%>"></td></tr>
<tr><td><b><font color="red">Description:</font></b></td><td><textarea name="Description" cols=30 rows=5 ><%=des%></textarea></td></tr>
<tr><td></td><td>
    <br>
    <input type="submit" value="OK" ></td><td><input type="button" value="Generate" onclick="pat()"></tr>

        </table>
            </form>
        
    </body>
</html>

Recommended Answers

All 6 Replies

1 class -> 2 methods.
1 method that inserts
1 method that reads.
Call those methods from the jsp.
Create a class that saves the data as attributes.

The rest of the code is wrong. The java is executed at the server. The javascript at the client. So when you click the pat function, it will not execute the java code that is inside it. That code has already been executed, when the page loaded.
If you want to execute java code, you need to submit the page, execute the java, take the values and display them. You can use javascript to submit the form though.
You can put the "Generate" button in its own little form and submit it to itself.

i got ur logic.......... but can u please elaborate it for me, i really need ur help...
i've made some projects in j2se... but this is my first project in j2ee....
do i need to use bean???

When the page loads or gets submitted the java code executes. After the code executes the html gets displayed. If you want to execute java code when you click a button you need to submit to the page where the code is. If you want that page to be the same then submit to the same page, but make sure that the code gets executed only when you submit. So here is a small example:

... html code

<%
String submitted = request.getParameter("submitted"); // the first time the page loads this is null:

String name = "";

if (submitted!=null) { // if you submitted to this page then this is not null

  // create an instance of your class
  // call its methods
  // take the data from the DB
  name = "some data";
}
%>

<form action="pres.jsp">
  ..
  <td>Name: </td>
  <td><%=name%></td>
  ..
</form>

<form action="">
  <input type="submit" value="Generate" >
  <input type="hidden" value="1" name="submitted" >
</form>

When action is empty it submits to the same page.

I would advise you to create a custom class with attributes the columns of the table and have your method that reads from the DB return that

In general I would advise you to change your logic. I don't know what you are trying to accomplish, but it would be better if you had one page where the user can insert data to the database and another where the user can view the data.
Maybe you can add input fields that get submitted to a page and then use those to call java methods and display the results.

At the above example the "name" is empty and you display it. If you submit the page, the if statements executes, the name takes value and then again gets displayed.

commented: superb programmer and logic builder. +2

@javaAddict,
well i'll try this code, but seriously thank you so much, you changed my perspective of coding this page, otherwise i would have been executing it again and again with errors.
Thank you. i'll get back to you if i get any errors again.

@javaAddict,
whatever u said i did that and thanx my program is running now.
But another problem occured now.
i have 4 pages:-
1)Login
2)orthodoc
3)reception
4)admin
When the user is a doctor, i've used a jsp:forward tag to forward to the page orthodoc.jsp.
When i click on that "GENERATE " button i get this error:

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause

java.lang.NullPointerException
	org.apache.jsp.patient_jsp._jspService(patient_jsp.java:84)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

It is running if i directly run the orthodoc page, but not when i login and forward to orthodoc page.

PLEASE HELP, PLEASE................

You need to post the code of that page. Something is null in the page when it loads.
Probably there is some variable at the request that you don't pass, when you redirect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.