2 Replies Latest reply on Jan 26, 2003 4:50 PM by Juha Lindfors

    Getting a reference to data sources in non JBoss apps

    McGee Newbie

      Hello all,

      I (well, someone else) configured a MS SQL datasource in JBoss, and I'm having no trouble accessing it from my EJBs. But I can't get at it from a simple command line app running in a separate JVM. My boss has some theories like "maybe you can't access a DataSource remotely", and they seem plausible enough, but I'm interested in some facts.

      Any thoughts?
      McGee

        • 1. Re: Getting a reference to data sources in non JBoss apps
          Cindy Conway Newbie

          I apoligize in advance if I am misunderstanding what you are trying to do.

          Accessing a database from a command line application is completely different from accessing it via an app server.

          From the command line program, you have to load the JDBC driver. Here's an example that loads the microsoft driver, connects to a SQL Server database and executes a query:

          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

          String url = "jdbc:microsoft:sqlserver://myserver:1433;DatabaseName=mydbname;user=myuserid;password=mypassword";
          Connection connection = DriverManager.getConnection(url);
          Statement statement = connection.createStatement();

          String query = "Select * from mytable";
          ResultSet rs = statement.executeQuery(query);

          The URL is the same URL that you use in your JBoss datasource configuration.

          I hope this helps.

          • 2. Re: Getting a reference to data sources in non JBoss apps
            Juha Lindfors Master

            Your boss is correct, the JBoss datasources are only bound to the in-VM name space, not the global name space. This means that an EJB deployed to the server can lookup the data source directly, whereas an application running in a different process (such as your command line app) cannot.

            So either use JDBC directly from your command line app as the previous poster suggested, or build a server side component (stateless session bean or an MBean) that acts as a "bridge" between your command line app and the JBoss data source.