Skip to content

A simple tutorial, to a simple Java CRUD (Create, Read, Update and Delete)

Notifications You must be signed in to change notification settings

BrenoCPimenta/Java-CRUD

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java-CRUD

A simple tutorial, to a simple Java CRUD (Create, Read, Update and Delete).

We will use:

  • Java;
  • Maven to manage our dependencies;
  • MariaDB as DataBase;
  • JDBC as interface to connect to the DataBase;
  • JUnit for the tests.

What you need:

  • MySQL/MariaDB installed and configured. If you want to use MySQL from Oracle, there's no problem, I will let comments on the code and here, saying where you need to change.

  • It will be used Eclipse, it is optional, but the tips on the steps will be directed to it.

  • Java up and running. I will use maven, just to keep with the best practicies, but isn't necessary.

Step by Step:

  • Create a table called garden in a database called plants. I will let a CreatingTable.txt file with the comands.

  • Create a Maven Project on Eclipse.

  • Now go into maven repository site and search for MariaDB, it will be the "MariaDB java client" and add the dependency to the pom file. if you are using MySQL alone, you must search instead for "MySQL Connector/J".

  • Following the TDD (Test Driven Development) good practicies, first thing we will be adding into our package, will be a JUnit Test Case, I will call it _StepByStep. Why:

    • So we can make methods and test our code as we create it, preventing a lot of Debugging later.
    • Will be in the same package as our main code, because of the protected getConnection method.
    • It has a underscore in the name, so it gets above our other classes in our source package, a tip from James Shore.
  • Now lets create our first class BaseDAO, that will be responsible to connect to our DataBase using JDBC.

    • It consists on a Builder that activates the drivers and a method that uses the URL, the User and the password, to create a connection.
  • TEST In StepByStep we will add our first method "testConnection" so we can test our connection first, everytime we run the tests.

  • We will add a extra Class called SimpleDate, it will be responsile to get our date in Int and transform it into java.sql.Date, because the constructor with int numbers is Deprecated.

  • Our second class will be Plant, that will be responsible for the Getters and Setters

    • Will be Serializable.
    • Our variables and their types, must be coherent with the ones we created in the DataBase.
    • Notice in set date we will use the method from SimpleDate Class.
    • We will finish overriding the method toString, so that it returns all of our information in a string that we formated.
  • TEST Lets add 2 tests on StepByStep, one for the Object and one for the DataBase.

    • Test our object Plant.
      • Add a Private Plant object in the StepByStep.
      • Than a method called "testPlantObject()" to test getter and setters.
    • Test if we are connecting to the right table.
      • Insert a Plant to our DataBase manually (Query in the createTable file).
      • Now we verify with a connection if it returns correctly to what we ask by the "testDataBase()" method.
      • I let 2 options on the code, so you can decide what type of query you want to use.
  • Our third class will be PlantDAO, that will inherit our method getConnection(), so we can create new methods that open connections and manipulate our data.

    • A Method 'createPlant' that gets a result of a executed query on the database and transforms into a object.
    • A Method 'getPlantById'
    • A Method 'findByName'
    • A Method 'getPlants'
    • A Method 'getGeneratedId' that returns the ID of the recent added plant.
    • A Method 'savePlant' to add or update.
    • A Method 'deletePlant'.
    • A Method 'getNextID' that returns the next auto incremented value. This will be very useful in our tests.
  • Now will be the greater TEST, new method on StepByStep called: testCreateUpdateDeletePlantDAO()

    • We will create a plant object;
    • Save it in the table (DataBase);
    • Search for it on the DataBase, to see if it worked;
    • Verify the AutoIncremented id;
    • Then update this plant we added with another name and verify;
    • For last, we will delete the plant, and verify if it worked again;
  • Our last class will be PlantService, since its not a good practice to access directily the DAO layer, we will use this class.

  • Now we will TEST a Final method on StepByStep: testService()

    • It will be exactly as the last test method, but now we will be using the methods from the service class.

Now we have a Create, Read, Update and Delete applicantion in Java!

About

A simple tutorial, to a simple Java CRUD (Create, Read, Update and Delete)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages