| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2344 人关注过本帖
标题:大神帮帮忙看看,一个连接数据库比读取数据的 servlet,运行出现 405 错误, ...
只看楼主 加入收藏
我爱敲代码
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:70
专家分:165
注 册:2013-4-23
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:1 
大神帮帮忙看看,一个连接数据库比读取数据的 servlet,运行出现 405 错误,求指点!!!
package com.sun.java.lesson10;
public class Contants {
   public static final String driver="com.mysql.jdbc.Driver";
   public static final String url="jdbc:mysql://localhost:3306/javaweb";
   public static final String user="root";
   public static final  String password="504979454";
}


package com.sun.java.lesson10;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import *;
import com.sun.java.lesson10.Contants;
public class JdbcServlet extends HttpServlet {
    public void init() throws ServletException {
        super.init();
        try{
            Class.forName(Contants.driver).newInstance();
        }catch(Exception e){
            e.printStackTrace();
    }
    }
    protected void Server(HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException, InstantiationException, IllegalAccessException {
        Connection conn=null;
           Statement  st=null;
            ResultSet re=null;
            response.setContentType("text/html;charset=gb2312");
        PrintWriter out=response.getWriter();
         out.println("<html>");
            out.println("<head><title>网页标题</title></head>");
            out.println("<body>");
            out.println("网页内容");
            out.println("</body></html>");
        try{
            conn=DriverManager.getConnection(Contants.url,Contants.user,Contants.password);
        }catch(SQLException e){
        out.println("连接数据库失败");
        }
        try{
            st=conn.createStatement();
            String str="select * from user";
            re=st.executeQuery(str);
            while(re.next()){
                out.print("Id:"+re.getInt(1));
                 out.print("  用户名:"+re.getString(2));
                 out.print("  密码:"+re.getString(3));
                 out.println("<br>");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        try{
            while(re!=null){
            re.close();
            re=null;
            }
            while(st!=null){
            st.close();
            st=null;
            }
            while(conn!=null){
            conn.close();
            conn=null;
            }
            }catch(SQLException e){
            out.println("数据库关闭失败");
            }
    }
    public void destroy() {
        
    }
}


运行报错如下:

HTTP Status 405 - HTTP method GET is not supported by this URL

--------------------------------------------------------------------------------

type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource.


--------------------------------------------------------------------------------

Apache Tomcat/6.0.39
搜索更多相关主题的帖子: localhost 连接数据库 password package javaweb 
2014-05-30 00:32
shaw80
Rank: 2
等 级:论坛游民
威 望:1
帖 子:29
专家分:60
注 册:2014-5-31
收藏
得分:20 
405是service方法名称错误吧,你的方法名写错了
2014-05-31 14:34
快速回复:大神帮帮忙看看,一个连接数据库比读取数据的 servlet,运行出现 405 ...
数据加载中...
 
   



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

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