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);}

}
}  

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu