JDBC: Insert Records Example


 

JDBC: Insert Records Example

In this section, you will learn how to insert records to the table using JDBC API.

In this section, you will learn how to insert records to the table using JDBC API.

JDBC: Insert Records Example

In this section, you will learn how to insert records to the table using JDBC API.

Insert Records : After creating table you can insert records. Inserting records means adding values to your table which can be further used for different operations. There is various way of insertion but here we are taking the easiest way of insertion.

Before performing any operation, it is required to create connection to the database and for that take a look of following terms -

Connection: This  interface specifies connection with specific databases like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of this interface.

Class.forName(String driver): It loads the driver.

DriverManager  :  The DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property

getConnection(String url+dbName, String userName, String password): This method establishes a connection to specified database url.

The MySQL connection URL has the following format:
jdbc:mysql://[host][:port]/[database][?property1][=value1]...

  • host :The host name where MySQL server is running. Default is 127.0.0.1 - the IP address of localhost.
  • port : The port number where MySQL is listening for connection. Default is 3306.
  • dbName : The name of an existing database on MySQL server. If not specified, the connection starts no current database.
  • Property : The name of a supported connection properties. "userName" and "password" are 2 most important properties.

Statement: This interface executes the SQL statement and returns the result it produces.

createStatement(): It is a method of Connection interface which returns Statement object.

executeUpdate(String sql): This method of Statement interface execute sql statements(insert or delete or update) which takes the sql query of string type and returns int.

Example : In this example we are inserting student record to the student table. First create connection to the database. Next write query of insertion and call method executeUpdate().

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

class InsertRecords{
public static void main(String[] args){
System.out.println("Insert Records Example...");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "students";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driverName);
con = DriverManager.getConnection(url + dbName, userName, password);
try {
Statement st = con.createStatement();
String sql = "INSERT INTO student VALUES(1,'Roxi','MCA')";
st.executeUpdate(sql);
System.out.println("Record inserted successfully!");
} catch (SQLException s) {
System.out.println("Roll number already exist!");
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output  :

Insert Records Example...
Record inserted successfully!

Ads