SPRING MySQL Database Connection using JDBC STEPS

Overview

SPRING-MySQL-Database-Connection-using-JDBC-STEPS

SPRING MySQL Database Connection using JDBC STEPS

Step1: Create maven project Group id: com.cdac Artifact id: spring-database-connection Version : 1.0

Step 2: Create file Spring-intro/src/main/resources app-config.xml file And add following dependencies


">

  

===========================================================

Step 3: add following dependencies pom.xml file


   
	
    
		
     
      org.springframework
     
		
     
      spring-context
     
		
     
      5.3.15
     
	
    
	
    
		
     
      org.springframework
     
		
     
      spring-jdbc
     
		
     
      5.3.15
     
	
    
	
    
		
     
      mysql
     
		
     
      mysql-connector-java
     
		
     
      8.0.26
     
	
    
	
    
		
     
      junit
     
		
     
      junit
     
		
     
      4.13.2
     
	
    

   

=====================================================

Step 4: create Interface file CarPartsInventory.java file in com.cdac.component package

package com.cdac.component; import java.util.List; public interface CarPartsInventory { public void addNewPart(CarPart carPart); public List getAvailableParts(); }

=====================================================

Step5: create CarPart.java file in com.cdac.app package

package com.cdac.component; public class CarPart {

private int partNo;
private String partName;
private String carModel;
private double price;
private int quantity;

public int getPartNo() {
	return partNo;
}
public void setPartNo(int partNo) {
	this.partNo = partNo;
}
public String getPartName() {
	return partName;
}
public void setPartName(String partName) {
	this.partName = partName;
}
public String getCarModel() {
	return carModel;
}
public void setCarModel(String carModel) {
	this.carModel = carModel;
}
public double getPrice() {
	return price;
}
public void setPrice(double price) {
	this.price = price;
}
public int getQuantity() {
	return quantity;
}
public void setQuantity(int quantity) {
	this.quantity = quantity;
}

}

=========================================================== Step 6: implement CarPartsInventory interface in CarPartsInventoryimp1 And also add JDBC database connection package com.cdac.component; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List;

import org.springframework.stereotype.Component;

@Component("carParts1") public class CarPartsInventoryImpl1 implements CarPartsInventory {

getAvailableParts() { return null; }">
public void addNewPart(CarPart carPart) {
	Connection conn = null;
	PreparedStatement stmt = null;
	try {
		Class.forName("com.mysql.cj.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/training", "root", "cdac");
		stmt = conn.prepareStatement("INSERT INTO tbl_carpart(part_name, car_model, price, quantity) VALUES(?, ?, ?, ?)");
		stmt.setString(1, carPart.getPartName());
		stmt.setString(2, carPart.getCarModel());
		stmt.setDouble(3, carPart.getPrice());
		stmt.setInt(4, carPart.getQuantity());
		stmt.executeUpdate(); //DML
	}
	catch (ClassNotFoundException | SQLException e) {
		e.printStackTrace(); //rather we should throw some user defined exception
	}
	finally {
		try { conn.close(); } catch(Exception e) { }
	}
}

public List
   
     getAvailableParts() {
	return null;
}

   

}

============================================================= Step 7: add class file App.java file in com.cdac.app package

package com.cdac.app; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cdac.component.CarPart; import com.cdac.component.CarPartsInventory;

public class App {

public static void main(String[] args) {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("app-config.xml");
	CarPartsInventory cp = (CarPartsInventory) ctx.getBean("carParts1");
	
	CarPart c = new CarPart();
	c.setPartName("Nut & Bolt");
	c.setCarModel("Maruti 800");
	c.setPrice(500);
	c.setQuantity(99);
	cp.addNewPart(c);
	
}

}

============================================================= Step 8: write following Query in SQL TODO: Create Table in the DB before running this example

create table tbl_carpart(part_no int primary key auto_increment, part_name varchar(30), car_model varchar(35), price double, quantity int);


See OUTPUT in MySQL



| part_no | part_name | car_model | price | quantity |

| 1 | Nut & Bolt | Maruti 800 | 500 | 99 |

You might also like...

esProc SPL is a scripting language for data processing, with well-designed rich library functions and powerful syntax, which can be executed in a Java program through JDBC interface and computing independently.

esProc SPL is a scripting language for data processing, with well-designed rich library functions and powerful syntax, which can be executed in a Java program through JDBC interface and computing independently.

esProc esProc is the unique name for esProc SPL package. esProc SPL is an open-source programming language for data processing, which can perform comp

Dec 27, 2022

A JDBC driver for Cloudflare's D1 product, compatible with Jetbrains tools.

A JDBC driver for Cloudflare's D1 product, compatible with Jetbrains tools.

D1 JDBC Driver A JDBC driver for Cloudflare's D1 Database product! JDBC is the technology that drives popular database tools such as Jetbrains' databa

Dec 9, 2022

A tool based on mysql-connector to simplify the use of databases, tables & columns

A tool based on mysql-connector to simplify the use of databases, tables & columns

Description A tool based on mysql-connector to simplify the use of databases, tables & columns. This tool automatically creates the databases & tables

Nov 17, 2022

Database with Java Swing UI that stores consumables (food & drink) using RESTful API to send commands via HTTP

Database with Java Swing UI that stores consumables (food & drink) using RESTful API to send commands via HTTP

Database with Java Swing UI that stores consumables (food & drink) using RESTful API to send commands via HTTP.

Mar 8, 2022

Apache Druid: a high performance real-time analytics database.

Apache Druid: a high performance real-time analytics database.

Website | Documentation | Developer Mailing List | User Mailing List | Slack | Twitter | Download Apache Druid Druid is a high performance real-time a

Jan 1, 2023

eXist Native XML Database and Application Platform

eXist Native XML Database and Application Platform

eXist-db Native XML Database eXist-db is a high-performance open source native XML database—a NoSQL document database and application platform built e

Dec 30, 2022

Flyway by Redgate • Database Migrations Made Easy.

Flyway by Redgate • Database Migrations Made Easy.

Flyway by Redgate Database Migrations Made Easy. Evolve your database schema easily and reliably across all your instances. Simple, focused and powerf

Jan 9, 2023

MapDB provides concurrent Maps, Sets and Queues backed by disk storage or off-heap-memory. It is a fast and easy to use embedded Java database engine.

MapDB: database engine MapDB combines embedded database engine and Java collections. It is free under Apache 2 license. MapDB is flexible and can be u

Dec 30, 2022
Owner
Dnyaneshwar Madhewad
Dnyaneshwar Madhewad
Vibur DBCP - concurrent and dynamic JDBC connection pool

Vibur DBCP is concurrent, fast, and fully-featured JDBC connection pool, which provides advanced performance monitoring capabilities, including slow S

Vibur 94 Apr 20, 2022
FlexyPool adds metrics and failover strategies to a given Connection Pool, allowing it to resize on demand.

Introduction The FlexyPool library adds metrics and flexible strategies to a given Connection Pool, allowing it to resize on demand. This is very hand

Vlad Mihalcea 970 Jan 1, 2023
Java & Kotlin Async DataBase Driver for MySQL and PostgreSQL written in Kotlin

jasync-sql is a Simple, Netty based, asynchronous, performant and reliable database drivers for PostgreSQL and MySQL written in Kotlin. Show your ❤ wi

null 1.5k Dec 31, 2022
Hi, Spring fans! In this installment, we'll look at how to build tenancy-aware JDBC applications

Multitenant JDBC You'll need to spin up two separate PostgreSQL instances. Put this script into a file called postgres.sh: #!/usr/bin/env bash NAME=${

Spring Tips 19 Nov 7, 2022
JDBC driver for ClickHouse

This is a basic and restricted implementation of jdbc driver for ClickHouse. It has support of a minimal subset of features to be usable.

ClickHouse 1.1k Jan 1, 2023
Multi-DBMS SQL Benchmarking Framework via JDBC

BenchBase BenchBase (formerly OLTPBench) is a Multi-DBMS SQL Benchmarking Framework via JDBC. Table of Contents Quickstart Description Usage Guide Con

CMU Database Group 213 Dec 29, 2022
Provides many useful CRUD, Pagination, Sorting operations with Thread-safe Singleton support through the native JDBC API.

BangMapleJDBCRepository Inspired by the JpaRepository of Spring framework which also provides many capabilities for the CRUD, Pagination and Sorting o

Ngô Nguyên Bằng 5 Apr 7, 2022
Core ORMLite functionality that provides a lite Java ORM in conjunction with ormlite-jdbc or ormlite-android

ORMLite Core This package provides the core functionality for the JDBC and Android packages. Users that are connecting to SQL databases via JDBC shoul

Gray 547 Dec 25, 2022
Amazon AppFlow Custom JDBC Connector example

Amazon AppFlow Custom JDBC Connector example This project contains source code and supporting files that implements Amazon Custom Connector SDK and re

AWS Samples 6 Oct 26, 2022
Online Quiz system - JDBC, JSP

Online-Quiz-System-in-Java Online Quiz system - JDBC, JSP Java Project based on JDBC, JSP, Java Servlet and Server Deployment Project Aim Develop web

Muhammad Asad 6 Oct 14, 2022