简体   繁体   English

Eclipse中的MySQL(MAMP)jdbc连接错误

[英]MySql (MAMP) jdbc connection error in eclipse

I am trying to set up the connection with MySQL in Eclipse. 我正在尝试在Eclipse中建立与MySQL的连接。 I have tried the following - 我尝试了以下方法-

  1. I have added the "my-sql-connector.jar" to the build path and class path. 我已经在构建路径和类路径中添加了“ my-sql-connector.jar”。
  2. I have tried adding it in the WEB-INF. 我尝试将其添加到WEB-INF中。
  3. Tried using class.forName. 尝试使用class.forName。

After trying all the above, still I am not able to set up the connection. 在尝试了上述所有方法之后,仍然无法建立连接。 I keep getting the following error - 我不断收到以下错误-

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/sampledb java.sql.SQLException:找不到适用于jdbc的驱动程序:mysql:// localhost / sampledb

Here is my code - 这是我的代码-

public class Main {

    private static final String USERNAME = "user1";
    private static final String PASSWORD = "user1";
    private static final String CONN_STRING =
            "jdbc:mysql://localhost/sampledb";

    public static void main(String[] args) throws SQLException {

        Connection conn = null;
        try {
            conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
            System.out.println("Connected");
        } catch (SQLException e) {

            System.err.println(e);
        } finally { 
            if (conn != null) {
                conn.close();
            }
        }

I have seen multiple posts on this issue and I have tried all of the suggestions. 我已经看到有关此问题的多个帖子,并且已经尝试了所有建议。 I am new to Java and trying to get a hands on Java and JDBC, but I am stuck with it for last 2 days. 我是Java的新手,试图尝试Java和JDBC,但是最近两天我一直坚持使用它。 Can someone please guide me with this issue? 有人可以指导我解决这个问题吗?

Did you try registering your Driver? 您是否尝试过注册驱动程序? And check if your driver is proper. 并检查您的驱动程序是否正确。

Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM