当前位置: 代码迷 >> Java Web开发 >> 遇到问题,求高手相助!非常感谢!
  详细解决方案

遇到问题,求高手相助!非常感谢!

热度:462   发布时间:2011-04-06 17:14:26.0
遇到问题,求高手相助!非常感谢!
大家好!
        我写了一个连接数据库的类,然后在servlet类中实例化一个对象,然后对数据库进行操作!然而报错,提示如下:
        2011-4-6 16:44:32 org.apache.catalina.startup.HostConfig checkResources
信息: Reloading context [/DownLoad1.0]
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

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(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:690)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1093)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2338)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2371)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2163)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:378)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at com.bean.Dao.open(Dao.java:21)
    at com.control.Logic.service(Logic.java:20)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2539)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:614)
    ... 30 more
    望高手给予指点!
    非常感谢!
程序代码如下:Dao.java
package com.bean;

import java.sql.*;
                        
public class Dao {
    private String user;
    private String password;
    private String url;
    private Statement stmt;
    private Connection conn;                 
   
    public Dao()
    {
        url="jdbc:mysql://localhost:8080/phone?characterEncoding=UTF-8";
        user="root";
        password="123";
    }
    public void open() throws ClassNotFoundException, SQLException
    {
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection(url,user,password);
        stmt=conn.createStatement();
    }
    public void insert(String sql) throws SQLException
    {
        stmt.execute(sql);
    }
    public void close() throws SQLException
    {
        if(stmt!=null)
            stmt.close();
        if(conn!=null)
            conn.close();
    }
   
}
logic.java代码:
package com.control;

import com.bean.Dao;
import com.bean.Websit;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Logic extends HttpServlet {
    private Dao dao=new Dao();
    public void init() throws ServletException {
        try {
            dao.open();
        } catch (ClassNotFoundException e) {                  
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    protected void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
      
    }
    public void destroy() {
        try {
            dao.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


搜索更多相关主题的帖子: 数据库  

----------------解决方案--------------------------------------------------------
有没有吧JDBC驱动复制到项目中去啊、
----------------解决方案--------------------------------------------------------
嗯.没驱动!
----------------解决方案--------------------------------------------------------
  相关解决方案