Home Tutorial Java Jdbc JDBC Database URLs

 
 

JDBC Database URLs
Posted on: June 14, 2010 at 12:00 AM
Advertisement
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 :

        jdbc:subprotocol:subname  

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.

URL'S EXAMPLE :

  • JDBC ODBC Bridge driver:

     jdbc:odbc:dsn_name;UID=your_userid;PWD=your_password

  • Oracle JDBC Driver(TYPE 4) :

   jdbc:oracle:thin:@machine_name:port_number:instance_name 

  • MySql Connector JDBC driver

    jdbc:mysql://host_name:port/dbname

Note: You can Download Mysql Connector From the site "http://dev.mysql.com/downloads/connector/j/". 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 :

   Class.forName("com.mysql.jdbc.Driver").newInstance();

 

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.

Example: JdbcUrl.java

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://192.168.10.13:3306/ankdb";

// 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,

          "root""root");
            
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 (rs.next()) {
   System.out.println(rs.getInt(1) + "\t\t" 

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

Output:

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

Advertisement

Related Tags for JDBC Database URLs :


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.

Posted on: June 14, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com