第一个文件::::
package cn.mldn.zheng.dao;
import java.util.*;
import cn.mldn.zheng.vo.*;
//规定出了操作person表在此项目里的全部方法
public interface PersonDAO
{       
       //增加操作
   public void insert(Person person) throws Exception;
   //修改操作
       public void update(Person person) throws Exception;
   //删除操作
   public void delete(String id) throws Exception;
   //按ID查询操作
   public Person queryById(String id) throws Exception;
   //查询全部
   public List queryAll() throws Exception;
   //模糊查询
       public List queryByLike(String cond) throws Exception;
   
}第二个文件::::::
package cn.mldn.zheng.vo;
//值对象:包括 属性、setter、getter方法
public class Person
{       
       private String id;
   private String name;
   private String password;
   private int  age;
   private String email;
public void setId(String id)
{
this.id=id;
}
public void setName(String name)
{
this.name=name;
}
public void setPassword(String password)
{
this.password=password;
}
public void setAge(int age)
{
this.age=age;
}
public void setEmail(String email)
{
     this.email=email;
}
public String getId()
{
return this.id;
}
public String getName()
{
return this.name;
}
public String getPassword()
{
return this.password;
}
public int getAge()
{
return this.age;
}
public String getEmail()
{
return this.email;
}
};第三个文件::::::
package cn.mldn.zheng.dbc;
import java.sql.*;
public class DateBaseConnection
{     
         private final String DBDRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
         private final String DBURL="jdbc:odbc:dao";
         private Connection conn=null;
      public DateBaseConnection()
    {
  
  try
              {
            Class.forName(DBDRIVER);
                    this.conn=DriverManager.getConnection(DBURL);
              }
         catch(Exception e)
              {
                     System.out.println("连接异常!!!!!");
              }
       }    public Connection getConnection()
   {   
    return  this.conn;   
   }
   public void close()
   {
   try
   {
  this.conn.close();
   }
   catch (Exception e)
   {
   }
   
   }
}
第四个文件::::;
package cn.mldn.zheng.dao.Impl;
import java.sql.*;
import java.util.*;
import cn.mldn.zheng.dao.*;
import cn.mldn.zheng.vo.*;
import cn.mldn.zheng.dbc.*;
//此类需要完成具体的数据库操作
public class  PersonDAOImpl implements PersonDAO
{
//1.增加操作
   public void insert(Person person) throws Exception
   {
              String sql="INSTER INTO person(id,name,password,age,email) VALUES(?,?,?,?,?)";
  PreparedStatement pstmt=null;
  DateBaseConnection dbc=null;
  
     try
     {
 //连接数据库
  dbc=new DateBaseConnection();
  pstmt=dbc.getConnection().prepareStatement(sql);
              pstmt.setString(1,person.getId());
  pstmt.setString(2,person.getName());
  pstmt.setString(3,person.getPassword());
  pstmt.setInt(4,person.getAge());
  pstmt.setString(5,person.getEmail());
  //进行数据库更新操作
  pstmt.executeUpdate();
  //关闭数据库
              pstmt.close();
     }
     catch (Exception e)
     {
 throw new Exception("出现异常!!!!!!");
     }
     
   finally
   {
              //关闭数据库连接
  dbc.close();
   }
   }
   //2.修改操作
       public void update(Person person) throws Exception
   {
              String sql="UPDATE  person   SET  name=?, password=?, age=?, email=?  WHERE id=?";
  PreparedStatement pstmt=null;
  DateBaseConnection dbc=null;
  
   //下面是针对数据库的具体操作
     try
     {
 //连接数据库
  dbc=new DateBaseConnection();
  pstmt=dbc.getConnection().prepareStatement(sql);
  pstmt.setString(1,person.getName());
  pstmt.setString(2,person.getPassword());
  pstmt.setInt(3,person.getAge());
  pstmt.setString(4,person.getEmail());
              pstmt.setString(5,person.getId());
  //进行数据库更新操作
  pstmt.executeUpdate();
  //关闭数据库
              pstmt.close();
     }
     catch (Exception e)
     {
 throw new Exception("出现异常!!!!!!");
     }
     
   finally
   {
              //关闭数据库连接
  dbc.close();
   }
   }
   //3.删除操作
   public void delete(String id) throws Exception
   {
              String sql="DELETE FEOM  person  WHERE id=?";
  PreparedStatement pstmt=null;
  DateBaseConnection dbc=null;
  
   //下面是针对数据库的具体操作
     try
     {
 //连接数据库
  dbc=new DateBaseConnection();
  pstmt=dbc.getConnection().prepareStatement(sql);
              pstmt.setString(1,id);
  //进行数据库更新操作
  pstmt.executeUpdate();
  //关闭数据库
              pstmt.close();
     }
     catch (Exception e)
     {
 throw new Exception("出现异常!!!!!!");
     }
     
   finally
   {
              //关闭数据库连接
  dbc.close();
   }
   }
   //4.按ID查询操作
   public Person queryById(String id) throws Exception
   {
              Person person = null;
  String sql= "SELECT id,name,password,age,email FROM person WHERE id=?";
              PreparedStatement pstmt=null;
  DateBaseConnection dbc=null;
  
   //下面是针对数据库的具体操作
     try
     {
 //连接数据库
  dbc=new DateBaseConnection();
  pstmt=dbc.getConnection().prepareStatement(sql);
  //进行数据库查询操作
  ResultSet rs = pstmt.executeQuery();
  if(rs.next())
  {
       //查询出内容,之后将查询出的内容赋值给person
   person=new Person();
   person.setId(rs.getString(1));
   person.setName(rs.getString(2));
   person.setPassword(rs.getString(3));
   person.setAge(rs.getInt(4));
   person.setEmail(rs.getString(5));
   //将查询的对象加入到person中来
   
  }
  //关闭数据库
  rs.close();
              pstmt.close();
     }
     catch (Exception e)
     {
 throw new Exception("出现异常!!!!!!");
     }
     
   finally
   {
              //关闭数据库连接
  dbc.close();
   }
          return person;
}
   //5.查询全部
   public List queryAll() throws Exception
   {
              List all= new ArrayList();
  String sql= "SELECT id,name,password,age,email FROM person";
              PreparedStatement pstmt=null;
  DateBaseConnection dbc=null;
  
   //下面是针对数据库的具体操作
     try
     {
 //连接数据库
  dbc=new DateBaseConnection();
  pstmt=dbc.getConnection().prepareStatement(sql);
  //进行数据库查询操作
  ResultSet rs = pstmt.executeQuery();
  while(rs.next())
  {
       //查询出内容,之后将查询出的内容赋值给person
   Person person=new Person();
   person.setId(rs.getString(1));
   person.setName(rs.getString(2));
   person.setPassword(rs.getString(3));
   person.setAge(rs.getInt(4));
   person.setEmail(rs.getString(5));
   all.add(person);
  }
  //关闭数据库
  rs.close();
              pstmt.close();
     }
     catch (Exception e)
     {
 throw new Exception("出现异常!!!!!!");
     }
     
   finally
   {
              //关闭数据库连接
  dbc.close();
   }
          return all;
   }    //6.模糊查询
       public List queryByLike(String cond) throws Exception
   {
              List all= new ArrayList();
  String sql= "SELECT id,name,password,age,email from person WHERE name like;or email like";
              PreparedStatement pstmt=null;
  DateBaseConnection dbc=null;
  
   //下面是针对数据库的具体操作
     try
     {
 //连接数据库
  dbc=new DateBaseConnection();
  pstmt=dbc.getConnection().prepareStatement(sql);
  //设置模糊查询条件
              pstmt.setString(1,"%+cond+%");
              pstmt.setString(2,"%+cond+%");
  //进行数据库查询操作
  ResultSet rs = pstmt.executeQuery();
  while(rs.next());
  {
       //查询出内容,之后将查询出的内容赋值给person
   Person person=new Person();
   person.setId(rs.getString(1));
   person.setName(rs.getString(2));
   person.setPassword(rs.getString(3));
   person.setAge(rs.getInt(4));
   person.setEmail(rs.getString(5));
   all.add(person);
  }
  //关闭数据库
  rs.close();
              pstmt.close();
     }
     catch (Exception e)
     {
 throw new Exception("出现异常!!!!!!");
     }
     
   finally
   {
              //关闭数据库连接
  dbc.close();
   }
          return all;
   }
}
jsp文件::::::::
<%@page contentType="text/html;charset=utf-8"%>
<%@page import="cn.mldn.zheng.vo.*"%>
<%@page import="cn.mldn.zheng.dao.*"%>
<%@page import="cn.mldn.zheng.dao.Impl.*"%>
<%@page import="cn.mldn.zheng.dbc.*"%>
<%
     //进行插入操作
 Person person=new Person();
 person.setName("郑武周");
 person.setPassword("zheng");
 person.setAge(23);
 person.setEmail("[email protected]");
 PersonDAO dao = new PersonDAOImpl();
 try
         {
               dao.insert(person);
         }
         catch(Exception e)
         {
         }
         
        

%>
都能运行 就是数据插不进去   什么原因  快疯了!!!!!!!!!!!各位帮忙看看!!!!!!

解决方案 »

  1.   

    可能不是插不进去,而是楼主在添加后没有submit,所以数据库就没数据啦,,,,
      

  2.   

    没报错的话就是缺少submit,肯定的all.add(person);
    在这句话后添加submit方法
      

  3.   

    一般检查下面三个要点:
    1.检查连接数据库部分的语句是否有错。
    2.检查sql语句是否有语法错误。
    3.检查表字段与sql语句字段是否一致。
      

  4.   

    怎么 添加   submit    我是一新手   不太会弄啊   您帮我改一下拜
      

  5.   

    没报错?这原因可能是压根就没执行到这个方法!你用debug断点看看!还有楼主的  private final String DBURL="jdbc:odbc:dao";你的url确定是这样?
      

  6.   

    页面中你的添加按钮可能是button,你把它改成submit提交表单就可以了
      

  7.   

    1. 好习惯打印一下异常catch(Exception e)
      {
    // 把异常打印一下
      }2.插入数据库是涉及到事务了,最好设置一下事务提交,或者设置为自动提交3.PersonDaoImpl中catch (Exception e)
    {
    throw new Exception("出现异常!!!!!!");
    }向外抛异常,外面接受了也最好打印出来