Reputation:
I am super newer in java language. Now i am getting started java.
I am keen interst know about how to connect java with back end.
For that I have been installed oracle 11g express edition and sql developer.
First of all i just confused here, is these two tools are (oracle 11g express edition and sql developer.) enough for connect java with back end.?
And i am using jdk1.8.0_25
and eclipse editor.
If yes, means, what are the further process to achieve?
Already i know little bit about php, i did something connecting database, retrive the data from database..
So what is my goal is, what is the best way to connect backend with java?
Can anyone java expert explain step step by process?
Upvotes: 0
Views: 2956
Reputation: 598
Step 1 : Download Oracle JDBC Drivers
You can download Oracle JDBC drivers from here. Choose the version appropriate for your database version. In this example, I use the Oracle 11g JDBC driver since I connect to Oracle 11g database. There are two versions available for Oracle 11g, ojdbc5.jar (for JDK 1.5) and ojdbc6.jar (for JDK 1.6). Use appropriate version for your Java installation (Oracle now requires online registration for downloading drivers). I use ojdbc6.jar for this tutorial.
Step 2 : Java Program to Connect to Oracle
The following Java program uses Oracle JDBC driver to connect to a running Oracle database instance. You can use this program on any Oracle database as this example uses Oracle’s built-in dummy table DUAL for fetching system date. DUAL enables us to get values such as system date using a normal SQL query.
// Example Java Program - Oracle Database Connectivity
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OracleSample {
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:XE";
public static final String DBUSER = "system";
public static final String DBPASS = "manager";
public static void main(String[] args) throws SQLException {
// Load Oracle JDBC Driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to Oracle Database
Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
Statement statement = con.createStatement();
// Execute a SELECT query on Oracle Dummy DUAL Table. Useful for retrieving system values
// Enables us to retrieve values as if querying from a table
ResultSet rs = statement.executeQuery("SELECT SYSDATE FROM DUAL");
if (rs.next()) {
Date currentDate = rs.getDate(1); // get first column returned
System.out.println("Current Date from Oracle is : "+currentDate);
}
rs.close();
statement.close();
con.close();
}
}
Before you run the program ensure that you change the values for DBURL, DBUSER and DBPASS. DBURL is of the form, jdbc:oracle:thin:@machinename:1521:databasename Replace machinename with the name of the machine where oracle is running and replace databasename with service name of the database instance. See this page for more details on JDBC API.
Step 3 : Add ojdbc.jar to Classpath
In order to compile or run the above program, you need to add ojdbc.jar to the classpath of your program. If you are using IDE such as NetBeans or Eclipse, you can add ojdbc.jar as a dependent library and NetBeans will automatically add it to classpath.
If you are running the above program from command line, copy ojdbc.jar to the folder where the above Java program is located and then compile the file using the following command (this adds ojdbc.jar to classpath), javac -classpath ./ojdbc6.jar OracleSample.java Run the Java program using the following command (ojdbc.jar is added to classpath), java -classpath "./ojdbc6.jar;." OracleSample Note that when you are running OracleSample, you need both the JDCB jar file and the current folder in the classpath.
Upvotes: 1