简体   繁体   English

如何将JEE应用程序连接到MySQL数据库?

[英]How to connect a JEE app to a MySQL database?

I'm trying to connect a JEE app to a MySQL database. 我正在尝试将JEE应用程序连接到MySQL数据库。 There is a lot of documentations and similar questions on this topic, but none of the solutions i've found works so far (Nonetheless, I may have miss one in particular so feel free to propose). 关于这个主题有很多文档和类似的问题,但到目前为止我找不到任何解决方案(尽管如此,我可能特别想念一个,所以随意提出)。

The server name is db and the database name is joinmyparty 服务器名称为db ,数据库名称为joinmyparty

Here is my java code to connect : 这是我要连接的java代码:

public class MySqlConnection {
    private static Connection conn;

    public static Connection getConn() {
        try {
           Context initContext =  new InitialContext() ;
           Context envContext  = (Context)initContext.lookup("java:/comp/env") ;
           DataSource ds = (DataSource) envContext.lookup("jdbc/joinmyparty") ;
           conn = ds.getConnection();

        }  catch (NamingException e) {
            System.out.println("Cannot get connection: " + e);
        }  catch (SQLException e) {
            System.out.println("Cannot get connection: " + e);
        }
        return conn;
    }

Every time I call a DAO, I use this method getConn() to open a connection and i close it in the finally block. 每次我调用DAO时,我都使用这个方法getConn()打开一个连接,然后在finally块中关闭它。

I wrote this into my /tomcat/conf/context.xml file : 我把它写入了我的/tomcat/conf/context.xml file

<?xml version="1.0" encoding="UTF-8"?>

<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

  <Resource url="jdbc:mysql://db:3306/joinmyparty"
driverClassName="com.mysql.jdbc.Driver" password="mypassword"
username="myusername" maxWait="10000" maxIdle="30"
maxActive="100" type="javax.sql.DataSource" auth="Container"
name="jdbc/joinmyparty" validationQuery="select 1" testOnBorrow="true"/>

</Context>

I've tried to put the mysql-connector .jar into the /tomcat/lib or into the /WEB-INF/lib and into the build-path. 我试图将mysql-connector .jar放入/tomcat/lib/WEB-INF/lib并放入build-path。 I've also tried multiple versions of connectors (but only one at a time), especialy to get the one with same level as MySQL database. 我也尝试了多个版本的连接器(但一次只能使用一个),特别是要获得与MySQL数据库相同级别的连接器。

When I call a servlet which requires to connect the database, I've a blank page with a POST() method and an error 5OO with a GET() method. 当我调用一个需要连接数据库的servlet时,我有一个带有POST()方法的空白页面和一个带有GET()方法的错误50。 Here is the log error (I tried to translate it my best since i'm not english native) : 这是日志错误(我试着翻译它,因为我不是英国本土人):

description : this server has encountered an internal error which prevents it from fulfilling your request

exception

java.lang.NullPointerException
    com.picco.user.dao.UserDao.findByEmail(UserDao.java:40)
    com.picco.user.servlet.Index.doGet(Index.java:56)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

And here the part of the code concerned (but as I said, all codes using DAO have the same problem). 这是相关代码的一部分(但正如我所说,所有使用DAO的代码都存在相同的问题)。

try {
            UserDao udao = new UserDao();
            User u = udao.findByEmail(myCookieVal);
            SongDao sdao = new SongDao();
            ArrayList<Song> list = sdao.getAllSongs(u.getId());
            Random rd = new Random();
            int count = udao.count();
            request.setAttribute("currentSong", list.get(rd.nextInt(list.size())));
            request.setAttribute("songList", list);
            request.setAttribute("partyCount", count);
            this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            HsqlConnection.closeInstance();
        }

Dont hesitate to ask me for details if I did not describe enought the problem. 如果我没有描述问题,请不要犹豫,向我询问详细信息。

也许问题是您在实例化设置为null的连接之前尝试获取资源User。

The MySQL JDBC JAR belongs in the Tomcat /lib folder. MySQL JDBC JAR属于Tomcat /lib文件夹。 You want the Tomcat class loader to find it first. 您希望Tomcat类加载器首先找到它。

Shouldn't your DAO get a connection? 您的DAO不应该建立连接吗? I'd add it to the constructor. 我将其添加到构造函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM