Quantcast

Grails & Java app datasource integration

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Grails & Java app datasource integration

chairao
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();
    }
}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Grails & Java app datasource integration

longwa
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.

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();
   }
}

--
View this message in context: http://grails.1312388.n4.nabble.com/Grails-Java-app-datasource-integration-tp4630483.html
Sent from the Grails - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Grails & Java app datasource integration

chairao
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Grails & Java app datasource integration

longwa
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
app, and all worked fine after that.

Regards
Chai

--
View this message in context: http://grails.1312388.n4.nabble.com/Grails-Java-app-datasource-integration-tp4630483p4630520.html
Sent from the Grails - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Loading...