开发者

How to use DataSource in Struts action class? It gives a compilation error "Type mismatch"

I have defined the data source in struts-config.xml. I want to get a connection from it in a St开发者_StackOverflow中文版ruts action class.

The code is:

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
    LoginForm loginForm = (LoginForm) form;
    Connection con = null;
    DataSource ds;

    try {
        ds = getDataSource(request,"A");
        con = ds.getConnection();

    } catch (Exception e) {

    }

    return null;
}

The line

        ds = getDataSource(request,"A");

gives the following compilation error:

Type mismatch: cannot convert from DataSource to DataSource

How can this happen and how can I fix this?


Type mismatch: cannot convert from DataSource to DataSource

This compilation error just means that the type returned by getDataSouce() doesn't match the type as you declared the ds to be. This compilation error is also not really helpful since it doesn't include the package name. But it at least means that you're using two different DataSource classes from different packages.

You need to ensure that the type is javax.sql.DataSource everywhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜