Recent Posts

Sunday 10 August 2014

03:21

Hello guys,

Today i am here to give you the basic idea about how to connect your java application to oracle database.
Even you can connect to any database just by manipulating these codes to other ..
Its very easy because our java provides a huge library support .So in java library, packages are available for most of the Databases .

So lets write some codes for Oracle JDBC.

import java.sql.*;

// ~ you can modify these codes according to your needs
// ~ I'm here providing you the basic idea how Oracle/any databasecan 
//be connected to java apps
@@ author TheRootCoder
public class OracleDbConn {

Connection c;  
Statement s; // ~ all the functions used for queries are defined inside this class
 

public OracleDbConn()
{ try { 
// ~ load and register the Jdbc drivers
Class.forName("oracle.jdbc.driver.OracleDriver");
c= DriverManager.getConnection("jdbc:oracle:thin:@localhost:port_no:db_name","db_username","db_password");
 
s= c.createStatement();
}catch(Exception e)
{e.printStackTrace();
}
}

// ~ you can call this function any where in your project to use queries like SELECT etc
public ResultSet execute (String r) throws Exception
{return(s.executeQuery(r));

// ~ it return the values based upon your query 
// ~ if you are using SELECT then all the records will be access by object of ResultSet class 
}

// ~ you can call this fucntion anywhere in your project to use queries like DELETE,UPDATE
//,ALTER etc
public int update(String t) throws Exception
{
 return(s.executeUpdate(t));
// ~ it return value>=1 if query is successfully executed
}
}

If you like this article then share it with your friends to support me to write some useful articles.
Like our  facebook page Click here

0 comments:

Post a Comment