Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

How can I connect MYSQL DB with eclipse?

1 Answer

0 votes
by (13.1k points)

Setting up JDBC Connection

First, you need to download the MYSQL connector for Java.

And setting up a JDBC connection in Eclipse is much easier. You can try this,

Right-click on your Java project in the package explorer in Eclipse workspace and go to Properties Go down the list that appears until you find Java Build Path and click Libraries and then click Add External Archives. Find the downloaded jar called mysql-connector-java-versionnumber.jar and choose it. This should import your jar file to Eclipse

And finally, you can code. Here is a sample Java JDBC connection,

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.ResultSet;

import java.sql.Statement;

class MySQLConnection {

  public static void main(String[] args) {

    Connection con = null;

    String url = "jdbc:mysql://localhost:3306/some_db";

    String username = "some_username":

    String password = "some_password";

    try {

      Class.forName("com.mysql.jdbc.Driver");

      con = DriverManager.getConnection(url, username, password);

      System.out.println("Connected!");

    } catch (SQLException ex) {

        throw new Error("Error ", ex);

    } finally {

      try {

        if (con != null) {

            con.close();

        }

      } catch (SQLException ex) {

          System.out.println(ex.getMessage());

      }

    }

  }

}

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
0 answers
asked Feb 17, 2021 in Java by Harsh (1.5k points)
0 votes
1 answer
asked Apr 6, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Aug 20, 2019 in Java by Krishna (2.6k points)
0 votes
1 answer
...