java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 错误 以及 远程连接MySql

版权声明: https://blog.csdn.net/baidu_41671472/article/details/82668299

https://pan.baidu.com/s/1JS8af_B0lowxbtmRADiY4A

最开始我的eclipse 中 是这个样子的。忽略掉 自己在外部建的lib文件夹 ,这个操作不需要。

这里写图片描述

运行之后提示的是找不到驱动。这个时候 你就需要这个东西。mysql-connector-java-5.1.23-bin.jar

这里写图片描述

网上百度很多这个,下载之后把jar 文件 复制到如图所示的位置。就是WEB-INF文件夹下的lib文件夹中。

这里写图片描述
这里写图片描述

之后右键jar文件

这里写图片描述

ADD jars

这里写图片描述

这里写图片描述

这里写图片描述

当然最后还是建议把这个文件给复制粘贴到 如图所示的文件夹中。

这里写图片描述

然后就是 连接数据库的问题了

<%
    String dirve = "com.mysql.jdbc.Driver";
    String name = "root";
    String password = "Szmt12345...";
    String url1 = "jdbc:mysql:192.79.249.199:3306/test";
    Connection connection = null;
    PreparedStatement statement = null;
    String sql = "insert into example (id,name,age) value('2016166110','cong',30);";
    try {
      Class.forName(dirve).newInstance();
      connection = (Connection) DriverManager.getConnection(url);
      System.out.print("ok1");
      statement = (PreparedStatement)connection.prepareStatement(sql);
      int result = statement.executeUpdate();
      if(result == 1)
        System.out.println("success");
      else
        System.out.println("faild");
    } catch (Exception e) {
      e.printStackTrace();
 }
  %>  

大家网上看到的很多都是这个样子的。我不知道有没有错,因为我的电脑里面的MySql删掉了,我觉得如果连自己电脑肯定是没问题的。但是如果是连接远端的其他人的电脑里面的MySql,可写出问题。(当然,我觉得不会出事,毕竟网上都这么写,我也只是初入java的萌新)

<%
    String dirve = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://192.79.249.199:3306/test?user=root&password=Szmt12345...&useUnicod e=true&characterEncoding=8859_1";        Connection connection = null;
    PreparedStatement statement = null;
    String sql = "insert into example (id,name,age) value('2016166110','cong',30);";
    try {
      Class.forName(dirve).newInstance();
      connection = (Connection) DriverManager.getConnection(url);
      System.out.print("ok1");
      statement = (PreparedStatement)connection.prepareStatement(sql);
      int result = statement.executeUpdate();
      if(result == 1)
        System.out.println("success");
      else
        System.out.println("faild");
    } catch (Exception e) {`
      e.printStackTrace();
 }
  %>  

如果 sql语句出现问题错误,编译器会报错的,所以要仔细查看控制台输出的语句。千万记得最后加分好。

猜你喜欢

转载自blog.csdn.net/baidu_41671472/article/details/82668299