Java – equivalent of Long data type in PostgreSQL
I want to know what’s the equivalent of Long data type in PostgreSQL?
Best Solution
According to the docs it looks like bigint is your friend, with a range of -9223372036854775808 to 9223372036854775807.
Related Solutions
PostgreSQL “DESCRIBE TABLE”
Try this (in the psql command-line tool):
Java – a serialVersionUID and why should I use it
The docs for java.io.Serializable are probably about as good an explanation as you’ll get:
The serialization runtime associates with each serializable class a version number, called a serialVersionUID , which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender’s class, then deserialization will result in an InvalidClassException . A serializable class can declare its own serialVersionUID explicitly by declaring a field named serialVersionUID that must be static, final, and of type long :
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
If a serializable class does not explicitly declare a serialVersionUID , then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class — serialVersionUID fields are not useful as inherited members.
Related Question
Java long in postgresql
Java users commonly use different databases to efficiently store and manipulate data. PostgreSQL is a popular choice among developers due to its notable features like security, scalability, and ease of use. However, when working with large numeric datasets, storing and managing enormous data can be challenging. For instance, Postgres offers BIGINT data type to store gigantic numeric values while Java keeps them in Long data type.
To address this problem, the «BIGINT» data type is used in PostgreSQL, which serves as an alternative to Java’s Long data type.
This post will present a comprehensive guide on Postgres’ equivalent of Java’s long data type.
What is Java’s Long Data Type?
Before understanding how BIGINT works, first, you must understand Java’s LONG data type. In Java, the long data type is commonly used to handle extremely large whole numbers. It allows us to store numbers as small as «-9,223,372,036,854,775,808» and as large as «9,223,372,036,854,775,807». To declare a variable of type long, simply use the keyword «long» followed by the desired variable name:
long num = 9223372036854775L;
«L» must be appended at the end of the number.
What is the Equivalent of Java Long in Postgres?
In Postgres, the «BIGINT» data type corresponds to Java’s long data type and is used to store extremely large positive or negative numbers. The range of BIGINT spans from “-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807”.
Example: How BIGINT Works in PostgreSQL?
To illustrate the usage of the «BIGINT» data type in Postgres, we can create a table using the «CREATE TABLE» command and define a column with the «BIGINT» data type, as shown below:
CREATE TABLE cp_table ( cp_id INT PRIMARY KEY, cp_name TEXT NOT NULL, cp_number BIGINT );
The “cp_table” has been created successfully. Let’s insert a really big value into the “cp_table” using the INSERT query:
INSERT INTO cp_table (cp_id, cp_name, cp_number) VALUES (1, 'Dean', 922337203685476800), (2, 'Joe', 922337203685477800), (3, 'Ambrose', 922337203685476802), (4, 'Joseph', 92233720368776804), (5, 'Mike', 922337203685768001);
Let’s fetch the table’s data using the below-provided query:
This is how you can store and manipulate really big numeric values in Postgres.
In PostgreSQL, the «BIGINT» data type is used to store extremely large positive or negative numbers and can serve as an alternative to Java’s Long data type. It allows us to store numbers as small as «-9,223,372,036,854,775,808» and as large as «9,223,372,036,854,775,807». This post has explained how to use BIGINT as an alternative to Java’s LONG data type.
Java long in postgresql
A long time ago at university, I learned my first high-level programming language, Pascal, from the book by Niklaus Wirth:
But I didn’t really learn much about databases studying computer science, as they were taught by another department (business systems). So maybe now’s the time to start with an imaginary book called something like:
Queries + Data Types = Databases?
Data types have a long and important history in computing, driven initially by word lengths and machine data types but becoming more powerful and abstract as computer science matured. One popular 1980’s magazine was even named after a data type (“BYTE”, early microprocessors such as the 8008, Z80, and 6800 were characterized by BYTE/8-bit word sizes, in an era when the PDP-11 had 16-bit words, the VAX had 32-bit words, and the Cray-1 a massive 64-bit word size).
2. PostgreSQL Data Types
“Data Types” is a popular PostgreSQL search, so I decided to do some investigation of my own into why they are so important. First of all, why do data types matter in PostgreSQL? Doing some preliminary research I found out that data types in PostgreSQL are important for at least the following aspects (possibly more!):
- As column data types when creating a table
- For functions and operators
- For constraints
- For creating types and domains, and
- When using PostgreSQL from a programming language (e.g. PostgreSQL to/from Python, and “C”).
PostgreSQL has a lot of built-in data types that are described in Chapter 8 of the documentation. And you can add new data types, so I guess there are really an infinite number of data types possible.
There’s a table that enumerates at least 43 built-in data types, and reveals that along with the official name some types have aliases (used internally for historical reasons). For example “real” has the alias “float4” (a single precision 4-byte floating-point number).
Here’s the full table which shows the variety of data types available:
Name | Aliases | Description |
bigint | int8 | signed eight-byte integer |
bigserial | serial8 | autoincrementing eight-byte integer |
bit [ (n) ] | fixed-length bit string | |
bit varying [ (n) ] | varbit [ (n) ] | variable-length bit string |
boolean | bool | logical Boolean (true/false) |
box | rectangular box on a plane | |
bytea | binary data (“byte array”) | |
character [ (n) ] | char [ (n) ] | fixed-length character string |
character varying [ (n) ] | varchar [ (n) ] | variable-length character string |
cidr | IPv4 or IPv6 network address | |
circle | circle on a plane | |
date | calendar date (year, month, day) | |
double precision | float8 | double precision floating-point number (8 bytes) |
inet | IPv4 or IPv6 host address | |
integer | int, int4 | signed four-byte integer |
interval [ fields ] [ (p) ] | time span | |
json | textual JSON data | |
jsonb | binary JSON data, decomposed | |
line | infinite line on a plane | |
lseg | line segment on a plane | |
macaddr | MAC (Media Access Control) address | |
macaddr8 | MAC (Media Access Control) address (EUI-64 format) | |
money | currency amount | |
numeric [ (p, s) ] | decimal [ (p, s) ] | exact numeric of selectable precision |
path | geometric path on a plane | |
pg_lsn | PostgreSQL Log Sequence Number | |
pg_snapshot | user-level transaction ID snapshot | |
point | geometric point on a plane | |
polygon | closed geometric path on a plane | |
real | float4 | single precision floating-point number (4 bytes) |
smallint | int2 | signed two-byte integer |
smallserial | serial2 | autoincrementing two-byte integer |
serial | serial4 | autoincrementing four-byte integer |
text | variable-length character string | |
time [ (p) ] [ without time zone ] | time of day (no time zone) | |
time [ (p) ] with time zone | timetz | time of day, including time zone |
timestamp [ (p) ] [ without time zone ] | date and time (no time zone) | |
timestamp [ (p) ] with time zone | timestamptz | date and time, including time zone |
tsquery | text search query | |
tsvector | text search document | |
txid_snapshot | user-level transaction ID snapshot (deprecated; see pg_snapshot) | |
uuid | universally unique identifier | |
xml | XML data |
Table 1: Postgres Data Types (Name, Alias, Description)
But how do you know what you can do with each data type? Chapter 9 documents which functions and operators are applicable to each data type. The documentation also says that each data type has an external representation, which raises the question of what these “external representations” are either in standard SQL data types or for a specific programming language.
3. Using PostgreSQL From Java—the PgJDBC Driver
How do you use PostgreSQL from Java? With JDBC! (Java Database Connectivity). There’s a PostgreSQL JDBC Driver (PgJDBC for short) which allows Java programs to connect using standard, database independent, Java code. It’s an open source Pure Java (Type 4, which talks native PostgreSQL protocol) driver and is well documented.
It’s easy to download PostgreSQL, install it, and start the database server running. You also need to download the JDBC driver.
Connecting to the database is easy from jdbc:
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
PostgreSQL Types
Clone this wiki locally
Supported Java types and their destination types on PostgreSQL
All types also support their array versions, but they are returned as IndexedSeq of the type and not pure Array types.
PostgreSQL type | Java type |
---|---|
boolean | Boolean |
smallint | Short |
integer (or serial) | Int |
bigint (or bigserial) | Long |
numeric | BigDecimal |
real | Float |
double | Double |
text | String |
varchar | String |
bpchar | String |
timestamp | LocalDateTime |
timestamp_with_timezone | DateTime |
date | LocalDate |
time | LocalTime |
bytea | Array[Byte] (PostgreSQL 9.0 and above only) |
All other types are returned as String.
Now from Scala/Java types to PostgreSQL types (when using prepared statements):
- Array types are encoded with the kind of object they hold and not the array type itself. Java Collection and Scala Traversable objects are also assumed to be arrays of the types they hold and will be sent to PostgreSQL like that.
- null values are supported, In prepared statement pass null .
- Spatial / Geometry data types (postgisjts) are supported via module jasync-postgis-jts . In order to use geometry types import the module and init with the following code:
JasyncPostgisRegister.init(connection)