| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1221 人关注过本帖
标题:遇到问题,求高手相助!非常感谢!
只看楼主 加入收藏
meikebo
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-4-6
结帖率:0
收藏
已结贴  问题点数:0 回复次数:2 
遇到问题,求高手相助!非常感谢!
大家好!
        我写了一个连接数据库的类,然后在servlet类中实例化一个对象,然后对数据库进行操作!然而报错,提示如下:
        2011-4-6 16:44:32 org.apache.catalina.startup.HostConfig checkResources
信息: Reloading context [/DownLoad1.0]
com.mysql.jdbc.exceptions. 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.(AprEndpoint.java:1665)
    at java.lang.Thread.run(Thread.java:619)
Caused by: 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
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();
        }
    }
}


搜索更多相关主题的帖子: 数据库 
2011-04-06 17:14
myhnuhai
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:3
帖 子:425
专家分:1725
注 册:2010-3-17
收藏
得分:5 
有没有吧JDBC驱动复制到项目中去啊、

不要让肮脏的记忆,迷失了原本纯洁的心灵!
2011-04-07 11:49
蓝色石头
Rank: 2
等 级:论坛游民
帖 子:13
专家分:70
注 册:2009-9-17
收藏
得分:5 
嗯.没驱动!
2011-04-07 13:50
快速回复:遇到问题,求高手相助!非常感谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019674 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved