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

JDBC connection timeout

In this Tutorial we want to describe you a code that help you in understand Jdbc connection timeout.

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 connection timeout

     

The Tutorial describe you a JDBC Connection timeout. In this program, we want to explain you a code that help you in understand the maximum time for which driver can wait to connect database.

Understand with Example

The code include a class JDBC Connection Timeout, inside the main method we include the list of following steps -

  1. The first step is to import a package name Java.sql.*  -  This package import the definition of all classes present in this package.
  2. Loading a driver -The next step is load a driver by calling class.forname( ) and passed the driver as argument in it. 
  3. DriverManager.getLoginTimeout( ) -This method return the maximum time for the driver can wait while trying to connect a database.
  4. DriverManager.getconnection ( ) - This method attempt to connect a url and database.
  5. con.createstatement ( ) - This method is used to return an Sql object. Once a connection is built ,your application page can interact with backend database. A connection object is used further to send and execute SQL Statement to a backend database
  6. next(  ) - This method return you the next successive element in series.
  7. execute query - This method is used to retrieve a record from a database using select queries in sql .

On the execution of code,the code attempt to establish a connection between url and database in 10 sec.Finally the println print the element from the database.

JdbcConnectionTimeout.java



import java.sql.*;
public class JdbcConnectionTimeout {
  public static void main(String args[]) {
  Connection con = null;
  Statement st = null;
  ResultSet rs = 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);
  
;
  System.out.println("Connection Login Timeout : " +
  DriverManager.getLoginTimeout());
  con = DriverManager.getConnection(url + db, user, pass);
  st = con.createStatement();
  String sql = "select * from customer";
  rs = st.executeQuery(sql);
  System.out.println("First Name  \tLast Name");
  while (rs.next()) {
  System.out.print(rs.getString("First_Name") + " \t");
  System.out.println(rs.getString("Last_Name"));
  }
  catch (Exception e) {
  System.out.println(e);
  }
  }

Output

Connection Login Timeout : 10
First Name  	Last Name
girish   	tewari
mahendra   	singh
komal   	singh
yogesh   	pandey
pinku   	tripathi
deepak   	tripathi 

Download code

Advertisements

Online Training Classes

Share on Google+Share on Google+

JDBC connection timeout

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 connection timeout  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads