|
Being a newbie to Grails, I need a little guidance here.
I created a grails app & using dynamic scaffolding, created a table in devDB . I also added some records into this table. The records are persistent (I modified the Datasource.groovy file to say 'update' instead of 'create-drop'). Now I am attempting to write a separate Java app to access this database and add some records into this table using the "h2 driver" ? The sample code is as follows. My question is how do I know if I am pointing to the same database as created in the grails app ? What should I do ? I ran the h2-console, but that did not show me the tables in my database. I am more of Microsoft SqlServer & .NET C# guy where this is quite obvious. import java.sql.*; import java.sql.Statement; import org.h2.*; public class DBAccess { public static void main(String[] a) throws Exception { Class.forName("org.h2.Driver"); Connection conn = DriverManager. getConnection("jdbc:h2:devDb", "sa", ""); System.out.println("Connected to database"); Statement st = conn.createStatement(); //String query = "SELECT * FROM Barcodes"; //String query = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES"; String query = "SHOW TABLES FROM devDb"; try { System.out.println("Execute Query"); ResultSet rs = st.executeQuery(query); while (rs.next()) { System.out.println(rs.getString(1)); } } catch (Exception e) { System.out.println("Exception:" + e.getMessage()); } // add application code here conn.close(); } } |
|
The default H2 instance is in-memory so changes don't really persist outside of the running grails instance. You can make it a file based database which can be accessed more easily by setting this in DataSource.groovy:
url = jdbc:h2:devDb;auto_server=true
Then change your getConnection(...) to match this string. Note that "devDb" is the database file so you might need to adjust the path accordingly. -Aaron
On Thu, Jun 21, 2012 at 10:22 AM, chairao <[hidden email]> wrote: Being a newbie to Grails, I need a little guidance here. |
|
Aaron, thanks very much. I had to point it to the correct file from my Java app, and all worked fine after that.
Regards Chai |
|
Glad to help.
For future reference, these type of usage questions should probably go to the user list instead of the dev list. -Aaron
On Thu, Jun 21, 2012 at 4:45 PM, chairao <[hidden email]> wrote: Aaron, thanks very much. I had to point it to the correct file from my Java |
| Powered by Nabble | Edit this page |
