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.

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

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

Tutorials

  1. Mapping MySQL Data Types in Java
  2. Connecting to a MySQL Database in Java
  3. Creating a Database in MySQL
  4. Creating a Database Table
  5. Creating a MySQL Database Table to store Java Types
  6. Description of Database Table
  7. Deleting a Table from Database
  8. Retrieving Tables from a Database
  9. Inserting values in MySQL database table
  10. Retrieving All Rows from a Database Table
  11. Adding a New Column Name in Database Table
  12. Change Column Name of a Table
  13. Make Unique Column in Database Table
  14. Remove Unique Column in Database Table
  15. Arrange a Column of Database Table
  16. Arrange a Column of Database Table
  17. Sum of Column in a Database Table
  18. Deleting All Rows from a Database Table
  19. Delete a Specific Row from a Database Table
  20. Delete a Column from a Database Table
  21. Join tables in the specific database
  22. Join tables with the NATURAL LEFT JOIN operation
  23. Join tables with the NATURAL RIGHT JOIN operation
  24. Cross Join Tables in a Specific Database
  25. Prepared Statement Set Object
  26. Statement Batch Update
  27. Prepared Statement With Batch Update
  28. Select Records Using Prepared Statement
  29. Update Records using Prepared Statement
  30. Inserting Records using the Prepared Statement
  31. Count Records using the Prepared Statement
  32. Deleting Records using the Prepared Statement
  33. Using the Prepared Statement Twice
  34. Set Data Types by using Prepared Statement
  35. Set byte, short and long data types by using the Prepared Statement
  36. Prepared Statement Set Big Decimal
  37. Set Date by using the Prepared Statement
  38. Set Time by using the Prepared Statement
  39. Set Timestamp by using the Prepared Statement
  40. Copy Table in a MySQL Database