Database with java eclipse

Managing databases with Eclipse and the Database Tools — Tutorial

Eclipse Data tools Platform. The Eclipse Data Tools Platform (DTP) provides tools to simplify the handling of databases. This article demonstrates the usage of the Eclipse DTP. This article is based on Eclipse Luna (Eclipse 4.4).

1. Introduction

1.1. Eclipse Data Tools Platform

The Eclipse DTP project provide tools for performing database tasks. For example the project provides an editor for SQL statements or a database browser.

1.2. Derby — Java DB

In this article Apache Derby is used as the example database. Derby is an open-source, freely available, pure Java database. See Apache Derby Tutorial to learn more about Apache Derby.

Eclipse DTP contains connectors for lots of other databases, e.g. MySQL, PostgreSQL, HSQLDB.

1.3. Using Eclipse

The following assumes that you already have knowledge in using the Eclipse IDE. See the Eclipse Java IDE Tutorial in case you lacking this knowledge.

2. Installation

2.1. Installation of DTP

Install the Data Tools Platform via the Eclipse update manager. Install «Data Tools Platform Enablement Extender SDK»

Читайте также:  METANIT.COM

installdtp10

2.2. Download Apache Derby

Download the latest Derby version from the Apache website http://db.apache.org/derby/. Choose the binary distribution.

3. Project

Create a new General Project de.vogella.dtp.example via File New Other General Project . This project will store the SQL files.

Add a folder lib to your project. Copy the file derby.jar from your Derby download into this folder.

The result should look like the following.

4. Configuration

The following will create a connection for an embedded Derby.

Define the driver for the derby access. Go to Window Preferences Data Management Connectivity Driver Definition . Press Add. Select Derby and the version you want to use. If your Derby version is not listed selected the highest number displayed.

Select then the tab jar press Add…​ and select the derby.jar from your project folder lib.

Источник

How to Connect MySQL Database in Java Using Eclipse Step by Step

Hello guys, Welcome to another tutorial, and in this tutorial, we will learn how to connect MySQL database in Java using Eclipse with simple and easy steps. Most of the time, whenever we try to connect our program to the database, we face a lot of problems, and even then, we cannot connect successfully. But if you follow this tutorial properly, you will be able to connect your Java program to the MySQL database using Eclipse quite easily.

So Let’s start our tutorial on how to connect MySQL database in Java using Eclipse.

How to Connect MySQL Database in Java Using Eclipse

  • 1 How to Connect MySQL Database in Java Using Eclipse
    • 1.1 Downloading and Installing Eclipse
    • 1.2 Creating a Database and Table in MySQL
    • 1.3 Creating a Java Project in Eclipse
    • 1.4 Adding MySQL connector jar file in Eclipse
    • 1.5 Connecting Java Program with MySQL Database
    • 1.6 Inserting Data into The MySQL Database using Java
    • 1.7 Updating Data in The MySQL Database using Java
    • 1.8 How to Retrieve Data from MySQL Database in Java Using Eclipse
    • 1.9 Deleting Data from the MySQL Database using Java
    • 1.10 Conclusion

    Downloading and Installing Eclipse

    • The very First thing you need to do is to Download and Install Eclipse IDE on your system.
    • Open your favorite browser and go to the official website of Eclipse www.eclipse.org.
    • When you enter the official website of the eclipse, you will see a download button in the top right corner. Click on that.

    How to connect mysql database in java using eclipse

    • After clicking on the Download Button, you will be redirected to another Web page. Click on the download button, as shown in the figure below.

    How to connect mysql database in java using eclipse

    • Now again, click on the Download button to download the executable eclipse file.

    How to connect mysql database with java using eclipse

    • After downloading the file, you need to install it in your system in the same way you install any application.

    Creating a Database and Table in MySQL

    • To create a database and table, you can either choose MySQL command-line client or MySQL workbench, which is nothing but a GUI version of the MySQL console where we can visually design, model, and generates databases.
    • If you don’t know how to install MySQL workbench in your system, then you can follow this guide, How to install MySQL Workbench.
    • To get to know how to download and Install MySQL in your system so that you can Use MySQL command-line client, you can follow this guide.

    • I will use MySQL command-line client in this tutorial, open MySQL command-line client, and enter your password so that you can connect the MySQL database.
    • This is the same password you provided while installing the MySQL database in your system.
    • Now create a database named “demodb” and a table named “student” inside it with columns
      • ROLLNO
      • USERNAME
      • DEPT

    create table student ( ROLLNO varchar ( 10 ) not null , USERNAME varchar ( 30 ) , DEPT VARCHAR ( 30 ) ) ;

    Creating a Java Project in Eclipse

    • Open Eclipse IDE and click on the Java Project under the new section of File Menu ( File>>New>>Java Project ).

    • Now give a name to your project (DbConnect in this example) and click on “Finish”.

    • Now right click on the project and create a new Java class (New>>Class).

    • Now give a name to your class(MyDbconnect in this Example), tick mark on the public static void main(String[] args), and then click on the Finish button as shown in the figure below.

    Adding MySQL connector jar file in Eclipse

    • In order to connect your java program with MySQL database, you need to include MySQL JDBC driver which is a JAR file, namely mysql-connector-java-8.0.28.jar. The version number in the Jar file can be different.
    • You can download the latest version of MySQL connector from this link (MySQL Connector Java download) .
    • After visiting the link, select platform independent from the drop-down menu as shown in the figure below.

    • Then download the Platform Independent (Architecture Independent), ZIP Archive as shown below.

    • Extract the zip archive and you will get the jar file.
    • Next, you have to include this jar file into your program so that you will be able to connect your java program with MySQL database.
    • Right-click on the project, go to the properties section then select Java Build Path and click on the Add External JARs button.

    How to connect mysql database in java using eclipse

    • After clicking on the button a pop-up window will appear where you have to select and open the jar file.

    How to connect mysql database in java using eclipse

    • You can see the added Jar file as shown in the figure below. Now click on the Apply and Close button.

    How to connect mysql database in java using eclipse

    Connecting Java Program with MySQL Database

    Since you have already finished adding jar file, now you are ready to connect your Java program with MySQL Database. Let’s quickly take a look at the steps.

    • Establish a connection using DriverManager.getConnection(String URL) and it returns a Connection reference.
    • In String URL parameter you have to write like this jdbc:mysql://localhost:3306/demodb”, “root”, “root” where,

      Источник

Оцените статью