Prepared Statement Set Big Decimal

In this JDBC section we are going to learn about the big decimal and how can be set it in the database table by using the PreparedStatement interface of java.sql package.

Prepared Statement Set Big Decimal

Prepared Statement Set Big Decimal

     

In this JDBC section we are going to learn about the big decimal and how can be set it in the database table by using the PreparedStatement interface of java.sql package. But here arises a question, what is big decimal and which type to use this in the database. We know that using of both data types MySQL and Java programming language are not same. So mapping between both is essential to integrate to each other, here we will work on the big decimal data type in java to integrate in MySQL through the help of NUMERIC and DECIMAL data types. See detail description below:

Description of program:

In this program we are going to insert big decimal data type in the database table (bigdecimal) through the use of PreparedStatement interface. Here, first of all we need to establish the connection with MySQL database by using the JDBC driver. After establishing the connection we will give own query in the prepareStatement method that provides us the PreparedStatement object. Now we will add all data in the data according to database table (Records, sal) . The sal column contains the big decimal data types. When we will get added data in the database table then it will provide a message "Successfully!" otherwise it shows "SQL statement is not executed!".

Description of code:

BigDecimal(String val);
This is the constructor of java.sql.BigDecimal class that takes one string type argument and translates it into a BigDecimal.

setBigDecimal( int index, BigDecimal big);
Above method takes integer type index number another is BigDecimal types number and sets an arguments to the given java.math.BigDecimal value.

Here is the code of program:

import java.sql.*;
import java.math.*;

public class SetBigDecimal{
  public static void main(String[] args) {
  BigDecimal str = null;
  System.out.println("Set BigDecimal Example!");
  Connection con = null;
  try{
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:mysql:
//localhost:3306/jdbctutorial"
,"root","root");
  try{
  String sql = "INSERT bigdecimal VALUES(?,?)";
  PreparedStatement prest = con.prepareStatement(sql);
  prest.setString(1,"Deepak");
  str = new BigDecimal("54547751418641878415841554878454");
  prest.setBigDecimal(2,str);
  prest.executeUpdate();
  System.out.println("Successfully!");
  }
  catch (SQLException s){
  System.out.println("SQL statement is not executed!");
  }
  }
  catch (Exception e){
  e.printStackTrace();
  }
  }
}

Download this example.

Output of program:

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

C:\vinod\jdbc\jdbc\PreparedStatement>java SetBigDecimal
Set BigDecimal Example!
Successfully!

Database Table: bigdecimal

Records sal
Rajesh 5.45152565625456e+032
Santhosh 5.45155487845426e+040
Tapan 5451554878454
Deepak 5.45477514186419e+031

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