SQL LIKE statement in JSP
I tried to write a query in jsp page using LIKE statement. However, Apache Tomcat report back an error in the query string. I tried to run the same query style in MS Access and it works. Can someone please tell me where is the error in jsp code:
JSP Code:
sql = "SELECT tblStudent.firstName, tblStudent.lastName, tblStudent.studentID FROM tblStudent WHERE (((tblStudent.firstName) LIKE '%" + g_firstNameInput + "%')) ORDER BY tblStudent.firstName";
MS Acccess:
SELECT tblStudent.firstName, tblStudent.lastName, *
FROM tblStudent
WHERE (((tblStudent.firstName) Like '*e*'))
ORDER BY tblStudent.firstName;
PS: I tried changing from '%' to '*' already, but still does not work
Thanks
Don't use JSP for SQL querries... to complicated.
1) Write a Java application that does what you want (as far as interacting with the database
2) Isolate the database access code into its own class/classes
3) Double check that you can then use those isolated classes inside a command line application
4) Adapt the code you used in 3) to use those same classes from a JSP - preferrably using Java Beans and DTOs
Similar Messages
-
Complex query using 'sql like' statement.
Given the following basic class structure.
public class Project {
private String name;
private Architecture architecture;
private Resources resources;
public Architecture getArchitecture();
public void setArchitecture(Architecture architecture);
public Resources getResources();
public void setResources();
public String getName();
public void setName(String name);
public Architecture {
private String name;
public String getName();
public void setName(String name);
public Resources {
private String name;
public String getName();
public void setName(String name);
we want to be able to do a query like:
find all Projects where (Project.name.indexOf("a") > 0) &&
(Project.architecture.name.indexof("b") > 0)
In essence a "like" comparison across String properties of the JDO objects.
Is it supported in KODO?? Anyone tried it out? Also, is it possible to
generate SQL type query in KODO?
rgds
NKNote that this relies on a current bug in Kodo -- we should escape out the
'%' character, but we don't.
Some day, we'll fix that bug. But, before then, we will introduce a proper,
supported mechanism for finding records that have a string field that
contains some substring.
-Patrick
On 5/30/02 6:12 PM, "Andrew" <[email protected]> wrote:
Hi,
I wanted to perform a LIKE statement to get records where the partial string
existed in a field -> eg. select a where a.field LIKE "SPRING" - 2 records,
SpringSteen & MindSprings
I did it like this:
setFieldName("Spring");
String sFilter = "field.startsWith(%\" + getFieldName() + \"%)"; -
Extent anExtent = pm.getExtent(this.getClass(), true);
Query aQuery = pm.newQuery(this.getClass(), anExtent, sFilter);
return (Collection) aQuery.execute();
Hope it helps. oh, don't know about the generating sql query.
Nitin Kanani wrote:
Given the following basic class structure.
public class Project {
private String name;
private Architecture architecture;
private Resources resources;
public Architecture getArchitecture();
public void setArchitecture(Architecture architecture);
public Resources getResources();
public void setResources();
public String getName();
public void setName(String name);
public Architecture {
private String name;
public String getName();
public void setName(String name);
public Resources {
private String name;
public String getName();
public void setName(String name);
we want to be able to do a query like:
find all Projects where (Project.name.indexOf("a") > 0) &&
(Project.architecture.name.indexof("b") > 0)
In essence a "like" comparison across String properties of the JDO
objects.
Is it supported in KODO?? Anyone tried it out? Also, is it possible to
generate SQL type query in KODO?
rgds
NK
Patrick Linskey [email protected]
SolarMetric Inc. http://www.solarmetric.com -
How to write statement using LIKE statement in jsp
hi
I am new to jsp. I would like to retrive the data from database using like command. But my query dosen't work well. Can you help me in this topic.
String name = request.getParameter("username");
int i = st.executeQuery("select * from emp where empname LIKE '%name%'");
Plase help me..
Thanking you
sure...Using LIKE :
==========
The following SQL statement will return persons with first names that start with an 'O':
SELECT * FROM Persons
WHERE FirstName LIKE 'O%'
The following SQL statement will return persons with first names that contain the pattern 'la':
SELECT * FROM Persons
WHERE FirstName LIKE '%la%'
Hope this might have helped you
REGARDS,
RaHuL -
SQL "LIKE" statement isn't working right -- help please!
Good day everyone,
I thought about putting this in the "SQL on Oracle" forum, but it looked like those messages were geared more toward the Oracle platform itself, so here I am. I apologize if this isn't the best forum.
I have the following SQL statement:
SELECT zzreporting_ut1
zzreporting_ut2
zzreporting_ut3
zzreporting_ut4
zzreporting_ut5
UP TO 1000 ROWS
INTO TABLE it_gmia_gmgrresp_data
FROM gmspprogram
WHERE ( ( zzreporting_ut1 LIKE '4011%' OR
zzreporting_ut1 LIKE '4012%' OR
zzreporting_ut1 LIKE '4013%' ) OR
( zzreporting_ut2 LIKE '4011%' OR
zzreporting_ut2 LIKE '4012%' OR
zzreporting_ut2 LIKE '4013%' ) OR
( zzreporting_ut3 LIKE '4011%' OR
zzreporting_ut3 LIKE '4012%' OR
zzreporting_ut3 LIKE '4013%' ) OR
( zzreporting_ut4 LIKE '4011%' OR
zzreporting_ut4 LIKE '4012%' OR
zzreporting_ut4 LIKE '4013%' ) OR
( zzreporting_ut5 LIKE '4011%' OR
zzreporting_ut5 LIKE '4012%' OR
zzreporting_ut5 LIKE '4013%' ) ).
All I want to do is bring back the rows where one of those 5 "ut" fields begins with 4011, 4012, or 4013. However, when I run it, it brings back anything: rows where all 5 "ut" fields are empty, where the ut1 field begins with 4014, etc.
Does anyone know what I'm doing wrong in my SQL statement?
Thank you! And as always, points awarded for ALL HELPFUL answers!
Dave
Edited by: Dave Packard on Jul 1, 2008 2:18 PMHi Dave,
because I don't know such a table named gmspprogram I'm not sure what to say.
According to boolean expression syntax you do not need any of the brackets because the only operator used is OR.
I'd suggest.
data: lt_pattern_range type range of gmspprogram-zzreporting_ut2.
perform insert_range using 'ICP':
'4011*' '' changing lt_pattern_range,
'4012*' '' changing lt_pattern_range,
'4013*' '' changing lt_pattern_range.
SELECT
zzreporting_ut1
zzreporting_ut2
zzreporting_ut3
zzreporting_ut4
zzreporting_ut5
UP TO 1000 ROWS
INTO CORRESPONDING FIELDS OF TABLE it_gmia_gmgrresp_data
FROM gmspprogram
WHERE zzreporting_ut1 in lt_pattern_range
OR zzreporting_ut2 in lt_pattern_range
OR zzreporting_ut3 in lt_pattern_range
OR zzreporting_ut4 in lt_pattern_range.
* need form
*& Form insert_range
* insert selection range - handles any range type "CLI20061218
FORM insert_range USING p_signopt TYPE c "#EC CALLED
p_low TYPE any
p_high TYPE any
CHANGING pt_range TYPE table.
FIELD-SYMBOLS:
<range> TYPE ANY,
<sign> TYPE ANY,
<option> TYPE ANY,
<low> TYPE ANY,
<high> TYPE ANY.
DATA:
lv_ref TYPE REF TO data.
CHECK NOT (
p_low IS INITIAL AND
p_high IS INITIAL ).
CREATE DATA lv_ref LIKE LINE OF pt_range.
ASSIGN lv_ref->* TO <range>.
CHECK sy-subrc = 0.
ASSIGN COMPONENT 'SIGN' OF STRUCTURE <range> TO <sign>.
CHECK sy-subrc = 0.
ASSIGN COMPONENT 'OPTION' OF STRUCTURE <range> TO <option>.
CHECK sy-subrc = 0.
ASSIGN COMPONENT 'LOW' OF STRUCTURE <range> TO <low>.
CHECK sy-subrc = 0.
ASSIGN COMPONENT 'HIGH' OF STRUCTURE <range> TO <high>.
CHECK sy-subrc = 0.
<sign> = p_signopt(1).
<option> = p_signopt+1(2).
<low> = p_low.
<high> = p_high.
READ TABLE pt_range WITH KEY table_line = <range> BINARY SEARCH
TRANSPORTING NO FIELDS.
CHECK sy-subrc <> 0.
INSERT <range> INTO pt_range INDEX sy-tabix.
ENDFORM. " insert_range
At least - I think - this looks better. And you may reuse the FORM insert_range whereever you want.
Regards,
Clemens -
how can i get all records starting with % ?
Something that I found here (
SQL Tutorial) might
help:
The optional ESCAPE sub-clause specifies an escape character for
the pattern, allowing the pattern to use '%' and '_' (and the
escape character) for matching. The ESCAPE value must be a single
character string. In the pattern, the ESCAPE character precedes any
character to be escaped.
For example, to match a string ending with '%', use:
x LIKE '%/%' ESCAPE '/'
..or here
Using
SQL Escape Sequences
... or here
How
to escape a wildcard in an SQL order with ORACLE.
Phil -
SQL LIKE statement to text field fomr CF MX 6.1
Hi.
Here's my scenario. I am running SQL Server 2000 SP4 on a
Windows 2003 Server running IIS 6. Using ColdFusion MX 6.1 Updater
version.
I am storing HTML page code (some complete, some just clips)
in a text type field.
I need to check to see if someone has already saved that
exact code already before. So I do the following query:
<cfquery name="checkforexisting"
datasource="mydatasource">
SELECT smallfieldtogetareturn
FROM MyTable
WHERE MyTextField LIKE '%#mystringoflongdata#%'
</cfquery>
Then to see if it posted:
<cfif checkforexisting.RecordCount GT 0>
do the don't clip optional processing
</cfif>
What is killing me is that it finds the match SOMETIMES. Just
not ALL the time. I have tried no percent signs when passing the
ColdFusion variable, only one on the right (which has solved these
types of problems before).
If I pull the record back OUT of the database in a page by
itself (retrieving it by key field), it finds every other copy of
the code EVERY time.
I have put this into a stored procedure, which seemed to help
I even have resorted to creating a temp table first with the
clip, then running a query to retrieve the record that was just put
in, and comparing THAT to my primary table (both have text type
fields). The reason for this, is that if I write a single page that
has just the retrieval of the temp record, and that compare is run,
it finds EVERY copy of it in the primary table. I also thought that
maybe there was a strong problem (I had tried HTMLEditFormat,
HTMLCodeFormat, JavaCast since it was coming from a java program on
the browser end).
I am not terribly familiar with using large text fields, or
dealing with these large pieces of HTML code as something to
compare!
- MikeMichaelSJudd wrote:
As Dan indicated your most likely issue is slight differences
in
capitalization and|or white space or other 'invisible'
difference
between your strings. One can have nearly infinite variety in
HTML code
that will display identically in a browser, the standard is
very
forgiving of formatting differences.
Something I have done in the past to compare HTML content is
to use the
hash() function. It will not help you eliminate the
differences but it
would show when something is different then it appears to be.
#hash(mystringoflongdata)# will return a hexadecimal number
representing
the string. Two *exactly* the same strings will produce the
same
number. Then it is very easy to compare these numbers to see
if two
long strings are the same.
To use this with your database data, you would probably have
to store
the hash value of the string in a field when you store the
HTML data.
HTH
Ian -
Hi all,
I want to select all values from prps-usr00 where the field contains XXX-XXXX...where X is any letter or number. How in the world do I do that?
Thanks,
MatHi,
SELECT SOMETHING INTO VARIABLE FROM TABLE
WHERE PARAM1 LIKE '%-%'.
Or
If you know that there will be exactly 2 char in front of '-' and 2 char after. then use.
SELECT SOMETHING INTO VARIABLE FROM TABLE
WHERE PARAM1 LIKE '__-__'.
Regards
madhu
Edited by: madhu tatikonda on Jun 27, 2008 9:08 PM -
Inserting your own sql-statements in JSP
How do I use my own sql-statements in JSP combined with the ones
generated by JDeveloper?
It seems to me that the following sentences are the ones
generating the sql for inserting in a database:
<jbo:DataSource id="ds" appid="MimPKG.MimPKGModule"
viewobject="BidragView"/>
<jbo:Row id="newRow" datasource="ds" action="Create">
<jbo:SetAttribute dataitem="*"/>
</jbo:Row>
I would like to have more control and therefore I need to make
my own sql-statements.
Is that possible?
Thanks
LeiseYou can use ExecuteSQL data tag to do so. It allows you to
include your own query like insert, update etc. -
Jdev9i, JSP, Execute a Sql Select statement directly?
Hi,
I need the syntax to execute a Sql SELECT statement directly against Oracle database and access rows(returned by SELECT statemnet) in a JSP page or Servlet? Any sample code available for this.
Thanks.Hi Ali,
I didn't work a lot with jsp ,but on this url some help .
http://otn.oracle.com/docs/products/oracle8i/doc_library/817_doc/java.817/a83726/basics7.htm#1014578 -
Sql - execute statement problems
I have a "try" with a "catch" that contains an sql execute statement, and some statements after that, but it doesn't execute any of the following statements. It successfully compiles and has no errors when I run it. It just doesn't insert the information and it stops every command that follows. When I remove the execute state (the one with the asterist next to it), the following statements execute fine. Any information on what I might be missing would be greatly appreciated. Thanks.
try{
new JdbcOdbcDriver();
String url = "jdbc:odbc:javadb";
String user = "";
String password = "";
Connection con =
DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
System.out.println("Made the Connection");
*stmt.executeUpdate("INSERT INTO tbl VALUES ('bradley')");
System.out.println("Made the Connection");
JOptionPane.showMessageDialog(this, "Data Inserted");
}catch(Exception e)
e.printStackTrace();
}Look in your log file. When you perform "e.printStackTrace()" the stack is sent to the System.out, which JSP has already redirected to the log.
If you're having trouble finding the log (it depends on what webserver you're using), try changing that to:
out.println("<h3>We have an error: " + e + "</h3>");
(Notice the absense of "System." here. We're using the pre-defined JSP "out" object that's going to the client browser.)
That will print the error message, plus the reason for the error, on your client as 2nd level header.
Personally, I always print out my error messages in this manner. I find that having a full, complete, extensive stack trace is next to useless. It's like asking for the definition of the word "Java", and having the Oxford Unabridged Dictionary dropped in your lap. -
How to find the backend SQL query of the JSP page in OIC
Does anybody how the best way to find the backend SQL QUERY of OIV JSP page?
How To Generate Trace Files in in HTML/JSP (using Profile Option)
Note: This requires proper responsibility to set SQL Initialization statement using Profile option.
Step 1. Login to the desired Form application.
Step 2. Select +Profile >> System ('Find System Profile Values' screen will pop up)
Step 3. Check 'User' and Type in the Username (in which the account for that user will be trace)
Step 4. Type 'Initialization%' in the Profile box and Hit 'Find' (Click here for preview.)
Step 5. In the User box, type the following statement and Hit 'Save' (Click here for preview)
BEGIN FND_CTL.FND_SESS_CTL('','','TRUE','TRUE','','ALTER SESSION SET TRACEFILE_IDENTIFIER = TESTING MAX_DUMP_FILE_SIZE = 5000000 EVENTS ='||''''||' 10046 TRACE NAME CONTEXT FOREVER, LEVEL 12'||'''');END;
Note: specify any name you like to identify your trace, in this case, testing is the end name on the trace. You can also specify the amount of data allowable to be in the trace, in this case, 5000000 is the amount set. Make sure you hit 'Save' afterwards.[Quotes in the statement are all 'Single' quotes.]
specifying TRACEFILE_IDENTIFIER value is mandatory when setting up the trace using the above profile option value
Step 6. Login to HTML / JSP page with username/password and start your flow. (Everything you do once login to HTML / JSP will get trace.)
Step 7. Logout of HTML / JSP application once you completed with your flow.
Step 8. Go back to the Profile option in the Form application and delete the Initialization SQL statement, and Hit 'Save'.
Step 9. Log in to the database server or login server and retrieve your trace file.
Identify and retrieve the trace file using the tracefile_identifier specified in Step 5.
In this case the tracefile_identifier is TESTING. (Click here for Trace file locations) *
Note: If you need to regenerate your trace or tracing a new flow, then repeat Step 1 to Step 8. To avoid self-confusion, choose a different name for your trace identifier everytime you set to trace.
Step 10. See TKPROF section on how to format trace file into readable text.
Trace Options Definition
No Trace Tracing is not activated
Activities will not get traced.
Regular Trace
(Level 1) Contains SQL, execution statistics, and execution plan.
Provides execution path, row counts as well as produces smallest flat file.
Trace with Binds
(Level 4) Regular Trace plus value supplied to SQL statement via local variables.
Trace with Waits
(Level 8) Regular Trace plus database operation timings that the SQL waited to have done in order to complete, i.e. disk access.
Trace with Binds and Waits
(Level 12) Regular trace with both waits and binds information.
Contains the most complete information and will produce the largest trace file.
****Send me an email to [email protected],I will share the document with you. -
Matching apostrophe's using sql LIKE
heh anyone,
Does anyone know how to match a string that has more than one apostrophe in it under SQL using the LIKE statement..
eg. finding the data string "Surprise Me Bits 'N' Baskets '" in a database table?
...WHERE store LIKE 'Sur%' <- this doesn't work of course as it throws an SQL exception because of the '.
regards
JKIt's ok, solved it. The answer if you are using it in a JSP and feeding in the string to search on is:
String sur = from where ever...
WHERE store LIKE " + "\"" + Sur + "%\"";
cheers
JK -
T-sql case statement in a select
When I execute the following t-sql 2012 statement, the "NO Prod' value is not
being displayed from the sql listed below:
SELECT DISTINCT
IsNull(cs.TYPE,'') as type,
CASE IsNull(Course.TYPE,'')
WHEN 'AP' then 'AP Prod'
WHEN 'IB' then 'IB Prod'
WHEN 'HR' then 'HR Prod'
WHEN '' then 'NO Prod'
END AS label
FROM CustSection cs
INNER JOIN dbo.Person p on P.personID = cs.personID
Left join customCustomer cs564 on cs564.personID = p.personID and
cs564.attributeID ='564'
where ( cs.type is null and cs564.attributeID = null)
or
(cs.type IN ('HR','AP') OR
(cs.type='IB' AND SUBSTRING(cs.code,1,1)='3'))
ORDER BY label
What I want is for 'NO Prod' to be displayed when
cs.type is null and cs564.attributeId is null.
Thus can you tell me how to fix query above so the 'NO Prod' value is displayed in the
select statement listed above?There is no CASE statement in SQL; we have a CASE expression. We do not use the old 1970's Sybase*- ISNULL(); we have COALESCE().
There is no such thing as a magic generic “type” in RDBMS. There is no such thing as a generic “code” in RDBMS. They have to to be “<something in particular>_type” and “<something in particular>_code” in a valid data model. How about blood_type
and postal_code??
There is no such thing as a generic “person” table in RDBMS. First of all, do you really have only one person, as you said?? But the important point is that these persons play a role in the data model – customers, students, etc. You are doing the wrong thing
and doing it badly. This table should not exist any more than a table of “Things” such exist.
And the reason you are beyond any real help is “attribute_id” which tell us that your schema is a total disaster of data and meta data mixed together in a non-RDBMS written in awful SQL. Based on cleaning up bad SQL for 30 years, it looks like you are an OO
programmer who never unlearned his prior mindset.
Why did you allow an encoding schema with blanks? Why do you have so many NULL-able columns?
SELECT DISTINCT is very rare in a properly designed schema. The DRI references assure that rows cam be matched. To get you started, look at this skeleton:
CREATE TABLE Products
(product_gtin CHAR(15) NOT NULL PRIMARY KEY,
product_type CHAR(2) DEFAULT 'XX' NOT NULL
CHECK (product_type IN ('AP', 'IB', 'HR', 'XX'))
The table name is a plural noun because it models a set (NOT an OO class).
The GTIN is an industry standard identifiers, and not have to invent our own.
The product_type (not blood_type, not automobile_body_type!) has a constraint that assures it is never NULL and never blank; I invented 'XX' as a default.
You need more help than you can get in a forum, but if you will follow Netiquette and post the DDL, we can get you started.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
SQL CASE statement in XML template- End tag does not match start tag 'group
Hi All,
I am developing a report that has the SQL CASE statement in the query. I am trying to load this into RTF with report wizard and it gives me below error
oracle.xml.parser.v2.XMLParseException: End tag does not match start tag 'group'
Does XML publisher support CASE statement?
My query is something like this
SELECT customercode,
SUM(CASE WHEN invoicedate >= current date - 30 days
THEN balanceforward ELSE 0 END) AS "0-30",
SUM(CASE WHEN invoicedate BETWEEN current date - 60 days
AND current date - 31 days
THEN balanceforward ELSE 0 END) AS "31-60",
SUM(CASE WHEN invoicedate < current date - 60 days
THEN balanceforward ELSE 0 END) AS "61>",
SUM(balanceforward) AS total_outstanding
FROM MyTable
GROUP BY customercode
ORDER BY total_outstanding DESC
Please advice if the CASE statement or the double quotes are causing this error
Thanks,
PPI got this to work in the XML but the data is returning zeros for all the case statements. When I run this in toad I get results for all the case conditions but when ran in XML the data displayed is all zeros. I am not sure what I am missing. Can someone shed some light on this please
Thanks!
PP -
Parsing an OPEN SQL select statement
Hi all,
I want to parse an open SQL select statement to get the list of tables and the corresponding fields in the select statement.
Is there some ABAP-internal syntax from the SAP Database Interface that does this? or is the only option to do this is to manually break the statement?
Thanks.
GirishHi,
I doubt if there is something like that available to use. It think you will have to write a parser for yourself.
Regards
Maybe you are looking for
-
Have been having problem when burning dvd, burns so far than quits & notice idvd crash do not understand what the message means is happening with every dvd burn attempt Also need to know how to send idvd project to trash / delete when done with it
-
Create Custom Application Loader
I don't know whether this is the right forum to post the question or not. My question is : Can I write a custom class loader in Weblogic App server ? My basic requirement is to write a custom application loader with will first load some jar files fro
-
No Reply on Flash Remoting Forum
Hi everyone, just to say that I posted a subject on the Flash Remoting Forum ( Here) but I'm not sure it's still active.. so if you have an idea don't hesitate.. :)
-
Layers keeps unchecking itself
New install of PS CS4 on New machine running Windows 7 Home Premium. When I am working, somehow layers just gets unclicked. When I do my normal stuff, like flatten the layer, when I am done editing, layers sometimes disappear, not always, but much o
-
Can I copy one page of several pictures from and old picture album? I hope I don't have to peel each photo of my mothers albums since they are very old.. Bob