Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to stop escapeing '\' using java with postgres db

Status
Not open for further replies.

ketandba

Programmer
Nov 15, 2004
38
US
Hi,
All,
i have problem in postgres db insert..

Here is my problem.
i have created table like..(for this question purpose only---Actual table defination is different)

1) Create table tab1(usr_id varchar(15), usr_name varchar(20),usr_filename_pattern varchar(1024));
insert table tab1 values('A','Mr. A','A\\d\\d\\d\\d');
Record is successfully inserted But when i retrieve using java program..
select * from tab1;
The output is 'A', 'Mr. A', 'A\d\d\d\d' (means it escapeing '\')
Also when i update the record
update tab1 set usr_name='Mr. B' where usr_id='A';

and after succefully updation when i try to retrieve record by
select * from tab1;
output is 'A','Mr. B', 'Adddd' (means it escapeing '\' again)


here is my java code snippet...
Code:
public class UpdateUserAction extends Action {
	
	public ActionForward execute(ActionMapping mapping,
						 ActionForm form,
						 HttpServletRequest request,
						 HttpServletResponse response) {

			
	  	  if (form instanceof UpdateUserForm) 
	  	  	{

	  	  	UpdateUserForm updateUserForm = (UpdateUserForm) form;		 			

	  	  	String username = ((UpdateUserForm) form).getUserId();
		
	  	  	HttpSession session = request.getSession();	
	  	  	session.setAttribute("SelectedUser",username);	
			
	  	  		
			String roleId    = ((UpdateUserForm) form).getRoleType();
			
			String firstName   = ((UpdateUserForm) form).getFirstName();
			
			String lastName  = ((UpdateUserForm) form).getLastName();
			
			String password     = ((UpdateUserForm) form).getPassword();			
			
			String emailAddress = ((UpdateUserForm) form).getEmailAddress();
[COLOR=red]			
			String filenamePattern = ((UpdateUserForm) form).getFileNamePattern();
			
[/color]
			String hosId = ((UpdateUserForm) form).getHosName();
			
			String middleName = ((UpdateUserForm) form).getMiddleName();
			
			String phone = ((UpdateUserForm) form).getPhone();
			

			boolean modifieduser= false;
			UserDAO userDao = new UserDAO();
	
			modifieduser = userDao.modifyUser(username,Integer.parseInt(roleId),Integer.parseInt(hosId), password,
					firstName, middleName,lastName,1,
					emailAddress,phone,filenamePattern);
I am very much surprise that only just retrieve all form fields, It escapeing '\' from filenamePattern..i am not changing the value of filenamePattern.

Intitally filenamePattern has valur 'A\\d\\d\\d\\d' after first retrieval it becomes 'A\d\d\d\d'
after second retrieval it becomes 'Adddd'

pl. not that i am not changing the value of filenamePattern...

My question :

After updation how i get
'A', 'Mr. B', 'A\\d\\d\\d\\d'
i.e. not escapeing '\\'.
I am using postgres 7.4.6 and java 1.4.

pl. help me out...

Thanks in advanced..

ketan
 
Run your filenamePattern through java.net.URLEncoder before committing it to database. Recover the String using java.net.URLDecoder.

e.g.
Code:
String filenamePattern = URLEncoder.encode(((UpdateUserForm) form).getFileNamePattern(), "UTF-8");

Note that the encode (and the URLDecoder.decode()) method declares an UnsupportedEncodingException to be thrown which you will have to catch.

You'll still have to escape the '\' in your original string, but once encoded no further interference will occur.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top