JDBC ConnectionUrl

In this Tutorial we want to describe you a code that helps you to understand Jdbc Connection Url.


 

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 ConnectionUrl

     

The JDBC Connection Url is the mean of connecting  a front end application made in java platform to backend database. An object of Connection enables your application to connect database. This all requires a SQL queries to execute and return the result  over that connection object. Simlararly,In this Tutorial we want to describe you an example that helps you to understand JDBC Connection url. The code describe you the JDBC Connection between url and a database.

Understand with Example

The package java.sql.* is to be imported, that include all the set of defined classes provides you a network interface between url and database. Here we have a class JdbcConnectionUrl. Inside this class, we have a main method( ) contains a declared list of objects and variable. Now you can see the uses of declared variable in the given below code -

   Loading Database Driver:  loading of a driver class that invokes Class.forName( ) with the Driver class name passed as an argument. Once the driver is loaded ,you can connect  the Java front end application to the backend database. In case the exception occurred  in loading a database inside the try block. The catch block handle the exception and println print the exception.

  Driver Manager.getConnection ( ) :This method return you a connection object that built your path between url and database.

  The Println print the connection url from the variable string url and db.

   st = con.createconnection( ) :This method return you an sql object. Once  connection is established ,your front-end application is able to interact with backend database. A connection object enables you to send and execute SQL Statement to a backend database.

   rs = st.executequery(sql) : This method return you result set object that accept sql  query as parameter.The value retrieved from it is assigned in  result set rs.The retrieved  table row  contain a element in sequence.

  next( ) :The next method is used to retrieve successively element through the rows from a result set.

   Finally the JDBC Connection url is built and println print the url and db,first name and last name from the result set obtained from database.

 

JdbcConnectionUrl .java

import java.sql.*;
public class JdbcConnectionUrl {
    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);
            con = DriverManager.getConnection(url + db, user, pass);
            System.out.println("Connection url : "+url + db);
            
            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 url : jdbc:mysql://localhost:3306/komal
First Name  	Last Name
girish   	tewari
mahendra   	singh
komal   	singh
yogesh   	pandey
pinku   	tripathi
deepak   	tripathi

Download code

Advertisements

Share on Google+Share on Google+

JDBC ConnectionUrl

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

Advertisements

 

Discuss: JDBC ConnectionUrl   View All Comments

Post your Comment


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

Ads

 

Ads