Jdbc Connection

Jdbc Connection Using Oracle, SQL Server, My Sql

A jdbc connection is the object where commands are sent to the database and responses are returned to the program. Jdbc connection is that enables Java programs to execute SQL statements. The JDBC API consists of a set of interfaces and classes written in the Java programming language.

All databases provide their driver that enable users to make JDBC connections to databases. The two most common methods of connecting to Oracle databases  are Oracle Thin JDBC driver and the Oracle OCI JDBC driver.

For mssql server there are two ways we can make jdbc connection, using windows authentication or SQL authentication. For this there are two commonly used drivers JDBC and JTDS.

Steps of making Jdbc Connection

  • Load the JDBC driver
  • Define the connection url
  • Establish the connection using connection parameters

Examples of JDBC driver

Oracle :

jar file:  ojdbc14-10.2.0.3.0.jar

MSSQL :

jar files : sqljdbc.jar   sqljdbc4-4.0.jar 

My Sql:

jar file – mysql-connector-java-5.1.6.jar

Examples of Connection Url

Oracle :

MSSQL :

My SQL :

Examples of Jdbc Connection parameters

Oracle/MSSQL/My Sql :

jdbc connection – common errors

MSSQL:

  • Error: “java.net.SocketTimeoutException: Receive timed out”. Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.
  • com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host  has failed. java.net.ConnectException: Connection refused: connect

Cause : 1. protocol (TCP/IP) is not enabled  2.Port is incorrect  3. missing of sqljdbc_auth.dll

Solutions:

1. protocol (TCP/IP) is not enabled :

a) Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager.  > Protocols for inst01, instance name, under SQL Server Network Configuration. Right-click on TCP/IP and choose Properties. Set Enabled = YES.  [this should be done by admin user]

Jdbc Connection - SQL Server Configuration Manager

Jdbc Connection – SQL Server Configuration Manager

2. Port is incorrect : Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager  > SQL Server Network Configuration >Protocols for inst01, instance name,  > Properties of TCP/IP  > IP Address tab and set TCP Port: 1433 , under IP All [this should be done by admin user]

3. missing of sqljdbc_auth.dll –

a) Download latest MSSQL JDBC driver from here: link1 , link2

b) take sqljdbc_auth.dll from x86/x64 folder.

c) copy the sqljdbc_auth.dll to your JDK directory bin and windowssystem32 folder. (you do not need restart your machine)

Download

To get a complete project of jdbc connection with MSSQL, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *