Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Set byte, short and long data types by using the Prepared Statement

This tutorial teaches you the process of setting the byte, short and long data types in the PreparedStatement interface of the java.sql package.

Ads

Tutorials   
Change Column Name in MySQL Java Mysql Connection Example JDBC Example with MySQL Jdbc Mysql Connection String Jdbc Mysql Connection Url Getting Column Names from a database table in Java Count Rows from a Database Table JDBC Example with MySQL Write Text File to Table Inserting Image in Database Table Insert Data in Table Using Stored Procedure Copy One Database Table to Another Copy Table in a MySQL Database Set Timestamp by using the Prepared Statement Set Time by using the Prepared Statement Set Date by using the Prepared Statement Prepared Statement Set Big Decimal Set byte, short and long data types by using the Prepared Statement Set Data Types by using Prepared Statement Using the Prepared Statement Twice Deleting Records using the Prepared Statement Count Records using the Prepared Statement Inserting Records using the Prepared Statement Update Records using Prepared Statement Select Records Using Prepared Statement Prepared Statement With Batch Update Statement Batch Update Prepared Statement Set Object Cross Join Tables in a Specific Database Join tables with the NATURAL RIGHT JOIN operation Join tables with the NATURAL LEFT JOIN operation Join tables in the specific database Delete a Column from a Database Table Delete a Specific Row from a Database Table Deleting All Rows from a Database Table Sum of Column in a Database Table Arrange a Column of Database Table Arrange a Column of Database Table Remove Unique Column in Database Table Make Unique Column in Database Table Change Column Name of a Table Adding a New Column Name in Database Table Retrieving All Rows from a Database Table Inserting values in MySQL database table Retrieving Tables from a Database Deleting a Table from Database Description of Database Table Creating a MySQL Database Table to store Java Types Creating a Database Table Creating a Database in MySQL
Ads

Set byte, short and long data types by using the Prepared Statement

     

This tutorial teaches you the process of setting the byte, short and long data types in the PreparedStatement interface of the java.sql package. We know that the java and MySQL data types could not be same. But mapping is a mechanism to provide the facility for transferring the data to each other. For better understanding see the mapping between java and MySQL in the following explanation. 

Java data type    MySQL data type
byte  tinyint (TINYINT)
short   smallint (SMALLINT)
long  bigint (BIGINT)

Description of program:

With the help of this program we are going to insert the byte, short and long data types in 'datatypes' table through the PreparedStatement interface of java.sql package. First of all, establish the connection with MySQL database by using the JDBC driver. After establishing the connection, it takes the SQL query in the prepareStatement and returns the PreparedStatement object. Now, we will insert different types of data like: byte, short and long by using the setByte, setShort and setLong methods. You will get the inserted data in the database table with a message "row(s) affected" otherwise it will display "SQL statement is not executed!".

Description of code:

setByte(int index, byte b):
This method takes two arguments: integer and byte types. The integer type data defines the index and other takes bytes. It sets the arguments to the java byte value. When it sends the into the database then the JDBC driver switch over this into the SQL TINYINT value.

setShort(int index, short s):
Above method sets the arguments to java short value. When it sends the data into database then the JDBC driver switch to SQL SMALLINT value. It also takes two types arguments like: integer and short. 

setLong(int index, long l):
The method used for setting the arguments to java long value. It takes integer and long types data. The JDBC driver sends the data into database to SQL BIGINT value. 

Here is the code of program:

import java.sql.*;

public class SetByteSortLong{
  public static void main(String[] args) {
  System.out.println("Set Byte,short and long example by using Prepared Statement!");
  Connection con = null;
  try{
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:mysql:
//localhost:3306/jdbctutorial","root","root");
  try{
  String sql = "INSERT datatypes VALUES(?,?,?)";
  PreparedStatement prest = con.prepareStatement(sql);
  prest.setByte(1,(byte)5);
  prest.setShort(2,(short)65);
  prest.setLong(3,(long)254);
  int row = prest.executeUpdate();
  System.out.println(row + " row(s) affected)");
  }
  catch (SQLException s){
  System.out.println("SQL statement is not executed!");
  }
  }
  catch (Exception e){
  e.printStackTrace();
  }
  }
} 

Download this example.

Database Table: datatypes

d_type d_smt d_
7 65 352
8 6565 1556
10 6565 1556

Output of program:

C:\vinod\jdbc\jdbc\PreparedStatement>javac SetByteSortLong.java

C:\vinod\jdbc\jdbc\PreparedStatement>java SetByteSortLong
Set Byte,short and long example by using Prepared Statement!
1 row(s) affected)

After executing the program:

Database Table: datatypes

d_type d_smt d_
7 65 352
8 6565 1556
10 6565 1556
35 6565 155645

Advertisements

Ads
Share on Google+Share on Google+

Set byte, short and long data types by using the Prepared Statement

Posted on: April 13, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Set byte, short and long data types by using the Prepared Statement  

Post your Comment


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

Ads

 

Ads