NUMBER(10,2) in oracle database [on hold]
up vote
0
down vote
favorite
Inside oracle database variable data type is NUMBER(10,2). So I need to get this value inside my java code. I tried with different ways:
FloatfloatdoubleDoubleBigDecimal
But these thing not mapping in java. What I need to do.
Thanks in advance.
java oracle type-conversion
put on hold as off-topic by Jim Garrison, Tetsuya Yamamoto, Vega, Carlos Heuberger, jww 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Jim Garrison, Tetsuya Yamamoto, Vega, Carlos Heuberger, jww
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
0
down vote
favorite
Inside oracle database variable data type is NUMBER(10,2). So I need to get this value inside my java code. I tried with different ways:
FloatfloatdoubleDoubleBigDecimal
But these thing not mapping in java. What I need to do.
Thanks in advance.
java oracle type-conversion
put on hold as off-topic by Jim Garrison, Tetsuya Yamamoto, Vega, Carlos Heuberger, jww 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Jim Garrison, Tetsuya Yamamoto, Vega, Carlos Heuberger, jww
If this question can be reworded to fit the rules in the help center, please edit the question.
1
Please try BigDecimal setting scale. BigDecimal db = new BigDecimal(d).setScale(10, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
@RaheelaAslam what is the d
– Dev4World
2 days ago
d is the actual value that comes from database.
– Raheela Aslam
2 days ago
@RaheelaAslam Why would you set scale to 10, when data type in Oracle database has a scale of 2? Why would you even set it? It'll already be 2 when read from database, and it will be rounded for you (if needed) when inserted/updated in database).
– Andreas
2 days ago
Sorry my Bad actually we have to set 2. BigDecimal db = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Inside oracle database variable data type is NUMBER(10,2). So I need to get this value inside my java code. I tried with different ways:
FloatfloatdoubleDoubleBigDecimal
But these thing not mapping in java. What I need to do.
Thanks in advance.
java oracle type-conversion
Inside oracle database variable data type is NUMBER(10,2). So I need to get this value inside my java code. I tried with different ways:
FloatfloatdoubleDoubleBigDecimal
But these thing not mapping in java. What I need to do.
Thanks in advance.
java oracle type-conversion
java oracle type-conversion
edited 2 days ago
ace
256214
256214
asked 2 days ago
Dev4World
1,97531228
1,97531228
put on hold as off-topic by Jim Garrison, Tetsuya Yamamoto, Vega, Carlos Heuberger, jww 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Jim Garrison, Tetsuya Yamamoto, Vega, Carlos Heuberger, jww
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Jim Garrison, Tetsuya Yamamoto, Vega, Carlos Heuberger, jww 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Jim Garrison, Tetsuya Yamamoto, Vega, Carlos Heuberger, jww
If this question can be reworded to fit the rules in the help center, please edit the question.
1
Please try BigDecimal setting scale. BigDecimal db = new BigDecimal(d).setScale(10, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
@RaheelaAslam what is the d
– Dev4World
2 days ago
d is the actual value that comes from database.
– Raheela Aslam
2 days ago
@RaheelaAslam Why would you set scale to 10, when data type in Oracle database has a scale of 2? Why would you even set it? It'll already be 2 when read from database, and it will be rounded for you (if needed) when inserted/updated in database).
– Andreas
2 days ago
Sorry my Bad actually we have to set 2. BigDecimal db = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
add a comment |
1
Please try BigDecimal setting scale. BigDecimal db = new BigDecimal(d).setScale(10, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
@RaheelaAslam what is the d
– Dev4World
2 days ago
d is the actual value that comes from database.
– Raheela Aslam
2 days ago
@RaheelaAslam Why would you set scale to 10, when data type in Oracle database has a scale of 2? Why would you even set it? It'll already be 2 when read from database, and it will be rounded for you (if needed) when inserted/updated in database).
– Andreas
2 days ago
Sorry my Bad actually we have to set 2. BigDecimal db = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
1
1
Please try BigDecimal setting scale. BigDecimal db = new BigDecimal(d).setScale(10, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
Please try BigDecimal setting scale. BigDecimal db = new BigDecimal(d).setScale(10, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
@RaheelaAslam what is the d
– Dev4World
2 days ago
@RaheelaAslam what is the d
– Dev4World
2 days ago
d is the actual value that comes from database.
– Raheela Aslam
2 days ago
d is the actual value that comes from database.
– Raheela Aslam
2 days ago
@RaheelaAslam Why would you set scale to 10, when data type in Oracle database has a scale of 2? Why would you even set it? It'll already be 2 when read from database, and it will be rounded for you (if needed) when inserted/updated in database).
– Andreas
2 days ago
@RaheelaAslam Why would you set scale to 10, when data type in Oracle database has a scale of 2? Why would you even set it? It'll already be 2 when read from database, and it will be rounded for you (if needed) when inserted/updated in database).
– Andreas
2 days ago
Sorry my Bad actually we have to set 2. BigDecimal db = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
Sorry my Bad actually we have to set 2. BigDecimal db = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
Use ResultSet.getBigDecimal(int) to get by index
or ResultSet.getBigDecimal(String) to get by column name
This is also recommended in JDBC Guide for any Numeric type
JDBC Types Mapped to Java Types
NUMERIC java.math.BigDecimal
add a comment |
up vote
0
down vote
Check out this link Data Type and Java-to-Java Type Mappings.
NUMBER is mapped to java.math.BigDecimal
For 2 numbers after comma you can set:
new BigDecimal(dbValue).setScale(2, RoundingMode.HALF_UP);
add a comment |
up vote
0
down vote
You can use double or BigDecimal.
Example, assuming table ATEST with column N defined as CREATE TABLE ATEST ( N NUMBER(10,2) ):
import java.math.BigDecimal;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class OraNum {
private static String url = "jdbc:oracle:thin:@localhost:1523:sid";
private static String user = "User";
public static void main(String args) throws Exception {
var pwd = JOptionPane.showInputDialog("Password: ");
if (pwd == null) {
return;
}
try (
var conn = DriverManager.getConnection(url, user, pwd);
var stmt = conn.prepareStatement("select N from ATEST");
var rset = stmt.executeQuery();
) {
while (rset.next()) {
double d = rset.getDouble(1);
BigDecimal big = rset.getBigDecimal(1);
System.out.printf("double: %s, BigDecimal: %s%n", d, big);
}
}
}
}
You could also use rset.getObject() to get an Object. Checking its class you will see what the driver returns for that type (BigDecimal here); getDouble() just converts it to double - this may cause some precision lost, so is is probably better to use BigDecimal. (same true for getInt)
Note: rset.getString() would also work, returning the textual representation (String) of the number.
BTW above code is a Minimal, Complete, and Verifiable example (well, almost minimal, I didn't want to include the password)
– Carlos Heuberger
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Use ResultSet.getBigDecimal(int) to get by index
or ResultSet.getBigDecimal(String) to get by column name
This is also recommended in JDBC Guide for any Numeric type
JDBC Types Mapped to Java Types
NUMERIC java.math.BigDecimal
add a comment |
up vote
1
down vote
Use ResultSet.getBigDecimal(int) to get by index
or ResultSet.getBigDecimal(String) to get by column name
This is also recommended in JDBC Guide for any Numeric type
JDBC Types Mapped to Java Types
NUMERIC java.math.BigDecimal
add a comment |
up vote
1
down vote
up vote
1
down vote
Use ResultSet.getBigDecimal(int) to get by index
or ResultSet.getBigDecimal(String) to get by column name
This is also recommended in JDBC Guide for any Numeric type
JDBC Types Mapped to Java Types
NUMERIC java.math.BigDecimal
Use ResultSet.getBigDecimal(int) to get by index
or ResultSet.getBigDecimal(String) to get by column name
This is also recommended in JDBC Guide for any Numeric type
JDBC Types Mapped to Java Types
NUMERIC java.math.BigDecimal
edited 2 days ago
answered 2 days ago
user7294900
17k92954
17k92954
add a comment |
add a comment |
up vote
0
down vote
Check out this link Data Type and Java-to-Java Type Mappings.
NUMBER is mapped to java.math.BigDecimal
For 2 numbers after comma you can set:
new BigDecimal(dbValue).setScale(2, RoundingMode.HALF_UP);
add a comment |
up vote
0
down vote
Check out this link Data Type and Java-to-Java Type Mappings.
NUMBER is mapped to java.math.BigDecimal
For 2 numbers after comma you can set:
new BigDecimal(dbValue).setScale(2, RoundingMode.HALF_UP);
add a comment |
up vote
0
down vote
up vote
0
down vote
Check out this link Data Type and Java-to-Java Type Mappings.
NUMBER is mapped to java.math.BigDecimal
For 2 numbers after comma you can set:
new BigDecimal(dbValue).setScale(2, RoundingMode.HALF_UP);
Check out this link Data Type and Java-to-Java Type Mappings.
NUMBER is mapped to java.math.BigDecimal
For 2 numbers after comma you can set:
new BigDecimal(dbValue).setScale(2, RoundingMode.HALF_UP);
edited 2 days ago
answered 2 days ago
ace
256214
256214
add a comment |
add a comment |
up vote
0
down vote
You can use double or BigDecimal.
Example, assuming table ATEST with column N defined as CREATE TABLE ATEST ( N NUMBER(10,2) ):
import java.math.BigDecimal;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class OraNum {
private static String url = "jdbc:oracle:thin:@localhost:1523:sid";
private static String user = "User";
public static void main(String args) throws Exception {
var pwd = JOptionPane.showInputDialog("Password: ");
if (pwd == null) {
return;
}
try (
var conn = DriverManager.getConnection(url, user, pwd);
var stmt = conn.prepareStatement("select N from ATEST");
var rset = stmt.executeQuery();
) {
while (rset.next()) {
double d = rset.getDouble(1);
BigDecimal big = rset.getBigDecimal(1);
System.out.printf("double: %s, BigDecimal: %s%n", d, big);
}
}
}
}
You could also use rset.getObject() to get an Object. Checking its class you will see what the driver returns for that type (BigDecimal here); getDouble() just converts it to double - this may cause some precision lost, so is is probably better to use BigDecimal. (same true for getInt)
Note: rset.getString() would also work, returning the textual representation (String) of the number.
BTW above code is a Minimal, Complete, and Verifiable example (well, almost minimal, I didn't want to include the password)
– Carlos Heuberger
2 days ago
add a comment |
up vote
0
down vote
You can use double or BigDecimal.
Example, assuming table ATEST with column N defined as CREATE TABLE ATEST ( N NUMBER(10,2) ):
import java.math.BigDecimal;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class OraNum {
private static String url = "jdbc:oracle:thin:@localhost:1523:sid";
private static String user = "User";
public static void main(String args) throws Exception {
var pwd = JOptionPane.showInputDialog("Password: ");
if (pwd == null) {
return;
}
try (
var conn = DriverManager.getConnection(url, user, pwd);
var stmt = conn.prepareStatement("select N from ATEST");
var rset = stmt.executeQuery();
) {
while (rset.next()) {
double d = rset.getDouble(1);
BigDecimal big = rset.getBigDecimal(1);
System.out.printf("double: %s, BigDecimal: %s%n", d, big);
}
}
}
}
You could also use rset.getObject() to get an Object. Checking its class you will see what the driver returns for that type (BigDecimal here); getDouble() just converts it to double - this may cause some precision lost, so is is probably better to use BigDecimal. (same true for getInt)
Note: rset.getString() would also work, returning the textual representation (String) of the number.
BTW above code is a Minimal, Complete, and Verifiable example (well, almost minimal, I didn't want to include the password)
– Carlos Heuberger
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use double or BigDecimal.
Example, assuming table ATEST with column N defined as CREATE TABLE ATEST ( N NUMBER(10,2) ):
import java.math.BigDecimal;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class OraNum {
private static String url = "jdbc:oracle:thin:@localhost:1523:sid";
private static String user = "User";
public static void main(String args) throws Exception {
var pwd = JOptionPane.showInputDialog("Password: ");
if (pwd == null) {
return;
}
try (
var conn = DriverManager.getConnection(url, user, pwd);
var stmt = conn.prepareStatement("select N from ATEST");
var rset = stmt.executeQuery();
) {
while (rset.next()) {
double d = rset.getDouble(1);
BigDecimal big = rset.getBigDecimal(1);
System.out.printf("double: %s, BigDecimal: %s%n", d, big);
}
}
}
}
You could also use rset.getObject() to get an Object. Checking its class you will see what the driver returns for that type (BigDecimal here); getDouble() just converts it to double - this may cause some precision lost, so is is probably better to use BigDecimal. (same true for getInt)
Note: rset.getString() would also work, returning the textual representation (String) of the number.
You can use double or BigDecimal.
Example, assuming table ATEST with column N defined as CREATE TABLE ATEST ( N NUMBER(10,2) ):
import java.math.BigDecimal;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class OraNum {
private static String url = "jdbc:oracle:thin:@localhost:1523:sid";
private static String user = "User";
public static void main(String args) throws Exception {
var pwd = JOptionPane.showInputDialog("Password: ");
if (pwd == null) {
return;
}
try (
var conn = DriverManager.getConnection(url, user, pwd);
var stmt = conn.prepareStatement("select N from ATEST");
var rset = stmt.executeQuery();
) {
while (rset.next()) {
double d = rset.getDouble(1);
BigDecimal big = rset.getBigDecimal(1);
System.out.printf("double: %s, BigDecimal: %s%n", d, big);
}
}
}
}
You could also use rset.getObject() to get an Object. Checking its class you will see what the driver returns for that type (BigDecimal here); getDouble() just converts it to double - this may cause some precision lost, so is is probably better to use BigDecimal. (same true for getInt)
Note: rset.getString() would also work, returning the textual representation (String) of the number.
edited 2 days ago
answered 2 days ago
Carlos Heuberger
23.7k85077
23.7k85077
BTW above code is a Minimal, Complete, and Verifiable example (well, almost minimal, I didn't want to include the password)
– Carlos Heuberger
2 days ago
add a comment |
BTW above code is a Minimal, Complete, and Verifiable example (well, almost minimal, I didn't want to include the password)
– Carlos Heuberger
2 days ago
BTW above code is a Minimal, Complete, and Verifiable example (well, almost minimal, I didn't want to include the password)
– Carlos Heuberger
2 days ago
BTW above code is a Minimal, Complete, and Verifiable example (well, almost minimal, I didn't want to include the password)
– Carlos Heuberger
2 days ago
add a comment |
1
Please try BigDecimal setting scale. BigDecimal db = new BigDecimal(d).setScale(10, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago
@RaheelaAslam what is the d
– Dev4World
2 days ago
d is the actual value that comes from database.
– Raheela Aslam
2 days ago
@RaheelaAslam Why would you set scale to 10, when data type in Oracle database has a scale of 2? Why would you even set it? It'll already be 2 when read from database, and it will be rounded for you (if needed) when inserted/updated in database).
– Andreas
2 days ago
Sorry my Bad actually we have to set 2. BigDecimal db = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);
– Raheela Aslam
2 days ago