Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JDBC ConnectionUrl 
 

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

 

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

                         

» View all related tutorials
Related Tags: java sql mysql c database api ide jdbc data batch application io get vi exec state int bat this id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

bothers

Posted by venki on Thursday, 12.4.08 @ 23:54pm | #82418

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.