0

I am writing a program in Java that uses the JDBC driver with Postgresql. I am using an API which when called, returns data in a string like so:

id=-870229851 date = finished-20130501  15:13:07-20130502  15:13:07 open=-1.0 high=-1.0 low=-1.0 close=-1.0 volume=-1 count=-1 WAP=-1.0 hasGaps=false

Most of these names are a column in my Postgresql db (and I can make all of them a column if necessary), but I was wondering what is the most efficient way to insert this into Postgresql?

Before I write a function to parse each title / value, I'm hoping that Postgresql has some way of handling data like this efficiently.

Can someone please shed some light?

my Postgresql version is 9.2 and my Java is 1.7

2
  • 1
    Can you change the API such that it returns you a ResultSet, a POJO, a bean, or something other than... a String? What you have is great for a log file, but not so great for, well, anything else...
    – corsiKa
    Commented May 2, 2013 at 21:44
  • @corsika I can overload the function of the api. I'm not sure what those things are so give me a sec while I use Google to see if that is possible... Commented May 2, 2013 at 21:49

1 Answer 1

0

Postrgre doesn't have a way to do this natively (that I know of anyway), however, you can use Java's string tokenizer like this:

StringTokenizer st = new StringTokenizer(sourceString, "= ");

while(st.hasMoreTokens()) {

  String key = st.nextToken();
  String valuse = st.nextToken();

  // Do something with the keys and values (i.e. build and execute insert statements.
} 

Where sourceString is your data string. This will delimit the string based on both the equals sign and the space between the name value pairs. Assuming that no spaces occur in the name or values in the string. If they do, then you are going to have to do some regex magic on the string before tokenizing it, depending on that can occur on the left and right hand side of the equals sign.

Then you can generate insert statement strings to be executed by the database engine.

1
  • I was hoping for some quick postgresql fix but since there is none I ended up doing regex to get the information. I'll accept your answer though since it is less code than what I wrote. Thank you. Commented May 3, 2013 at 2:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.