与MySql ERROR的Java EE连接

时间:2015-11-11 13:19:51

标签: java mysql tomcat java-ee servlets

我尝试连接到我的Java EE项目上的mysql数据库,但是我收到错误,我不知道问题出在哪里。 这是SERVLET,CLASS和ERRORS

Java EE Servlet:

package com.hopital.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hopital.database.Recup_Insert;
import com.hopital.forms.Connections;


@WebServlet("/hopital")
public class hopital extends HttpServlet {
    private static final long serialVersionUID = 1L;   

    public hopital() {
        super();

    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//extrait les cookies
        Cookie[] cookies= request.getCookies();
        if(cookies != null){
            for(Cookie cookie: cookies){
                if(cookie.getName().equals("login")){
                    request.setAttribute("login", cookie.getValue());
                }
            }
        }
//session
        HttpSession session= request.getSession();
        String login= (String) session.getAttribute("login");
        request.setAttribute("login", login);

//extrait les chambers

        Recup_Insert tableChamber= new Recup_Insert();
        request.setAttribute("chambres",tableChamber.recup_chmabre());


        this.getServletContext().getRequestDispatcher("/WEB-INF/home.jsp").forward(request,response);
    }






    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//verification de champs
        Connections form= new Connections();
        form.verifie(request);
        request.setAttribute("form", form);

//stocke les champs entre
        String login=request.getParameter("login");
        String pass=request.getParameter("pass");

//cree un cookies
        Cookie cookie=new Cookie("login",login);
        cookie.setMaxAge(60*60*24*30);
        response.addCookie(cookie);

// cree un session sur login et mot de passe
        HttpSession session= request.getSession();
        session.setAttribute("login", login);
        session.setAttribute("pass", pass);

        this.getServletContext().getRequestDispatcher("/WEB-INF/home.jsp").forward(request,response);
    }

}

我的包含数据库连接的类:

package com.hopital.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import com.hopital.projet.chamber;


public class Recup_Insert {
    public List<chamber> recup_chmabre(){

        List<chamber> chambers= new ArrayList<chamber>();

//charger les drivers
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch( ClassNotFoundException e){
        }
//Connexion
        Connection connexion=null;
        Statement statement=null;
        ResultSet resultat = null;
        try {
            connexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/hopital", "root", "3636");

            statement= connexion.createStatement();

            resultat= statement.executeQuery("SELECT * FROM CHAMBER;");

            while(resultat.next()){
                int etage = resultat.getInt("etage");
                int ID_pat = resultat.getInt("ID_pat");
                int num = resultat.getInt("num");
                int nom_lits = resultat.getInt("nom_lits");
                int occupee = resultat.getInt("occupee");

                chamber chamber = new chamber();
                chamber.setEtage(etage);
                chamber.setID_pat(ID_pat);
                chamber.setNum(num);
                chamber.setOccupee(occupee);
                chamber.setNom_lits(nom_lits);

                chambers.add(chamber);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try{
                if(resultat != null)resultat.close();
                if(statement != null)statement.close();
                if(connexion != null)connexion.close();

            }catch(SQLException ignore){
            }
        }


        return chambers;

    }

}

这是运行后的错误:

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2252)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.hopital.database.Recup_Insert.recup_chmabre(Recup_Insert.java:29)
    at com.hopital.servlet.hopital.doGet(hopital.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:214)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
    ... 39 more

3 个答案:

答案 0 :(得分:0)

检查连接URL和凭据。尝试使用某个SQL客户端连接相同的参数。

答案 1 :(得分:0)

您收到此错误,因为您无法连接到数据库。 很可能你没有启动服务器。检查它是否正在运行,然后仔细检查它运行的端口。还要检查数据库中表名的拼写,它应该是“hopital”。

答案 2 :(得分:0)

检查添加到属性=&gt; libraries =&gt; mysql-connector-java-5.1.21-bin.jar 文件 如果没有那么添加外部jar =&gt; mysql-connector-java-5.1.21-bin.jar

还要检查tomcat服务器和mysql的端口号