一个简单的servlet注册试验的一些问题(写不到数据库,望高手指点)

程序实现目标:写到数据库即可
目录结构:


UserBean.java
package  com.study.bean;

public   class  UserBean {
    
    
private  String username;
    
private  String password;
    
public  String getUsername() {
        
return  username;
    }
    
public   void  setUsername(String username) {
        
this .username  =  username;
    }
    
public  String getPassword() {
        
return  password;
    }
    
public   void  setPassword(String password) {
        
this .password  =  password;
    }

    

}
UserDao.java
package  com.study.dao;

import  java.sql.Connection;
import  java.sql.PreparedStatement;
import  java.sql.SQLException;

import  com.study.bean. * ;
import  com.study.db.DBHelper;

public   class  UserDao {
    
/**
     * 
     * 增加用户
     *
     
*/

    
public   void  setUser(UserBean tuser)
    {
        String sql 
=   " INSERT INTO T_USER VALUES(null,?,?) " ; // USER_NAME=?,USER_PWD=?;
        PreparedStatement pstat  =   null ;
        Connection conn 
=   new  DBHelper().getConn();
        
try  {
            pstat 
=  conn.prepareStatement(sql);
          
            pstat.setString(
1 , tuser.getUsername());
            pstat.setString(
2 , tuser.getPassword());

            
        } 
catch  (SQLException e) {
            
//  TODO Auto-generated catch block
  

        }  finally  {
            
try  {
                
if  (conn  !=   null )
                    conn.close();
                
if  (pstat  !=   null )
                    pstat.close();
            } 
catch  (SQLException e) {
                
//  TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
   

    }
}
DBHelper.java
package  com.study.db;


import  java.sql.Connection;
import  java.sql.DriverManager;
import  java.sql.SQLException;
/**
 * 
 * 数据库连接类
 *
 
*/
public   class  DBHelper {
    
private  String driver  =   " org.gjt.mm.mysql.Driver " ;
    
private  String url  =   " jdbc:mysql://localhost:3306/first " ;
    
private  String user  =   " root " ;
    
private  String pwd  =   " 0433gygy " ;
    
    
public  Connection getConn(){
        Connection conn 
=   null ;
        
try  {
            Class.forName(driver);
            conn 
=  DriverManager.getConnection(url, user, pwd);
        } 
catch  (ClassNotFoundException e) {
            
//  TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch  (SQLException e) {
            
//  TODO Auto-generated catch block
            e.printStackTrace();
        }
        
return  conn;
    }
    
public   static   void  main(String[] args) {
        System.out.println(
" DBHelper.main() "   +   new  DBHelper().getConn());
    }
}
Register.java
package  com.study.servlet;


import  javax.servlet.http. * ;
import  javax.servlet. * ;

import  java.io. * ;

import  com.study.bean. * ;
import  com.study.dao. * ;

public   class  Register  extends  HttpServlet{
    
public   void  doGet(HttpServletRequest request,HttpServletResponse response)
    
throws  ServletException,IOException
    {    
        String un 
=  request.getParameter( " username " );
        String pd 
=  request.getParameter( " password " );
        
        
        UserBean Tuser 
=   new  UserBean();
        Tuser.setPassword(pd);
        Tuser.setUsername(un);
//         PrintWriter out = response.getWriter();
//         out.println("<html>");
//         out.println("<marquee>"+Tuser.getUsername()+"</marquee>");
//         out.println("</html>");
        UserDao dao  =   new  UserDao();    
        dao.setUser(Tuser);
        
    }

     
public   void  doPost(HttpServletRequest request, HttpServletResponse response)
     
throws  ServletException, IOException
   {
       doGet(request, response);
        
   }
}
web.xml
< servlet >
    
< servlet-name > first </ servlet-name >
    
< servlet-class > com.study.servlet.Register </ servlet-class >
  
</ servlet >
  
< servlet-mapping >
    
< servlet-name > first </ servlet-name >
    
< url-pattern > /first </ url-pattern >
   

  
</ servlet-mapping >
reg.jsp
< body >
    
< form  action ="first"  method  = "post" >
    username:
< input  type ="text"  name ="username" >
    password:
< input  type ="password"  name ="password" >
    
< input  type ="submit"   >
    
</ form >
  
</ body >

在reg.jsp 自己随便提交个表单.
程序很简单,没什么东西,结果就是写不到数据库,整个程序运行没有报错!望高手指点一下哪边有问题了.



你可能感兴趣的:(一个简单的servlet注册试验的一些问题(写不到数据库,望高手指点))