Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to parse StringBuffer

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a values filled in ArrayList and also appending some elements inside the StringBuffer



The Output I am Getting is
The String Buffer value is 'Test''Test''Test''Test''Test'

Actually What i want is that while Iterating the ArrayList If i found a NUMBER element i need to remove the Quation Marks

This is waht i am trying .
I don't know how to proceed further plase help .Or tell me is theer any other way to accomplish this .



Please guide me with this or suggest me if there is any easyway to acconplish this task

Thanks in advance .
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's about it. You can write some class around it (some Format subclass perhaps), but in the end it will still need to check the column types.

But you do know that most database systems allow numerical strings (e.g. '1') to be inserted into a numeric column? I know Oracle does.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya ROB you are right , I agree with you , but unfortunately my Manager is not agreeing as this becomes problem if null is passed .

so please tell me any solution .
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I suggested when you asked a simmilar question in a another post - if you want to build a SQL statement why not use a PreparedStatement? That way you don't even need to know what data type the column is, setObject will do the bulk of the work for you. You'll need to watch for nulls, but that is easy, right?
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I completely agree with Paul on this one. PreparedStatement is the way to go. It will also handle the case where the value already includes quotes.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Thanks ,

I also go for preparedstatements whenever needed , but here the situation is that the Table name and columns are built dynamically (Means depending on he click of the hyper link)

so it will be some thing like this :





Anyway I completed the task , i will post the code tomorow , so that somebody may use it if required.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the table name being dynamic affect the choice to use Statements over PreparedStatements?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul ,
we don't know even the column names also.
Tomorow i will paste the whole code .
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...again I'm not sure I see what the issue is. You have some sort of meta data (which you are holding in an ArrayList) and you have the table name. That's all you need to build a valid PreparedStatement. Statements (particularaly built up from a web application) are a security hole. If I could use a PreparedStatement over a Statement I would.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


May be i followed a wrong approach . Now i don't want to touch that code again and be in headache .
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can build your query for prepareStatement using the unknown column names, then use setObject / setNull for setting the actual values.

Still, the security hole still exists. What if I use the following for my first column:
The full query will be this:
I'll let you guess what it will do
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic