JDBC Database URL

JDBC Database URL


Posted in : Core Java Posted on : October 15, 2010 at 5:45 PM Comments : [ 0 ]

This section contains the detail about the basic operators in java.

JDBC Database URL

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

Way-2: 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.geString(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

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics