servlet 页面转发连带数据库

今天上了一次javaEE课,课后老师布置了作业是用servlet实现登陆和注册,主要是练习数据库的使用,我写的注册页面servlet代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response, String sql)    throws ServletException, IOException { 
  int x=0;   
 String username = request.getParameter("username"); 
   String password1 = request.getParameter("password1"); 
   sql = "insert into users (username,password) values (' "+username+" ','"+password1+"')";  
  Dbcon con = new Dbcon(  );
      try{   
    x = con.ex(sql);  
     }catch(Exception e){    
   e.printStackTrace(); 
    }   
    if( x > 0)   {    
    request.getRequestDispatcher("registersuccess.jsp").forward(request,response); 
    }   else
     {    
    request.getRequestDispatcher("error.jsp").forward(request, response);
       }   
  con.close(); 
 }
这里用到的Dbcon.java代码如下:
package com.lsxy.db;  
import java.sql.Connection;
 import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException;
 import java.sql.Statement; 
 public class Dbcon {   
  public Connection con;    
 public Statement stmt;    
 public String url = "jdbc:mysql://localhost:3306/me";    
 public String username = "root";    
 public String password = "root";   
 public Dbcon() {     
  try {    
  Class.forName("com.mysql.jdbc.Driver");    
   con = DriverManager.getConnection(url, username, password);     
  stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE ,ResultSet.CONCUR_UPDATABLE);  
   } catch (ClassNotFoundException e)
   {    
   // TODO Auto-generated catch block   
    e.printStackTrace();  
   } catch (SQLException e)
   {    
   // TODO Auto-generated catch block    
   e.printStackTrace();
     }   
 
 public ResultSet query(String sql){    
    ResultSet rs = null;  
   try {    
    rs = stmt.executeQuery(sql);   
    } catch (SQLException e) {
         // TODO Auto-generated catch block    
     e.printStackTrace();  
     }     
   return rs;    
 
 public int ex(String sql){   
  int count = 0;    
    try {    
   count = stmt.executeUpdate(sql);   
  } catch (SQLException e) {    
   // TODO Auto-generated catch block  
     e.printStackTrace();   }   return count; 
   
 public void close(){   
 try {    
  stmt.close();   
   con.close();  
  } catch (SQLException e) { 
      // TODO Auto-generated catch block  
     e.printStackTrace();  
   }   
 
出现的错误如下: HTTP Status 405 - HTTP method POST is not supported by this URL type Status report message HTTP method POST is not supported by this URL description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL). Apache Tomcat/6.0.32
大家看着代码,能知道是什么地方出错了吗?我也没有想到居然用Myeclipse写的时候我把sql没定义,在dopost()方法里面出错,然后不经意鼠标一点sql她就变成参数传给dopost了,所以提示上面的错误,错误原因是public void doPost(HttpServletRequest request, HttpServletResponse response)没有重写,其实我重载了public void doPost(HttpServletRequest request, HttpServletResponse response, String sql)。
大家在享受工具带给我们便捷的同时,可要千万当心这种致命小错误的发生啊,它同样可以整死你,你甚至连怀疑的机会都没有!!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值