aspose file tools*
The moose likes Beginning Java and the fly likes Problem with StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with StringBuffer" Watch "Problem with StringBuffer" New topic
Author

Problem with StringBuffer

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
I am working with StringBuffer class for converting the characters.
like single quote to star and agian star to single quote.

but while converting this i need to use append method
this append mehtod is creating problems ...

while i convert single quote char to * it adds one more '*' to the end of the text each time

please can any one tell me why it is appending one more '*' to the end of the string.???


thanx in advance

cinux


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Frank Ertl
Ranch Hand

Joined: Apr 25, 2005
Posts: 59
It's difficult to tell wihout a snippet of the code you use. Please post your code, so we might help you.
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
import java.io.*;
import java.util.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.util.Enumeration ;
import java.sql.*;

public class TextFmt extends HttpServlet

{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{


Statement stmt;
Connection con = null;
ResultSet rs2=null;
String query1,query2;
String text,outputText,textfmt;

String qtext=new String();
textfmt= request.getParameter ( "textarea1" );
System.out.println(textfmt);
PrintWriterout =response.getWriter();
StringBuffer sb1=new StringBuffer();
StringBuffer sb2=new StringBuffer();
try {
System.out.println("Registering........");
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
System.out.println("connecting........");
con = con=DriverManager.getConnection("jdbc dbc:jdbcodbc","scott","tiger");
stmt = con.createStatement ();

StringTokenizer st1 = new StringTokenizer(textfmt,"'");

while(st1.hasMoreTokens())
{
sb1.append(st1.nextToken() +"�");
}

String startext=new String(sb1);
System.out.println("inserting ur data........");
query1="insert into ttable values ( '" + startext + "' )";
stmt.executeUpdate( query1 );

query2 = "select textfmt from ttable";

rs2 = stmt.executeQuery(query2);

while ( rs2.next () ){

qtext=rs2.getString(1);
}


StringTokenizer st2 = new StringTokenizer(qtext,"�");

while(st2.hasMoreTokens())
{
sb2.append(st2.nextToken() +"'");
}

String str=new String(sb2);

System.out.println(str);
out.println(str);




}



catch ( SQLException sqle ){

System.out.println ( "Could Not get the Database Connection: " + sqle );
}

}





}
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Why aren't you using a simpler way of replacing characters in a String??


ASCII silly question, Get a silly ANSI.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Moving to "Java in General (Beginner)."


[Jess in Action][AskingGoodQuestions]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
It's almost ( :roll: ) always better to be clear about your context and what
your problem is -- what you are trying to do -- than to just ask a specific
question about subproblem. It appears that you've had trouble in the past inserting
text with embedded single quotes into your database, and as a result of that,
you are doing your own custom encoding of that character as a star of some
sort (although it looks like an o with a diaeresis to me!). I would
suggest the solution is to use a Prepared Statement instead of just a Statement and
your ad hoc approach. If you do this, your need for a StringBuffer disappears.

Code using a prepared statement is also simpler to write, something like:
[ November 28, 2005: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with StringBuffer