JDBC Insert Null

In this Tutorial we want to describe you a code that help in understanding JDBC InsertNull.


 

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 Insert Null

     

The java.sql package contain a Prepared Statement  derived from the Statement interface. The Prepared Statement provides you to pass the parameter at runtime to the SQL statement, which is used for modify the data in a table.

In this Tutorial we want to describe you a code that help in understanding JDBC Insert Null. The program include a class JDBC Insert Null, Inside the class we have a main method that follows the list of steps -

  Loading a driver by calling a class.forName( ),that accept driver class as argument. The DriverManager.getConnection ( )return you a connection object, provides a connection between url and database.

  prepareStatement ( ):This return you a Prepared Statement object, perform an insertion of rows into employees table by passing data of Employee at runtime. The setString ( ) method set the value into the column of Employee table.  The INSERT statement is executed using executeUpdate ( ),return you the numbers of rows modified and affected.

 executeQuery ( ) - This method returns you a record set retrieved from a table in the database. The select  statement is used to retain the records from the result set.

 next ( ) - This method is used to retain  next elements in a series.

Finally the print print the No, Name from a employee table present in database. In case there exist an exception in try block, the subsequent catch block catch and handle the exception.

JdbcInsertNull.java


import java.sql.*;

public class JdbcInsertNull {

  public static void main(String args[]) {

  Connection con = null;
  PreparedStatement pst = null;
  ResultSet rs = null;

  String url = "jdbc:mysql://localhost:3306/";
  String db = "komal";
  String driver = "com.mysql.jdbc.Driver";
  String user = "root";
  String pass = "root";

  try {
  Class.forName(driver);
  con = DriverManager.getConnection(url + db, user, pass);
  con.setAutoCommit(false);// Disables auto-commit.

  String sql = "insert into employees values(?,?,?,?) ";
  pst = con.prepareStatement(sql);

  pst.setString(1"Rakesh");
  pst.setString(2"Kanwal");
  pst.setString(3"Delhi");
  pst.setString(4null);

  pst.executeUpdate();

  sql = "select * from employees";
  rs = pst.executeQuery(sql);

  System.out.println("No  \tName");
  while (rs.next()) {
  System.out.print(rs.getString(1) + " \t");
  System.out.print(rs.getString(2) + " \t");
  System.out.print(rs.getString(3) + " \t");
  System.out.println(rs.getString(4));
  }
  rs.close();
  pst.close();
  con.close();

  catch (Exception e) {
  System.out.println(e);
  }
  }
}

Output

No  	Name
Girish     	Tewari     	Rohini     	Indian
Komal     	Singh     	Rohini     	Indian
Sandeep     	kumar     	Rohini     	Indian
Amit     	Singh     	Rohini     	Indian
Girish     	Tewari     	Rohini     	Indian
Darshan     	Tewari     	Rohini     	Indian
Rakesh     	Kanwal     	Delhi     	null

Download code

Advertisements

Ads
Share on Google+Share on Google+

JDBC Insert Null

Posted on: November 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: JDBC Insert Null  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Arjun kp
June 7, 2012
java

i tried similarily..but showed null value cannot be inserted into database................

Ads

 

Ads