The Artima Developer Community
Sponsored Link

Java Buzz Forum
Jdbc Connction Steps with Examples

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Jdbc Connction Steps with Examples Posted: Mar 22, 2015 2:48 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Jdbc Connction Steps with Examples
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
Example to connect to oracle database:

Connecting to java application to oracle database.you have to follow 5 steps :
1.load the driver class,
2.create the connection object,
3.create the statement object
4.execute query Connect
5.Connection close

oracle database connectivity steps:

Driver class:The oracle database driver class is oracle.jdbc.driver.OracleDriver.

Connection URl:The oracle database connection URL is jdbc:oracle:thin:@localhost:1521:xe.
here jdbc is API,oracle is the database, thin is the driver,local host is server on which oracle is running,or we can use IP address,1521 is port number and
and XE is the Oracle service name.

User name:The oracle database default userName is system.

Password:Password is given by the user at the time of installing the oracle database. In this example, we are going to use system123 as the password.

import java.sql.*;
class ConnecttoOracle{
public static void main(String args[]){
try{
// load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");

//create  the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");

// create the statement object
Statement stmt=con.createStatement();

// execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));

// close the connection object
con.close();

}
catch(Exception e){
System.out.println(e);}

}
}


Example to connect to Mysql databaase:

Connecting to java application to mysql database.you have to follow 5 steps :
1.load the driver class,
2.create the connection object,
3.create the statement object
4.execute queryConnect
5.Connection close

Mysql database connectivity steps:

Driver class:The mysql database driver class is com.mysql.jdbc.Driver.

Connection URl:The mysql database connection URL is jdbc:mysql://localhost:3306/test.
here jdbc is API,mysql is database,local host is server on which mysql is running,or we can use IP address,3306 is port number and test is database name.
we can use any database name here.

Username:The mysql database default userName is root.

Password:Password is given by the user at the time of installing the mysql database. In this example, we are going to use root123 as the password.

import java.sql.*;
class ConnecttoMysql{
public static void main(String args[]){
try{
class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/database name","root","root123");

//here root is username and root123 is password

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next())
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));

con.close();

}
catch(Exception e){
System.out.println(e);}

}
}  

Read: Jdbc Connction Steps with Examples

Topic: One Year After Java 8’s Release, IDEs and Compilers are not Fully Ready Yet Previous Topic   Next Topic Topic: Conquer continuous delivery with GitHub and Jenkins

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use