Reading:  

Getting started with JDBC


Data Types

The JDBC driver maps Java data type to appropriate JDBC type before interacting with the database. For example a Java string is converted to an SQL CHAR.

The JDBC Type and Java data mapping is as below:-

SQL

JDBC/Java

VARCHAR

java.lang.String

CHAR

java.lang.String

LONGVARCHAR

java.lang.String

BIT

boolean

NUMERIC

java.math.BigDecimal

TINYINT

byte

SMALLINT

short

INTEGER

int

BIGINT

long

REAL

float

FLOAT

float

DOUBLE

double

VARBINARY

byte[ ]

BINARY

byte[ ]

DATE

java.sql.Date

TIME

java.sql.Time

TIMESTAMP

java.sql.Timestamp

CLOB

java.sql.Clob

BLOB

java.sql.Blob

ARRAY

java.sql.Array

REF

java.sql.Ref

STRUCT

java.sql.Struct

JDBC 3.0 supports CLOB, BLOB, REF and ARRAY. To manipulate data on server the ResultSet object has updateCLOB(),updateBLOB(),  updateRef() and updateArray() methods.

Date & time Data Types:

The java.sql.Date class matches to SQL DATE type, java.sql.Timestamp and java.sql.Time classes matches to SQL TIMESTAMP and SQL TIME data types.

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.*;
public class SqlDateTime {
public static void main(String[] args) {
// date and time
java.util.Date javaNewDate = new java.util.Date();
long javaNewTime = javaDate.getTime();
System.out.println("Current Java date:" +
javaNewDate.toString());
java.sql.Date sqlNewDate = new java.sql.Date(javaTime);
System.out.println("Current Date from SQL: " +
sqlDate.toString());
}
}

 

 

Save the file as JavaDateTime.Java complie and run it

C:\>javac JavaDateTime.Java

C:\>java JavaDateTime

Current Java Date: Tue Mar 3 11:26:42 GMT+04:00 2015

Current Date from SQL: 2015-03-03

Description

This tutorial is focused on getting you started with JDBC 4.1. This tutorial has following parts

  1. Introduction
  2. SQL Syntax
  3. Setting up Environment
  4. Getting started with some samples
  5. Connecting to database
  6. Statements
  7. Result Sets
  8. Data Types
  9. Transactions
  10. Exception handling
  11. Batch Processing
  12. Streaming Data
  13. More JDBC Examples

Leave your feedback to help us improve next time. Also let us know if you see any errors that needs to be corrected



Prerequisites

Understanding of Java language a must to understand various things in this tutorial

Audience

Absolute beginners who wants to get started with JDBC

Author: Subject Coach
Added on: 8th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!