Share on Google+Share on Google+

JDBC Database URLs

In this Section, We will discuss about JDBC Database URL,specifying a JDBC driver name,Connection to a database table.

JDBC Database URLs

In this Section, We will discuss about following topics :

  • JDBC Database URL
  • specify a JDBC driver name
  • Connection to a database table

JDBC Database URL

JDBC Uniform resource locater or 'JDBC URL'  is used to identify a database using a unique name or address. All database should be connect to database using these URL strings. Format of JDBC URL :


In this URL string, the first string "jdbc" represents the protocol name, "subprotocol" represents the type of database you want to connect .Example-jdbc, oracle, Mysql. While "subname " provide additional information like host name or machine name, port, database name or instance etc.


  • JDBC ODBC Bridge driver:


  • Oracle JDBC Driver(TYPE 4) :


  • MySql Connector JDBC driver


Note: You can Download Mysql Connector From the site "". You have to paste it in "lib" folder of "jdk" directory.

Specifying a  JDBC Driver Name

To load driver before  connection , you must know the full class name of your JDBC driver. We can load driver in two different ways, theses are :

WAY-1: To load the the driver/s at JVM startup, specify the driver/s in  jdbc.drivers system property like this:

    java -Djdbc.drivers=com.mysql.jdbc.Driver YourJavaProgram


WAY2: To load driver explicitly, use "Class.forName()" method like this :



Connection to a database table

For connecting to a database, You must import  "java.sql.*" package. After loading Driver  implicitly or explicitly, as mention above. You will have to create connection with the help of "getConnection() " method using "Connection" interface object. In "getConnection()" method you have to pass "JdbcURL" of the type of Database you want to connect. After creating connection,  Queries will be run with the help of  "createStatement ()"  method using "Statement" or "PrepareStatement" object.


In this code, we load the  JDBC driver at JVM startup without using "Class.forName()" .

import java.sql.*;

class JdbcUrl{

public static void main(String[] args) {
try {
//URL of the database(ankdb)
  String connectionURL = "jdbc:mysql://";

// declare a connection by using Connection interface 
    Connection connection = null;

// declare object of Statement interface that uses for 
//executing sql statements.
    Statement statement = null;

// declare a resultset that uses as a table
// for output data from the table.
    ResultSet rs = null;

connection = DriverManager.getConnection(connectionURL,

statement = connection.createStatement();
System.out.println(" Showing row of student table");

 // sql query to retrieve values from the specified table.  
String QueryString = "SELECT * from student";
    rs = statement.executeQuery(QueryString);
 while ( {
   System.out.println(rs.getInt(1) + "\t\t" 

+ rs.getString(2)+"\t\t"+ rs.getString(3)+"\n");
// close all the connections.
  catch (Exception ex) {
    System.out.println("Unable to connect to batabase.");


C:\Program Files\Java\jdk1.6.0_18\bin> java -Djdbc.drivers=com.mysql.jdbc.Driver JdbcUrl

Showing row of student table

2147483647      Ankit         1985-06-06

2147483647      Somesh     1984-05-05

Download Source code



Posted on: June 14, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null