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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with StringBuffer" Watch "Problem with StringBuffer" New topic

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


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" );
PrintWriterout =response.getWriter();
StringBuffer sb1=new StringBuffer();
StringBuffer sb2=new StringBuffer();
try {
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
con = con=DriverManager.getConnection("jdbc dbc:jdbcodbc","scott","tiger");
stmt = con.createStatement ();

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

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 () ){


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

sb2.append(st2.nextToken() +"'");

String str=new String(sb2);



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

Joined: Jul 08, 2003
Posts: 24183

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!
Don't get me started about those stupid light bulbs.
subject: Problem with StringBuffer
Similar Threads
Guess the answer
String Objects
A String Buffer question,help!
StringBuffer question