JDBC Video Tutorial- How to insert data into MySQL database table?

This video tutorial is for beginners in JDBC which teaches the new comers to write Java program to insert data into database.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

JDBC Video tutorial example for inserting data into MySQL database

JDBC programming is the main thing a developer should learn after learning the Core Java and in this video I have explained the steps of write code to insert the data into MySQL database.

We have used the MySQL database as it is easy to download and install, but you can easily modify this program to run against database. You have to change the JDBC driver for that particular database and the program remains the same.

How the program works?

  1. Program uses the MySQL database to connect to the MySQL database
  2. The Statement class of the JDBC api is used to run a sql statement against the database
  3. We have used the query “INSERT employee (id,name,salary) VALUES(1,'Deepak','10000')” to insert the database into database.
  4. The executeUpdate() method of the statement object runs the query against database and database is inserted into database.
  5. The try and catch exception block is used to catch exceptions if any while executing the program.

Here is the code of creating the database table:

CREATE TABLE `employee` (
            `id` int(11) NOT NULL DEFAULT '0',   
            `name` varchar(30) DEFAULT NULL,     
            `salary` int(11) DEFAULT NULL,       
            PRIMARY KEY (`id`)                   
          ) ENGINE=InnoDB DEFAULT CHARSET=latin1 

Video Tutorial: How to insert data into MySQL Table?

Here is the code of the full Java program:

package net.roseindia.jdbcexamples;

import java.sql.*;

public class InsertValues {
	public static void main(String[] args) {
		System.out.println("Inserting values in Mysql database table!");
		Connection con = null;
		String url = "jdbc:mysql://localhost:3306/";
		String db = "jdbcexamples";
		String driver = "com.mysql.jdbc.Driver";
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url + db, "root", "root");
			try {
				Statement st = con.createStatement();
				int val = st
		.executeUpdate("INSERT employee (id,name,salary) VALUES(1,'Deepak','10000')");
				st.close();
				System.out.println("Data added");
			} catch (SQLException s) {
				System.out.println("SQL statement is not executed!");
			}

			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

To run the program right click in the java code window and then select Run As-> Java Application

After successful execution of the program record will be inserted into the MySQL table.

Briefly following are the steps to insert into database:

  1. Connect to MySQL Database using JDBC
  2. Create the Statement Object
  3. Construct the sql
  4. Run the sql using the Statement object against database
  5. Process results if any

Here is the sql script to create table:

CREATE TABLE `employee` (               
            `id` int(11) NOT NULL DEFAULT '0',    
            `name` varchar(30) DEFAULT NULL,      
            `salary` int(11) DEFAULT NULL,        
            PRIMARY KEY (`id`)                    
) ENGINE=InnoDB DEFAULT CHARSET=latin1  ;

So, in this tutorial on RoseIndia.net you have learned the example program for inserting the data into MySQL database using the JDBC driver.

Download the source code in the Eclipse Project format.

You can find more details at http://www.roseindia.net/jdbc/jdbc-mysql/InsertValues.shtml

Check all JDBC Tutorials at http://www.roseindia.net/jdbc/index.shtml

Advertisements

Share on Google+Share on Google+

JDBC Video Tutorial- How to insert data into MySQL database table?

Posted on: June 5, 2015 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: JDBC Video Tutorial- How to insert data into MySQL database table?  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads