Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I trim() inside the query or in the "String"?

Posted on 2006-07-18
10
Medium Priority
?
323 Views
Last Modified: 2010-03-31
Experts,

I have the query below that take the user's authentication ID (login ID) and then call the database table to see if there is a match.  IF there is a match then it list it.

However, I am not getting any errors, but the login ID is not matching up to that ID in the table, and I have checked that the ID exists in the table.

I am thinking perhaps the VZID needs to be trimmed in the query.  
How can I do that?

Here is my code:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*"%>
<%@ page errorPage = "../public/errpage.jsp" %>
<%@ page import="java.io.*" %>

<%
String username1 = (String)session.getAttribute("j_username");
String username = username1.trim();
%>


<HTML>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>jusername2.jsp</TITLE>
</HEAD>
<BODY>
<%
try {
javax.sql.DataSource dss;
java.sql.Connection conn = null;
Context icc = new InitialContext();
dss = (javax.sql.DataSource) icc.lookup("java:comp/env/jdbc/mydb2");
conn = dss.getConnection();
conn.setAutoCommit(false);
  java.sql.Statement stat = conn.createStatement();
  java.sql.ResultSet cols = stat.executeQuery("SELECT VZID,USER_NAME FROM CBSTDTFI.CS_USER_INFO WHERE VZID='"+username+"' FOR FETCH ONLY WITH UR ");
 while(cols.next()) {
   String VZID  = cols.getString("VZID");
   String USER_NAME  = cols.getString("USER_NAME");
%>
<OPTION value="<%= VZID %>"><%= VZID %></OPTION>
<OPTION value="<%= USER_NAME %>"><%= USER_NAME %></OPTION>
 
<% } // close while loop %>
 
<%
cols.close();
stat.close();
conn.close();
 
} catch (SQLException sqle) { sqle.printStackTrace(); }
%>
<BR><BR><BR>
<%= username %>
</BODY>
</HTML>
0
Comment
Question by:g118481
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17130870
You could do

stat.executeQuery("SELECT VZID,USER_NAME FROM CBSTDTFI.CS_USER_INFO WHERE VZID='"+username.trim()+"' FOR FETCH ONLY WITH UR ");
0
 
LVL 1

Author Comment

by:g118481
ID: 17131001
I think i need to trim the "VZID".
How can I do that?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17131060
You can try

stat.executeQuery("SELECT VZID,USER_NAME FROM CBSTDTFI.CS_USER_INFO WHERE rtrim(ltrim(VZID))='"+username.trim()+"' FOR FETCH ONLY WITH UR ");

depending on your db
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Accepted Solution

by:
mukundha_expert earned 1000 total points
ID: 17131119
>>I think i need to trim the "VZID".
>>How can I do that?
username is the value for VZID so you need to trim username ( which is the VZID),


try,

System.out.println ("SELECT VZID,USER_NAME FROM CBSTDTFI.CS_USER_INFO WHERE VZID='"+username+"' FOR FETCH ONLY WITH UR ");

before executing the query, check whether the username is the same as you expect.

i got the same problem once, the reason was the username had spaces in between

where are you getting the username from?? is it entered by user or you are getting from some where ( like remoteuser, NLTM authorization)
0
 
LVL 1

Author Comment

by:g118481
ID: 17132202
mukundha_expert,

I added the System.out.println... that you suggested.  the code still returns a blank page.  
I am doing a print of the username, and it shows up fine.  However, VZID that should match up to the username from the query shows a blank.

I know the ID exists in the table.  What am I doing wrong.  very frustrating.
Also, I am going to increase the points for this record.  Looks like this will be difficult.

Cheers
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17132340
>>the code still returns a blank page.

it will be printed in the server console.

>> am doing a print of the username, and it shows up fine

are you using out.println(); if so the blank spaces between the string will be ignored. see it in the console
0
 
LVL 1

Author Comment

by:g118481
ID: 17132833
mukundha_expert,

I cannot view the server console.  This is running on Mainframe IBM Z/OS Websphere 5.0
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17132874
ok, check the length of the string and see if it is equal to what you have thought

0
 
LVL 1

Author Comment

by:g118481
ID: 17133094
OK, the last trim of the variable worked.  I now get the result I expected.
Thanks for your help.

Cheers
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17135473
I suggested trimming the username ...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question