File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Head First Android 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.util.*;
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 ( () ){


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: 24189

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!
Consider Paul's rocket mass heater.
subject: Problem with StringBuffer
It's not a secret anymore!