Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 2nd, 2004, 11:42 AM
Authorized User
 
Join Date: Jul 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic Query

I have a search code from Database. i want to display queries from Database with multiple option. how do i assign variable on both. for example
 String jsql= "Select * from UserList WHERE '"+abu+ "' = '"+userSearch+ "'";
the full code:

<%@ page contentType="text/html; charset=utf-8" import="java.sql.*, java.util.*" errorPage="error.jsp"%>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chemistry Search</title>
</head>

<body>
<br>
<%

//load the MS Access JDBC driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    //open a connection to the "chemistry_test" database
    String url="jdbc:odbc:chemistry_test";

    Connection testConn= DriverManager.getConnection(url,"test1","test1");
   String userSearch = request.getParameter("Pnumber") ;
    String buttonChoice = request.getParameter("searchChoice") ;
     //place query results in a ResultSet object
    //String jsql="Select * from UserList WHERE uName= '"+userSearch + "'" ;

  // String abu = request.getParameter("Pnumber") ;


String abu;

    if (buttonChoice == ("username"))
    abu = "uName";
else if(buttonChoice == ("userrole"))
     abu = "Role" ;
else if (buttonChoice == ("firstname"))
    abu = "Fname" ;



  String jsql= "Select * from UserList WHERE '"+abu+ "' = '"+userSearch+ "'";


    //create a statement object for sending SQL queries
    Statement stmt = testConn.createStatement();

    ResultSet rs = stmt.executeQuery(jsql);

%>
<br>

<form method="POST" action="ChemistryDataSearchVar.jsp">

    <table border="0" width="228" id="table1">
        <tr>
            <td colspan="2">Please choose</td>
        </tr>
        <tr>
            <td width="150"><input type="text" name="Pnumber" size="20"></td>
            <td><input type="submit" value="Search" name="B2"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="radio" value="username" checked name="searchChoice">user name
            <input type="radio" value="userrole" name="searchChoice">role
            <input type="radio" value="firstname" name="searchChoice">first name</td>
        </tr>

    </table>
</form>
<br>
<br>

<br>
<tt class="1">User Information</tt><br><br>
<table border="1" width="300" id="table1" cellspacing="0" cellpadding="4">
    <tr bgcolor="bcbcbc">
        <td width="30">First Name</td>
        <td width="50">Last Name</td>
        <td width="30">User Name</td>
        <td width="20">Password</td>
        <td width="30">Role</td>
        <td width="140">E-mail Address</td>

    </tr>
        //assign ResultSet's column 1 to a String variable
       <% while(rs.next()){
%>
    <tr>

        <td><%=rs.getString("Fname")%>&nbsp;</td>
        <td><%=rs.getString("Lname")%>&nbsp;</td>
        <td><%=rs.getString("Uname")%>&nbsp;</td>
        <td><%=rs.getString("pword")%>&nbsp;</td>
        <td><%=rs.getString("Role")%>&nbsp;</td>
        <td><%=rs.getString("email")%>&nbsp;</td>

    </tr>

    <%
       }
       //clean up all objects
         rs.close();

    %>
</body>

</html>
<%
   testConn.close();
%>


Please help.


 
Old August 2nd, 2004, 11:01 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

String jsql= "Select * from UserList WHERE "+abu+ " = '"+userSearch+"'";


Om Prakash
 
Old August 3rd, 2004, 09:54 AM
Authorized User
 
Join Date: Jul 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for the advice. Now it displays without any errors, but no query displays. Here is the code again with some more documentations.


<%@ page contentType="text/html; charset=utf-8" import="java.sql.*, java.util.*" errorPage="error.jsp"%>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chemistry Search</title>
</head>

<body>
<br>
<br>

<form method="POST" action="ChemistryDataSearchVar.jsp">

    <table border="0" width="270" id="table1">
        <tr>
            <td colspan="2">Please choose</td>
        </tr>
        <tr>
            <td width="150"><input type="text" name="Pnumber" size="20"></td>
            <td><input type="submit" value="Search" name="B2"></td>


        </tr>
        <tr>
            <td colspan="2"><input type="radio" value="username" checked name="searchChoice">user name
            <input type="radio" value="userrole" name="searchChoice">User role
            <input type="radio" value="firstname" name="searchChoice">first name</td>
        </tr>


</form>


</table>
<br>
<br>

<br>
<tt class="1">User Information</tt><br><br>
<table border="1" width="300" id="table1" cellspacing="0" cellpadding="4">
    <tr bgcolor="bcbcbc">
        <td width="30">First Name</td>
        <td width="50">Last Name</td>
        <td width="30">User Name</td>
        <td width="20">Password</td>
        <td width="30">Role</td>
        <td width="140">E-mail Address</td>

    </tr>

<%

   //load the MS Access JDBC driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    //open a connection to the "chemistry_test" database
    String url="jdbc:odbc:chemistry_test";

    Connection testConn= DriverManager.getConnection(url,"test1","test1");
    String userSearch = request.getParameter("Pnumber") ; //get data from the textbox
    String buttonChoice = request.getParameter("searchChoice") ; //get choice from the radio button

    String abu = null;

    if (buttonChoice == ("username"))
        abu = "uName"; //search by radio button user name DB Field Name: uName
        else if(buttonChoice == ("userrole"))
                 abu = "Role" ; //search by radio button user role DB Field Name: Role
        else if (buttonChoice == ("firstname"))
                abu = "Fname" ; //search by radio button user First name, DB Field Name: Fname


 String jsql= "Select *from UserList WHERE "+abu+" = '"+userSearch+"'";
System.out.println(jsql);



    //create a statement object for sending SQL queries
    Statement stmt = testConn.createStatement();

    ResultSet rs = stmt.executeQuery(jsql);

%>


     <% //assign ResultSet's column 1 to a String variable
         while(rs.next()){
%>
    <tr>

        <td><%=rs.getString("Fname")%>&nbsp;</td>
        <td><%=rs.getString("Lname")%>&nbsp;</td>
        <td><%=rs.getString("Uname")%>&nbsp;</td>
        <td><%=rs.getString("pword")%>&nbsp;</td>
        <td><%=rs.getString("Role")%>&nbsp;</td>
        <td><%=rs.getString("email")%>&nbsp;</td>

    </tr>

    <%
       }
       //clean up all objects
         rs.close();

    %>
</body>

</html>
<%
   testConn.close();
%>







uid uName pword Fname Lname Role email
72 admin admin Reza Mahmud Admin [email protected]
78 pet pet Pet Shop Pet Shop ShopOwner [email protected]

 
Old August 4th, 2004, 09:29 AM
Authorized User
 
Join Date: Jul 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Does anyone knows any good resources on dynamic SQL query

 
Old August 4th, 2004, 02:16 PM
Authorized User
 
Join Date: Jul 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I resolved my previous stated programming problem. So, please discard it. Thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Table Name in SQL Query drani C# 2 November 6th, 2007 05:47 PM
Dynamic Query Sum Colonel Angus SQL Server 2000 4 March 30th, 2005 11:08 AM
Dynamic Query ninel SQL Server 2000 1 March 8th, 2005 01:18 AM
Dynamic query problem drex10 SQL Server 2000 2 April 6th, 2004 11:55 PM
ADO Dynamic Query - Please help rudidoku Access 1 July 23rd, 2003 06:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.