Git Product home page Git Product logo

spring_jdbc_template's Introduction

Spring_JDBC_Template

This is a Spring JDBC Template using the com.mysql.cj.jdbc.Driver data source driver.

Project structure

|--src/main/
|   |--java
|   |   |--com/jdbc/template
|   |   |   **SpringJdbcTemplateApplication.java
|   |   |   |--config
|   |   |   |   **JdbcConfig.java
|   |   |   |--dao
|   |   |   |   |--impl
|   |   |   |   |   **EmployeeDAOImpl.java
|   |   |   |   |--models
|   |   |   |   |   **EmployeeDAO.java
|   |   |   |--errorHandlers
|   |   |   |   **CustomSQLErrorCodeTranslator.java
|   |   |   |--mapper
|   |   |   |   **EmployeeRowMapper.java
|   |   |   |--models
|   |   |   |   **Employee.java
|   |   |   |--services
|   |   |   |   **EmpoyeeTest.java
|   |--resources
|   |   **application.properties

Run Template as is

  1. Create a table inside your database called "Employee"
CREATE TABLE `Employee` (
  `id` int(11) unsigned NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `role` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
)	
  1. Insert your mysql database information (username, password, and url) into the application.properties file
spring.datasource.username=[username]
spring.datasource.password=[password]
spring.datasource.url=jdbc:mysql://[url]:[port]/[database name]
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  1. Run it!

Notes

a. The EmployeeTest class [com.jdbc.template.services] simply just saves a single "employee" to the table and outputs if it was successful or not.

@Service
public class EmployeeTest {

	@Autowired
	EmployeeDAO employeeDAO;

	@PostConstruct
	public void test() {

		Employee emp1 = new Employee(0, "[NAME HERE]", "[ROLE HERE]");

		int output = employeeDAO.save(emp1);

		if (output != 0) {
			System.out.println("Employee " + emp1.getName() + " was saved successfully with id: " + emp1.getId());
		} else {
			System.out.println("Employee " + emp1.getName() + "with id: " + emp1.getId() + " could not be saved.");
		}
	}
}

The @PostConstruct annotation just executes the "test" method after all dependencies have been injectioned. This isn't required, this is just a quick way to call the method for demo purposes.

spring_jdbc_template's People

Contributors

jaycen9887 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.