Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

JDBC Connectivity Code In Java

In this Tutorial we want to describe you a code that helps you in understanding Jdbc Connectivity Code in Java.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

JDBC Connectivity Code In Java

     

In this Tutorial we want to describe you a code that helps you in understanding JDBC Connectivity Code in Java. In this program, the code returns you the time set by the driver to connect url and database. For this we have a class JdbcConnectivityCodeInJava and include the main method given by the list of steps are involved -

 In order to know the time required to connect url and database, we import a package java.sql,enables you to connect the url and database. Loading a driver by calling a class.forName ( ),that accept driver class as argument. Once the driver is loaded, you set the time by the driver to connect url and database. The following methods are used to set and get the time by driver -

  DriverManager.setLoginTimeout ( )  - This method set you the time taken by the driver to connect the url and a database in second.

  DriverManager.getLoginTimeout( )  -This method return the time taken by the driver to connect the url and a database in second.

On execution of the code connection login timeout is printed and connection is created.. In case there is an exception occurred  in establishing a connection between database and url in the try block. The catch block check the exception and println print the exception.

JdbcConnectivityCodeInJava.java

import java.sql.*;
public class JdbcConnectivityCodeInJava {
  public static void main(String args[]) {
  Connection con = null;
  String url = "jdbc:mysql://localhost:3306/";
  String db = "komal";
  String driver = "com.mysql.jdbc.Driver";
  String user = "root";
  String pass = "root";
  try {
  Class.forName(driver);
  DriverManager.setLoginTimeout(10);
  System.out.println("Connection Login Timeout : " +
  DriverManager.getLoginTimeout()+" Sec");
  con = DriverManager.getConnection(url + db, user, pass);
  System.out.println("Connection is created....");
  con.close();
  catch (Exception e) {
  System.out.println(e);
  }
  }
}

Output

Connection Login Timeout : 10 Sec
Connection is created....

Download code

Advertisements

Share on Google+Share on Google+

JDBC Connectivity Code In Java

Posted on: November 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: JDBC Connectivity Code In Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Vishv Deepak
August 27, 2012
JDBC

while registering jdbc driver using DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); my program gives error that unknown symbol symbol :clas jdbc please,tell obout it. thankyou sir

Ads

 

Ads