0

I've written a small code which functions like follows:

  1. Read an input file
  2. Read the first line in it and check if it has value AAA at a certain position
  3. if it satisfies the condition call the method insertAAAmethod and load the data to the oracle table
  4. read the next line if it is record BBB call the insertBBBrmethod which has different insert query

The problem is I have 15 different records in the input file so I have 15 different methods like insertAAAmethod each with different insert query:

public static void insertAAARecord() throws Exception {

    String sqlQuery = "insert into my_table(ColumnA,ColumnB,ColumnC,ColumnD,ColumnE,ColumnF,ColumnG)"
            + "values (?,?,?,?,?,?,?)";

    try {

        pstmt = conn.prepareStatement(sqlQuery);

        pstmt.setString(1, "AAA");
        pstmt.setDate(2,
                StringtoDate("AAA", CurrentLine.substring(150, 158)));
        pstmt.setDate(3,
                StringtoDate("AAA", CurrentLine.substring(158, 166)));
        pstmt.setString(4, CurrentLine.substring(24, 34));
        pstmt.setString(5, CurrentLine.substring(37, 45));
        pstmt.setString(6, CurrentLine.substring(147, 150));
        pstmt.setDate(7, headerDate);

        pstmt.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace(System.out);

    } finally {

        pstmt.close();

    }

}

Is it possible to keep the sql query out of the java code? (Like in a properties file, for example)

Note: The insert query changes as per the record if it is record 'AAA' it should follow one insert query if the record is different insert query should change..

Let me know how to optimize my code.

madth3
  • 7,275
  • 12
  • 50
  • 74
javanoob
  • 6,070
  • 16
  • 65
  • 88

4 Answers4

2

Yes, you should create a properties file with all your queries and load them on startup using Properties object, then use the Properties to look them up. You can also you Spring to inject the queries into your configuration objects.

I have always built DB object that integrate your code to DB data and queries are kept there, it is much easier to debug and manage as everything you need is in one place.

Make your life simple, avoid ORMs and tune your SQL queries (or let DBAs do that that's what they do and they are good at it). However if you do not like SQL or don't care how efficient it is, then ORM like Hibernate may be what you need.

Alex Chacha
  • 423
  • 2
  • 7
1

Maybe this will take more time than other approaches, but it would help you with your code optimization.

Every query/process have common attributes.

  • SQL Query
  • String pattern
  • Parameters

and each parameter has

  • Position
  • Datatype
  • etc

If you're using spring already, you'll be able to define these as beans in your config.xml. if not, you can use xml configuration anyway (instead of property files)

Then, you will have to create some class to parse these beans and create the custom queries.

Hope it helps.

psabbate
  • 767
  • 1
  • 5
  • 22
  • Thanks for your answer..I am not using spring. Could you forward any link or tutorial on how to use xml configuration instead of property files.. Thanks so much – javanoob Feb 18 '13 at 15:28
  • There are several ways to do that, in this link are interesting options, let me know if you need any further help. [http://stackoverflow.com/questions/2333479/how-to-read-an-xml-file-with-java](http://stackoverflow.com/questions/2333479/how-to-read-an-xml-file-with-java) – psabbate Feb 18 '13 at 17:30
0

Try to use any ORM framework like Hibernate/JPA, Ibatis?

dillip
  • 1,782
  • 17
  • 16
0

You want to use JPA. I would recommend walking through a simple JPA tutorial such as the following: http://glassfish.java.net/javaee5/persistence/persistence-example.html

That tutorial will demonstrate the basics of how to create or modify objects and persist those changes in a database. Good luck.

mightyrick
  • 910
  • 4
  • 6