Java中的MySQL连接错误-com.mysql.jdbc.Driver [英] MySQL Connection Error in Java - com.mysql.jdbc.Driver

查看:349
本文介绍了Java中的MySQL连接错误-com.mysql.jdbc.Driver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将Java应用程序连接到MySQL数据库,并使用以下代码行:

I have been trying to connect my java application to a MySQL database and have used the following lines of code:

import java.sql.*; 
public class AcceptValues extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

             String url = "jdbc:mysql://localhost:3306/db_name";
             String driver = "com.mysql.jdbc.Driver";
             String userName = "root";
             String password = "";
             try {
             Class.forName(driver);
             Connection conn = DriverManager.getConnection(url,userName,password);
             out.print("Connection estd");
             Statement st = conn.createStatement();
             st.executeQuery("insert into table_name values(2, 'testing');");
             }
             conn.close();
             } catch (Exception e) {
                 out.print("Error : " +e.getMessage());
             }
        }
}

我还将类名设置为我从mysql网站下载的mysql-connector-java-5.1.29-bin.jar.但是我仍然无法使用上述代码行连接到数据库,并且将异常 com.mysql.jdbc.Driver 抛出错误.

I have also set the classname to mysql-connector-java-5.1.29-bin.jar which I downloaded from the mysql website. But I still am not able to connect to the database using the above lines of code and is throwing the exeption com.mysql.jdbc.Driver as an error.

我是Java开发的新手,任何帮助将不胜感激.谢谢.

I'm new to java development and any help would be deeply appreciated. Thank you.

推荐答案

WEB-INF/lib文件夹中添加mysql-connector-java-5.1.29-bin.jar. 仅将jar添加到您的构建路径中是不够的. 甚至我一开始也遇到同样的问题.

Add the mysql-connector-java-5.1.29-bin.jar in the WEB-INF/lib folder. Its not enough to just add the jar in your built path. Even I had the same issue at the start.

这篇关于Java中的MySQL连接错误-com.mysql.jdbc.Driver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆