How to create a database in MySQL with connection object conn (Student Class Example)

Java code for inserting data into the database is done using JDBC (Java Database connectivity).

JDBC is an API to connect the java programs to various databases like MySql, Oracle, MSAccess, etc.

Here Step by step tutorial to insert data from java into the database.

Here MySql or MariaDB database is used for this purpose.

So to insert data in the database we need to set in two ends

  1. Create a Database and table
  2. Write Java (JDBC) code

Let’s see them one by one

Create a Database and table

Open phpMyAdmin click on the home icon or the left side

Click on SQL on the middle menu.

Write create query given below

Copy and insert above code in SQL query box

create table query

After clicking on the go button it will show the below box if the query is successfully executed.

created table query

You can see the structure of the table. which show below output.

table structure

Now the table is created.

Write JDBC Code

The steps for JDBC are as below

  1. import JDBC classes
  2. Load and register JDBC Driver
  3. Open Database connection
  4. Create a Statement object to pass the query
  5. Execute Statement
  6. Get the result
  7. Close connections

File Structure of Program

file explorer

Importing JDBC classes

Load and register JDBC Driver

Here Class.forName() is used to load and register driver

Open Database connection

Create a Statement object to pass the query

Execute the Statement and get the result

Here ps.executeUpdate() execute the query and get the of row affected.

Close connections

pre class=”theme:eclipse font:verdana toolbar:2 show-plain:3 lang:java decode:true”> con.close();

Insert a Student Object into the database

Before proceeding add MySQL-connector-java-5.1.14-bin.jar to your classpath.

All file details are as below.

Student.java

Student object that contains basic properties of students like id, rollNo, name, branch,section, emailId,dob, mobileNo, addDate.

ConnectionFactory .java

This file is responsible to connect with MySql database, after getting connection this will return the connection object

StudentJDBCInsert.java

This class will get a connection from ConnectionFactory and insert data in the database and return the rows inserted in the database.

Output

Insert multiple Student Objects into the database

an efficient way to insert multiple records in the database is using batch processing

Steps to store multiple records in the database

  1. Create multiple objects
  2. Add all to a List
  3. Add list item to batch
  4. executeBatch()

StudnetJDBCBatchInsert.java

this program will insert batch data to MySql database

Output

MySql Admin view

data in MySql table

Reading Data using JDBC

StudentJDBCRead .java

This program read student records from the database table and shows them in java console.

Output

Download the above code from GitHub.

Read More

MySql Jdbc Operations for beginner

JTable Pagination in Java JDBC and MySQL Database

Registration Form in Java | Student registration form using java swing source code