user3166793
user3166793

Reputation: 27

Connecting MySQL workbench to Eclipse

I am very new to programming and have never used MySQL before so bear with me! I am trying to connect MySQL workbench to my eclipse project but I have no idea where to start. Can someone please explain the processes I need to go through and how I would go about implementing them?

Thanks!

Upvotes: 0

Views: 11711

Answers (1)

learner
learner

Reputation: 3110

Create a java project in Eclipse

create package Name com.example.sql

Include mysqlconnector.jar in your project

package com.example.sql;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCExample {
    public static Connection con;
  public static void main(String[] argv) {
      try
        {
            connectionQuery();

            PreparedStatement statement =  con.prepareStatement("SELECT * from table_name");/*write query inside of prepared statement*/
            ResultSet result = statement.executeQuery();
            System.out.println("DataBase table accessed");

            while(result.next())
            {
               String retrievedid= result.getString("Column_name");

               System.out.println(retrievedid);
            }


            con.close();
        }

        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println(e.getMessage().toString());
        }
  }

  public static void connectionQuery()
  {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/boopathi","root","root");
            System.out.println("Remote DB connection established");
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
            System.out.println("Remote server could not be connected");
        }
        catch (NullPointerException e)
        {
            e.printStackTrace();
            System.out.println("Remote server could not be connected");
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            System.out.println("Remote db connection establishment error");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("False query");
        }
    }
}

Refer this link also

Upvotes: 1

Related Questions