我正在尝试第一次创建J2EE应用程序。
我在MySQL上创建了我的数据库,现在我想用我的应用程序在屏幕上获取所有数据。
问题是:当我想连接MySQL数据库上的应用程序时,我有错误:
java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)
我正在使用:
server.xml(在c:/ serveur / tomcat / conf /中)
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<!-- JNDI de la base nouvellebd -->
<Resource name="base_test"
type="javax.sql.DataSource"
password=""
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="500"
username="root"
url="jdbc:mysql://127.0.0.1/nouvellebd?autoReconnect=true"
maxActive="500"
/>
我的“上下文”(在conf / catalina / localhost / ProjetServlet.xml中):
<Context path="/ProjetServlet"
reloadable="true"
docBase="c:\MonEspace\ProjetServlet"
workDir="c:\MonEspace\ProjetServlet\work" >
<ResourceLink
name="base_test"
global="base_test"
type="javax.sql.DataSource"/>
</Context>
我的代码(AffichePersonne.java):
package mesServlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class AffichePersonne extends HttpServlet {
Connection BD;
DataSource ds;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
BD=ds.getConnection();
Statement s = BD.createStatement();
ResultSet r = s.executeQuery("select * from personne");
PrintWriter out=null;
resp.setContentType("text/html");
out = resp.getWriter();
out.println("<html>");
out.println("<head><title> Test servlet </title></head>");
out.println("<body>");
out.println("Contenu de la table personne <BR>");
out.println("<table>");
out.println("<TR>");
out.println("<TD>Nom</TD>");
out.println("<TD>Prénom</TD>");
out.println("</TR>");
while (r.next()) {
out.println("<TR>");
out.println("<TD>");
out.println(r.getString("nom"));
out.println("</TD>");
out.println("<TD>");
out.println(r.getString("prenom"));
out.println("</TD>");
out.println("</TR>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
r.close();
s.close();
BD.close();
s = null;
r = null;
} catch (java.sql.SQLException ex) {
System.out.println("Erreur d'exécution de la requête SQL \n"+ex);
}
}
public void init() throws ServletException {
try {
System.out.println("Récupération du contexte");
Context initCtx = new InitialContext();
System.out.println("lookup de env");
Context envCtx = (Context) initCtx.lookup("java:comp/env");
System.out.println("lookup de base_test");
ds=(DataSource) envCtx.lookup("base_test");
//System.out.println("Datasource chargée");
}
catch(Exception er) {
System.out.println("Erreur de chargement du contexte " + er);
} }
}
“mapping”(web.xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Mes servlets</display-name>
<description>
Premi�res servlets
</description>
<servlet>
<servlet-name>ServletHello</servlet-name>
<servlet-class>mesServlets.HelloServlet</servlet-class>
<description>Servlet d'essai</description>
</servlet>
<servlet-mapping>
<servlet-name>ServletHello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ServletBD</servlet-name>
<servlet-class>mesServlets.AffichePersonne</servlet-class>
<description>Servlet d'essai de la connection BD</description>
</servlet>
<servlet-mapping>
<servlet-name>ServletBD</servlet-name>
<url-pattern>/essaibd</url-pattern>
</servlet-mapping>
</web-app>
非常感谢您的帮助:)