package org.kodejava.basic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TypeSpecificCollection {
public static void main(String[] args) {
// Using a Generic can enable us to create a type specific collection
// object. In the example below we create a Map whose key is an Integer
// a have the value of a String.
Map<Integer, String> grades = new HashMap<>();
grades.put(1, "A");
grades.put(2, "B");
grades.put(3, "C");
grades.put(4, "D");
grades.put(5, "E");
// A value obtained from type specific collection doesn't need to
// be cast, it knows the type returned.
String value = grades.get(1);
System.out.println("value = " + value);
// Creating a List that will contain a String only values.
List<String> dayNames = new ArrayList<>();
dayNames.add("Sunday");
dayNames.add("Monday");
dayNames.add("Tuesday");
dayNames.add("Wednesday");
// We also don't need to cast the retrieved value because it knows the
// returned type object.
String firstDay = dayNames.get(0);
System.out.println("firstDay = " + firstDay);
}
}
Category Archives: Java
How do I get all available timezones?
package org.kodejava.util;
import java.util.TimeZone;
public class TimezonesExample {
public static void main(String[] args) {
String[] availableTimezones = TimeZone.getAvailableIDs();
for (String timezone : availableTimezones) {
System.out.println("Timezone ID = " + timezone);
}
}
}
Some examples of the returned timezones ids are:
Timezone ID = Etc/GMT+12
Timezone ID = Etc/GMT+11
Timezone ID = MIT
Timezone ID = Pacific/Apia
Timezone ID = Pacific/Midway
Timezone ID = Pacific/Niue
Timezone ID = Pacific/Pago_Pago
Timezone ID = Pacific/Samoa
Timezone ID = US/Samoa
...
How do I get data types supported by database?
package org.kodejava.jdbc;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
public class DatabaseSupportedType {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "kodejava";
private static final String PASSWORD = "s3cr*t";
public static void main(String[] args) {
try (Connection connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
DatabaseMetaData metadata = connection.getMetaData();
ResultSet resultSet = metadata.getTypeInfo();
while (resultSet.next()) {
String typeName = resultSet.getString("TYPE_NAME");
System.out.println("Type Name = " + typeName);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Here are the data types supported by MySQL database:
Type Name = BIT
Type Name = TINYINT
Type Name = TINYINT UNSIGNED
Type Name = BIGINT
Type Name = BIGINT UNSIGNED
Type Name = LONG VARBINARY
Type Name = MEDIUMBLOB
Type Name = LONGBLOB
Type Name = BLOB
Type Name = VARBINARY
Type Name = TINYBLOB
Type Name = BINARY
Type Name = LONG VARCHAR
Type Name = MEDIUMTEXT
Type Name = LONGTEXT
Type Name = TEXT
Type Name = CHAR
Type Name = ENUM
Type Name = SET
Type Name = DECIMAL
Type Name = NUMERIC
Type Name = INTEGER
Type Name = INT
Type Name = MEDIUMINT
Type Name = INTEGER UNSIGNED
Type Name = INT UNSIGNED
Type Name = MEDIUMINT UNSIGNED
Type Name = SMALLINT
Type Name = SMALLINT UNSIGNED
Type Name = FLOAT
Type Name = DOUBLE
Type Name = DOUBLE PRECISION
Type Name = REAL
Type Name = DOUBLE UNSIGNED
Type Name = DOUBLE PRECISION UNSIGNED
Type Name = VARCHAR
Type Name = TINYTEXT
Type Name = BOOL
Type Name = DATE
Type Name = YEAR
Type Name = TIME
Type Name = DATETIME
Type Name = TIMESTAMP
Maven Dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
How do I know if database support transaction?
A database transaction in any relational database management system (RDBMS) is a sequence of one or more SQL operations that are executed as a single unit of work. The primary characteristics and properties of transactions are encapsulated in the ACID principles:
- Atomicity: Ensures that all operations within the transaction are completed successfully. If any operation fails, the entire transaction is rolled back, and the database state is left unchanged.
- Consistency: Guarantees that a transaction will bring the database from one valid state to another. The database must be consistent before and after the transaction.
- Isolation: Ensures that concurrently executed transactions do not affect each other. The intermediate state of a transaction is invisible to other transactions.
- Durability: Once a transaction is committed, it will remain so, even in the event of a system failure.
Using JDBC API in the code snippet below we can check whether a database support transaction or not:
package org.kodejava.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
public class TransactionSupportChecker {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "kodejava";
private static final String PASSWORD = "s3cr*t";
public static void main(String[] args) {
try (Connection connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
DatabaseMetaData metadata = connection.getMetaData();
boolean supported = metadata.supportsTransactions();
System.out.println("Product Name : " + metadata.getDatabaseProductName());
System.out.println("Supports Transaction: " + supported);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output of the code snippet:
Product Name : MySQL
Supports Transaction: true
Maven Dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
How do I get database product information?
The code below helps you to get some product information about the database that you use in creating your program. You can retrieve database information such as the major and minor version of the product, the database product name and its release version.
package org.kodejava.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
public class DatabaseProductInfo {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "kodejava";
private static final String PASSWORD = "s3cr*t";
public static void main(String[] args) {
try (Connection connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
DatabaseMetaData metadata = connection.getMetaData();
String productName = metadata.getDatabaseProductName();
String productVersion = metadata.getDatabaseProductVersion();
int majorVersion = metadata.getDatabaseMajorVersion();
int minorVersion = metadata.getDatabaseMinorVersion();
System.out.println("Product Name = " + productName);
System.out.println("Product Version = " + productVersion);
System.out.println("Major Version = " + majorVersion);
System.out.println("Minor Version = " + minorVersion);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
And here are the results:
Product Name = MySQL
Product Version = 5.7.43
Major Version = 5
Minor Version = 7
Maven Dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
