Unable to create connection to GlassFish database with Netbeans?

邮差的信 提交于 2019-12-10 23:29:57

问题


I'm using Netbeans 8.1 beta and Glassfish server 4.1, Mysql.

Every time I create connection between my Webapplication with database it's unable to do it.

Index.jsp file:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%-- 
    Document   : index
    Created on : Sep 14, 2015, Sep 14, 2015 12:48:03 AM
    Author     : ESAJ
--%>
<link rel="stylesheet" type="text/css" href="style.css">



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Samir</title>
    </head>
    <body>
        <h1><span style="color:#FF0000;">IFPWAFCAD</span></h1>
        <table border="0">
            <thead>
                <tr>
                    <th>IFPWAFCAD offers expert counseling in a wide range of fields.</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <form action="response.jsp">
                            <strong>Select a subject:</strong>
                        </form>
                        <select name="subject_id">
                            <option></option>
                            <option></option>
                        </select>
                        <input type="submit" value="submit" />
                    </td>
                </tr>
                <tr>
                    <td>To view the contact details of an IFPWAFCAD certified former
                        professional wrestler in your area, select a subject below:</td>
                </tr>
            </tbody>
        </table>
        <sql:query var="subject" dataSource="jdbc/IFPWAFCAD">
            SELECT subject_id, name FROM Subject
        </sql:query>
        <table border="1">
            <!-- column headers -->
            <tr>
                <c:forEach var="columnName" items="${subjects.columnNames}">
                    <th><c:out value="${columnName}"/></th>
                    </c:forEach>
            </tr>
            <!-- column data -->
            <c:forEach var="row" items="${subjects.rowsByIndex}">
                <tr>
                    <c:forEach var="column" items="${row}">
                        <td><c:out value="${column}"/></td>
                    </c:forEach>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

Glassfish resources.xml fiel:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
  <jdbc-resource enabled="true" jndi-name="jdbc/IFPWAFCAD" object-type="user" pool-name="IfpwafcadPool">
    <description/>
  </jdbc-resource>
  <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="IfpwafcadPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.ConnectionPoolDataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
    <property name="URL" value="jdbc:mysql://localhost:3306/mynewdatabase?zeroDateTimeBehavior=convertToNull"/>
    <property name="User" value="root"/>
    <property name="Password" value="admin"/>
  </jdbc-connection-pool>
</resources>

I already copy the Jar file to glassfish lib, also I already add reference to web.xml.

I face this error:

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

exception

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/IFPWAFCAD"

root cause

javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/IFPWAFCAD"


回答1:


Looks like u are missing jdbc driver for MySql. Download it from here

http://dev.mysql.com/downloads/connector/j/

unzip and copy mysql-connector-java-5.x.xx-bin.jar it into $glassfish_install_folder\glassfish\lib

User proper driver which match jdk and mysql version u are using to avoid errors.




回答2:


After many hours of trying to get Netbeans (8.2) to work with Glassfish and in particular JDBC, I ditched Glassfish as I realized it was discontinued. Instead installed Payara which seems to be a fork. Pretty easy in that you just use Glassfish settings in Netbeans, just point to a new direction. To get the Connection Pool and JDBC Resources to show up, go to the admin console of Paraya by right clicking the server in Netbeans and choose "View Domain Admin Control". Go to JDBC Connection Pool and manually add "IpwafcadPool". Remember to add properties for User (root) and Password (root). URL has two properties, one upper and one lower case. I changed both to jdbc:mysql://localhost:3306/MyNewDatabase?zeroDateTimeBehavior=convertToNull.

But then for this above issue. I can just confirm that the connector driver must be updated to the latest. I tried with mysql-connector-java-5.1.40-bin.jar (in lib folder), restarted Paraya and now it works like a charm. Now I can FINALLY continue with the tutorial https://netbeans.org/kb/docs/web/mysql-webapp.html. Yep, I learned a lot by troubleshooting this, so thanks for that.



来源:https://stackoverflow.com/questions/32571575/unable-to-create-connection-to-glassfish-database-with-netbeans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!