简体   繁体   中英

Failed to connect MySQL in IDEA 12.1.4

I had learn javascript on the base of code of my web. Then, I tried to develop a new web but failed. After google, I still don't have clues. So I ask for help here. Thanks for advance.

My IDEA is 12.1.4. The install process is:
1) I make new project by selecting "JavaEE Web Module";
2) I then copy previously java script to deal with mysql, and a part of code would be listed below;

package inhouse;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.*;

public class DBTool {
    private DataSource ds = null;
    private Connection conn = null;
    private Statement stm = null;
    private ResultSet rs = null;
    private Context ctx = null;

    public DBTool() throws NamingException{
        this("editingphosphorylation");
    }

    public DBTool(String dbName) throws NamingException{
        try{
            ctx = new InitialContext();
            ds = (DataSource) ctx.lookup("java:comp/env/jdbc/" + dbName);
        }
        catch (NamingException e) {
            throw new NamingException("Can't get DataSource from pool: " + e.getMessage());
        }
    }

    public Connection getConnection() throws SQLException{
        conn = ds.getConnection();
        return conn;
    }
......
}

3) Add "resource-ref" to WEB-INF/web.xml;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/editingphosphorylation</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

4) Download "mysql-connector-java-5.1.40-bin.jar", and put it to WEB-INF/lib;
5) With "View-Tool Windows-Database", I confirm mysql database could be accessed;
6) But when start tomcat, it output following error:

HTTP Status 500 - An exception occurred processing JSP page /geneList.jsp at line 26

type Exception report

message An exception occurred processing JSP page /geneList.jsp at line 26

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /geneList.jsp at line 26

23: 
24:             try{
25:                 db = new DBTool();
26:                 conn = db.getConnection();
27:                 //Class.forName("com.mysql.jdbc.Driver").newInstance();
28:                 //Connection conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/editingphosphorylation","2pm","editingphosphorylation");
29:                 pst = conn.prepareStatement("select * from gene_sequence where geneName = ?");


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

javax.servlet.ServletException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
    org.apache.jsp.geneList_jsp._jspService(geneList_jsp.java:147)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    inhouse.DBTool.getConnection(DBTool.java:39)
    org.apache.jsp.geneList_jsp._jspService(geneList_jsp.java:85)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
    sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:524)
    sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:493)
    sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
    java.sql.DriverManager.getDriver(DriverManager.java:262)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    inhouse.DBTool.getConnection(DBTool.java:39)
    org.apache.jsp.geneList_jsp._jspService(geneList_jsp.java:85)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Any geneList.jsp is like this

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="inhouse.*, java.sql.*" %>
<%@ page import="java.io.*" %>

<html>
    <head>
        <title>get gene list</title>
    </head>
    <body>
        <%  String geneName = request.getParameter("geneName");
            DBTool db;
            Connection conn = null;
            PreparedStatement pst = null;
            ResultSet rs = null;

            try{
                db = new DBTool();
                conn = db.getConnection();
                //Class.forName("com.mysql.jdbc.Driver").newInstance();
                //Connection conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/editingphosphorylation","2pm","editingphosphorylation");
                pst = conn.prepareStatement("select * from gene_sequence where geneName = ?");
                pst.setString(1, geneName);
                rs = pst.executeQuery();
                if(rs.next()){
                    rs.beforeFirst();%>
                    <table>
                        <th>
                            <td>Gene Name</td><td>Transcript Name</td><td>DNA Sequence</td><td>Protein Sequence</td>
                        </th>
                        <%while(rs.next()){
                                String geneNameRetrieve = rs.getString("geneName");
                                String transcriptNameRetrieve = rs.getString("transcriptName");
                                String dnaSequenceRetrieve = rs.getString("dnaSequence");
                                String proteinSequenceRetrieve = rs.getString("proteinSequence"); %>
                        <tr>
                            <td><%=geneNameRetrieve%></td><td><%=transcriptNameRetrieve%></td><td><%=dnaSequenceRetrieve%></td><td><%=proteinSequenceRetrieve%></td>
                        </tr><%
                    }%>
                    </table>
        <%}else{%>
                    <div>No data.</div>
        <%}
                DBTool.close(rs, pst);
            }finally {
                DBTool.close(conn);
            }%>
    </body>
</html>

Compare with previously project, and according to google, I found the new project lack META-INF directory, so I make a new directory in web, and put a new context.xml into it.

<?xml version='1.0' encoding='utf-8'?>

<Context path="/">
    <Loader delegate="true" />
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>


    <Resource name="jdbc/editingprosphorylation" auth="Container" type="javax.sql.DataSource"
              maxActive="555" maxIdle="50" maxWait="10000"
          timeBetweenEvictionRunsMillis="60000"
          username="2pm" password="editingprosphorylation" driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/editingprosphorylation"/>        

</Context>

However, it doesn't work. After google, I hardly don't know how to do, Any suggestion would be grateful!

Typo:

editingphosphorylation (editingp H osphorylation)

editingprosphorylation (editingp R osphorylation)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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