7 Replies Latest reply: Mar 15, 2007 4:41 AM by Subathra Murugesan RSS

Connecting to SQL via EJB

Subathra Murugesan
Currently Being Moderated

Hi all,

      I need to access a SQL server from my EJB program. I have to connect to the SQL and retrieve data.This is my requirement. As of now, I am connecting to the SQL using the following code in my bean.


/********Code begins*******/


//To connect to SQL

          try {


          } catch (ClassNotFoundException e) {





          Statement stmt = null;

          Connection connection = null;

          System.out.println("SQL Web Service");

          try {

               connection =





               stmt = connection.createStatement();

          } catch (SQLException e1) {





          // To retrieve the data from DB


          ResultSet rs = null;

          String return_string = null;

          String query = "select * from <tablename>";

          try {

               rs = stmt.executeQuery(query);

               while (rs.next()) {

                    return_string = rs.getString(1);




          } catch (SQLException e2) {





          try {


          } catch (SQLException e3) {





                               return return_string;



/*****Code ends*******/


This is what I've done now. But it doesn't work!! When I deploy the EAR and run , I get the following exception.



Can someone help me with the right way of connecting to SQL server from EJB. I need to expose this as a web service later.


Thanks & Regards,