jspjdbc

How to compare a query result with a Textbox value in jsp


I'm creating an app, that displays a String from a database. This String is then displayed in a textbox that cannot be edited(read only).

I now require that variable (ID passed) in the text box to be compared with a column in a SQL database table which is an identical String. Once the matching String is found I need to update other columns based on the queries.

When I use request.getParameter() method it is giving output as null.

I need it to be done purely in jsp.

<%@page import="java.sql.*"%>  
<%@page import="java.lang.*"%>  
<%@page import="java.io.*"%>  
<%@page language="Java"%>  

<html>  
<head>  
<script type="text/javascript">  
function move(){  
 document.getElementById('tgt1').value = document.getElementById('Allocation').value;  
 document.getElementById('Allocation').value="";  
 document.getElementById("Send").disabled=true;  
}  
function UnBlock()  
{  
    document.getElementById("tgt1").value="";  
    document.getElementById("Send").disabled=false;  
    document.getElementById("tgt2").style.display="block";  
    document.getElementById("Query_but").style.display="block";  
   }  
function UnBlock_Only(){  
    document.getElementById("Send").disabled=false;  
    document.getElementById("Query").disabled=true;  
}  
function Alloc_Insert()  
{  
    document.myform.action="Alloc_Insert.jsp";  
    document.myform.method="post";  
    document.myform.submit();  
}  
</script>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
<style type="text/css">  
body {  
    margin-top: 20px;  
    margin-left: 30px;  
}  
</style>  
</head>  
<body>  
    <form name="myform" method="post" action="Alloc_Insert.jsp">  
<%  
try {  
    String sessname=(String)session.getAttribute("myusername");  
    PreparedStatement ps,ps2=null;  
    ResultSet rs=null;  
    Statement st=null;  
    Connection con=null;  
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
    con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "tiger");  
    ps=con.prepareStatement("select DBID from Scope1 where specialist IS Null");  
    rs = ps.executeQuery();  
    if(rs.next())  
    {  
  %>  
<input type="text" name="Allocation" size="75" id="Allocation" value="<%=rs.getString("DBID")%>" readonly="readonly">  
<%  
}  
}  
catch(Exception e){  
out.println(e);  
}  
%>  
<a href="Alloc_Insert.jsp" value="Allocate" id="Send" name="Send" onclick="Clicked()">Allocate</a><br/>  
<br/><br/><br/><br/><br/><br/>  
<table>  
<tr>  
<input type="text" id="tgt1" size="100">  
<td><input type="button"value="Query" id="Query" onClic="UnBlock()"></td>  
<td><input type="button" value="Westlaw Verification" onClick="UnBlock_Only()"></td>  
</tr><br/>  
</table><textarea name="tgt2" id="tgt2" cols="30" rows="5" style="display:none"></textarea>  
<table><tr><td><a href="Insert.jsp" style="display: none" id="Query_but" onclick="Add_Query()">Submit Query</a></td></tr></table>  
<table><tr><td><input type="text" value="" id="demo" size="75"></td></tr></table>  
</form>  
 </body>  
</html>

And the jsp used to compare the data is as below

<%@page import="java.sql.PreparedStatement"%>  
<%@page import="java.sql.DriverManager"%>  
<%@page import="java.sql.DriverManager"%>  
<%@page import="java.sql.Connection"%>  
<%@page import="java.sql.ResultSet"%>  
<%@page import="java.sql.ResultSet"%>  
<%@page import="java.lang.String"%>  

<html>  
    <head>  
    </head>  
    <body>  
<%   
     try{  
            ResultSet rs=null;  
            String Add=request.getParameter("Allocation.text");  
            String user=(String) session.getAttribute("myusername");  
            Class.forName("oracle.jdbc.driver.OracleDriver");  
            java.util.Date today = new java.util.Date();  
            java.sql.Timestamp t= new java.sql.Timestamp(today.getTime());  
            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","tiger");  
            PreparedStatement ps=con.prepareStatement("Update Scope1 SET ALLOCATED=?, SPECIALIST=? WHERE DBID='"+Add+"'");  
            ps.setTimestamp(1, t);  
            ps.setString(2, user);  
            /*ps.setString(3, Add_U);*/  
            ps.executeUpdate();  
            out.println(user);  
            out.println(con);  
            out.println(t);  
            out.println(Add);  
            con.commit();  
             }  
        catch(Exception e)  
                       {  
            out.println(e);  
                       }  
%>  
    </body>  
</html> 

What can I do to fix this?


Solution

  • Are you are getting null for this statement

    String Add=request.getParameter("Allocation.text"); 
    

    If so change to the following and try.

     String Add=request.getParameter("Allocation"); 
    
    <input type="text" name="Allocation" size="75" 
    id="Allocation" value="<%=rs.getString("DBID")%>" 
    readonly="readonly"> 
    

    Regards

    Edit 1

    This line of code makes value of Allocation to null. Are you calling move() javascript function anywhere? document.getElementById('Allocation').value="";

    If so you should get the value from tgt1 because you are assigning Allocation value to tgt1 in this statement

        document.getElementById('tgt1').value = 
    document.getElementById('Allocation').value;  
    

    Edit 3

    if you want to navigate to your second page by clicking hyperlink, you could try as following

    <a href="Alloc_Insert.jsp?val=<%=rs.getString("DBID")%>" />
    

    and in your second jsp

    request.getParameter("val").