在eclipse里直接运行向数据库添加数据的java文件没有问题!
可是,用servlet的方式,就报错找不到 com.mysql.jdbc.Driver 
非常的苦恼,请高手帮忙,感激不尽

解决方案 »

  1.   

    2种方式都是在同一个project里面的吗?如果不是的话,那就是第2个里面没有放Jar包
      

  2.   

    不是在同一个工程里
    包都导入了呀
    真是奇怪啊!!
    用servlet就显示java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
      

  3.   

    这个是servlet
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "214845";
    String sql = "insert into table1 (username,password) values(?,?) ";
    Connection connection;
    try {
    Class.forName(driver);
    connection = DriverManager.getConnection(url, username,password);
    PreparedStatement pstmt = connection.prepareStatement(sql);

    pstmt.setString(1,"request.getParameter('username')");
    pstmt.setString(1,"request.getParameter('password')");
    pstmt.executeUpdate();
    pstmt.close();
    connection.close();
    }
    catch (ClassNotFoundException e)//捕获类未找到异常 
    {
    e.printStackTrace();
    }
      catch (SQLException e) {
    e.printStackTrace();
    }
    }
    这个是带main方法的
    public static void main(String[] args) {

    String driver="com.mysql.jdbc.Driver";
    String url="jdbc:mysql://localhost:3306/test";
    String username="root";
    String password="214845";
    String sql="insert into table1 (username,password) values(?,?) ";
    try {
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url,username,password);
    PreparedStatement pstnt = conn.prepareStatement(sql);
    pstnt.setString(1,"1fds");
    pstnt.setString(2,"123456");
    pstnt.executeUpdate();
    pstnt.close();
    conn.close();
    } catch (ClassNotFoundException e)
    {
    e.printStackTrace();
    }
      catch (SQLException e) {
    e.printStackTrace();
    }
    }
    真是奇怪啊!直接运行main方法就可以添加,用servlert就不行了。
      

  4.   

    在servlet中写类引用
    import com.mysql.jdbc.Driver;
    看有没有问题
      

  5.   

    肯定是没导入jar包的问题,lz再仔细检查一下。
      

  6.   

    你直接把mysql的jar包复制到给工程的lib文件夹下
      

  7.   

    你直接把mysql的jar包复制到给工程的lib文件夹下
    是“该工程”
      

  8.   

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    貌似没有导入驱动包
      

  9.   

    mysql的jar包放到lib目录下面,应该就不会有问题啦
      

  10.   

    非常感谢各位!!!
    问题解决啦!
    就是没把mysql的jar包放到lib目录下!!!谢谢啦!!!