解决方案 »

  1.   

    我是LZ,补充一下,我试验过,进行数据库的“增”和“改”操作可行,但是出现异常,但是那个根据id查询就没成功,也出现了异常。
      

  2.   

    是不是还是少jar包或者jar包有问题
      

  3.   

    会不会是jar包版本问题呢?或者你用了什么框架中间有冲突啊?您还是把调用语句也贴出来吧,光看这个看不出来什么啊
      

  4.   

    好的,我贴一下调用的程序,增和改操作能成功,jar包也可能有问题吗?//更新操作都用preparedStatement,都要将确保数据库关闭
    package com.lww.UserProject.DAO.impl;import com.lww.UserProject.DAO.*;
    import com.lww.UserProject.jdbc.*;
    import com.lww.UserProject.vo.*;import java.sql.*;
    import java.util.*;public class IUserDAOImpl implements IUserDAO {
    //声明dbc,conn
    private DatabaseConnection dbc=null;
    private Connection conn=null;
    public IUserDAOImpl(){
    this.dbc=new DatabaseConnection();
    this.conn=this.dbc.getConnection();
    } @Override
    /*
     *进行“增”的操作 
     *
     */
    public boolean doCreate(User user) throws Exception {
    // TODO Auto-generated method stub
    boolean flag=false;
    PreparedStatement pstmt = null;
    String sql="INSERT INTO user (name,sex,birthday) VALUES (?,?,?)";
    try{
    pstmt=conn.prepareStatement(sql);
    pstmt.setString(1,user.getName());
    pstmt.setString(2, user.getSex());
    pstmt.setDate(3,new java.sql.Date(user.getBirthday().getTime()));
    if(pstmt.executeUpdate()>0){//至少更新了一次
    flag=true;
    }
    }
    catch(Exception e){
    throw e;
    }
    finally{
    if(pstmt != null){
    try{
    pstmt.close();
    }
    catch(Exception e){
    throw e;
    }
    }
    try{
    conn.close();
    }
    catch(SQLException e){
    throw e;
    }

    }

    return flag;
    }
        //进行更新操作
    @Override
    public boolean doUpdate(User user) throws Exception {
    boolean flag=false;
    PreparedStatement pstmt = null;
    String sql="UPDATE user SET name=?,sex=?,birthday=? WHERE id=?";
    try{
    pstmt=conn.prepareStatement(sql);
    pstmt.setString(1,user.getName());
    pstmt.setString(2, user.getSex());
    pstmt.setDate(3,new java.sql.Date(user.getBirthday().getTime()));
    pstmt.setInt(4, user.getId());
    if(pstmt.executeUpdate()>0){//至少更新了一次
    flag=true;
    }
    }
    catch(Exception e){
    throw e;
    }
    finally{
    if(pstmt != null){
    try{
    pstmt.close();
    }
    catch(Exception e){
    throw e;
    }
    }
    try{
    conn.close();
    }
    catch(SQLException e){
    throw e;
    }
    }

    return flag;
    }
        //进行删除操作
    @Override
    public Boolean doDelete(int id) throws Exception {
    boolean flag=false;
    PreparedStatement pstmt = null;
    String sql="DELETE FROM user WHERE id=?";
    try{
    pstmt=conn.prepareStatement(sql);
    pstmt.setInt(1, id);
    if(pstmt.executeUpdate()>0){//至少更新了一次
    flag=true;
    }
    }
    catch(Exception e){
    throw e;
    }
    finally{
    if(pstmt != null){
    try{
    pstmt.close();
    }
    catch(Exception e){
    throw e;
    }
    }
    try{
    conn.close();
    }
    catch(SQLException e){
    throw e;
    }
    }

    return flag;
    }
        //通过关键词,进行查询
    public List<User> findALL(String keyWord) throws Exception {
    List<User> all=new ArrayList<User>();
    PreparedStatement pstmt = null;
    String sql="SELECT id,name,sex,birthday FROM user WHERE name LIKE ?,sex LIKE ?,birthday LIKE ?";
    try{
    pstmt=conn.prepareStatement(sql);
    pstmt.setString(1, "%"+keyWord+"%");
    pstmt.setString(2, "%"+keyWord+"%");
        pstmt.setString(3, "%"+keyWord+"%");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()){
         User user=new User();
         user.setId(rs.getInt(1));
         user.setName(rs.getString(2));
         user.setSex(rs.getString(3));
         user.setBirthday(rs.getDate(4));
         all.add(user);
        }
        rs.close();

    }
    catch(Exception e){
    throw e;
    }
    finally{
    if(pstmt != null){
    try{
    pstmt.close();
    }
    catch(Exception e){
    throw e;
    }
    }

    conn.close();

    }
    return all;
    }
    //通过id,查询
    public User findByID(int id) throws Exception{
    User user=new User();
    PreparedStatement pstmt=null;
    String sql="SELECT id,name,sex,birthday FROM user WHERE id =?";
    try{
    pstmt=conn.prepareStatement(sql);
    ResultSet rs=pstmt.executeQuery();
    while(rs.next()){
    user.setId(rs.getInt(1));
    user.setName(rs.getString(2));
    user.setSex(rs.getString(3));
    user.setBirthday(rs.getDate(4));
    }
    rs.close();
    }
    catch(Exception e){
    throw e;
    }
    finally{
    if(pstmt != null){
    pstmt.close();
    }

    dbc.close();
    }
    return user;

    }}
      

  5.   

    com.mysql.jdbc.Driver吧,
    如果你确认jar包添加好了,打开那个jar包,看看里面有没有org.gjt.mm.mysql这个类
      

  6.   


    你好,是有org.gjt.mm.mysql这个包,里面有Driver.class
      

  7.   

    你好,本人新手,换一个jar包就是指在网上下一个,然后再add这个jar包?
      

  8.   


    你好,是有org.gjt.mm.mysql这个包,里面有Driver.class
    那应该是org.gjt.mm.mysql.driver这么写吧
      

  9.   


    你好,是有org.gjt.mm.mysql这个包,里面有Driver.class
    那应该是org.gjt.mm.mysql.driver这么写吧
    你好,谢谢你,的确是你说的这样,我之前不用加Driver也行的,不过加了的确成功了,分就给你了,也谢谢其他有评论的人。