3

I am trying to implement a variant of this SO question

I want to run tens of SQL statements against a database. Building the statements up in Strings is getting laborious. I got the idea to make a file of them instead (statements.sql) which is super easy to write and debug.

However I have a requirement that I must have everything needed contained in a runnable JAR, so this .sql file is going to have to be an internal resource (like C# projects). I cannot simply read an external file.

The SO question above sounds like the right idea for what I want, but no matter where I place the .sql file (src package, or a folder by itself), or if I "Add to Build Path" or not, I keep getting...

java.lang.NullPointerException

...when I run this code...

InputStream input = getClass().getResourceAsStream("/MySQL/src/com/package/statements.sql");

My Java IDE is Eclipse (Luna), running on Debian 7 GNome and the .sql file is currently in a package under source.

I just need the file's contents in a big string for parsing / processing. If there is a better way to do that I'd appreciate a push in the right direction. I think my issue may have more to do with my unfamiliarity with how to do this in Eclipse as opposed to code issue(s). To get the "path" string for the code I am right-clicking the .sql file and selecting "properties". The Path to the file (relative to the project?) is displayed there and I am copy / pasting that.

What is the right way to register this file with Eclipse and consume it? Thanks kindly.

Community
  • 1
  • 1
Geek Stocks
  • 2,010
  • 3
  • 27
  • 43
  • If you are using hibernate check http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/tool/hbm2ddl/ImportSqlCommandExtractor.html to parse the `SQL`. – Arturo Volpe Aug 02 '14 at 22:46
  • You can add some stacktrace, what framework are you using? – Arturo Volpe Aug 02 '14 at 22:47
  • You might find `java sql properties file` useful. Google this. Here is one link from my search - http://stackoverflow.com/questions/1544335/java-storing-sql-statements-in-an-external-file – Erran Morad Aug 02 '14 at 22:55
  • Not using any frameworks to do this, its just straight up java.sql and java.io for now. I'll check into your link. – Geek Stocks Aug 02 '14 at 22:55
  • see this question http://stackoverflow.com/questions/5476530/how-to-read-a-file-from-a-java-class-both-are-in-the-same-jar – Arturo Volpe Aug 02 '14 at 23:05
  • Opa!! @AVolpe the link to the SO question (5476530) was a help. The key phrase in THAT question which resonates is "...the path either has to be RELATIVE TO THE GIVEN CLASS..." (emphasis mine). The answer is that the file must be in the folder WITH THE CLASS THAT CALLS IT and then you can just use the filename ONLY as the path; in my case its: getClass().getResourceAsStream("statements.sql") once I moved the file to the class's folder in Eclipse. Write up an answer if you want the points - thanks for the link. – Geek Stocks Aug 02 '14 at 23:34

1 Answers1

1

From this answers

The path either has to be relative to the given class but without navigating back up the tree, or it has to be absolute to start with.

So you have two options:

  • Put the file in the same location of the class and get the file with getClass().getResourceAsStream("statements.sql")
  • Put the file in the root and get it with getClass().getResourceAsStream("\statements.sql") or getClass().getClassLoader().getResourceAsStream("statements.sql")
Community
  • 1
  • 1
Arturo Volpe
  • 3,442
  • 3
  • 25
  • 40