Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with StringBuffer

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Frank Ertl
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why aren't you using a simpler way of replacing characters in a String??
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to "Java in General (Beginner)."
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic