text: This tutorial teaches you the process of setting the byte, short and long data types in the PreparedStatement interface of the java.sql package.
  AdsTutorials
  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(); } } }
  Database Table: datatypes
  Output of program:
  After executing the program:
  Database Table: datatypes
  Advertisements
  Posted on: April 13, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles
  Advertisements
  Ads
  Ads
  Discuss: Set byte, short and long data types by using the Prepared Statement
  Post your Comment