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
|