hello friends i am new to ejb and i tried to create a session bean for obtaining JDBC Connection,and i called it from a servlet the session bean was a remote session bean but connection is not getting established..
EJB CODE :
ConnectionBean.java
package com.ejb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.ejb.Stateless;
import com.mysql.jdbc.*;
/**
* Session Bean implementation class GetConnect
*/
@Stateless
public class GetConnect implements GetConnectRemote,GetConnectLocal {
/**
* Default constructor.
*/
public GetConnect() {
// TODO Auto-generated constructor stub
}
public Connection getConnect() throws ClassNotFoundException
{
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jobportal","root","root");
System.out.println("Connection is:"+con);
}
catch(SQLException e) {
System.out.print("Error:"+e.getMessage());
}
return con;
}
}
And Servlet code:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import javax.ejb.EJB;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ejb.GetConnectRemote;
/**
* Servlet implementation class Connection
*/
public class ConnectionServlet extends HttpServlet {
@EJB
GetConnectRemote db;
private static final long serialVersionUID = 1L;
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out=response.getWriter();
Connection con=null;
try {
con=db.getConnect();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
out.println(e.getMessage());
}
out.println(con);
}
}
please help me...thanking u