Java Plan To Connect Oracle Database Amongst Event - Jdbc Tutorial Sample Code
How to connect to Oracle database from Java Program using JDBC API is mutual postulate for many Java programmer, though at that topographic point are lot of framework available which has simplified JDBC evolution e.g hibernate, Spring JdbcTempate as well as many more, but creating Java plan to connect to oracle database from manifestly sometime Java is however close tardily as well as quickest method for testing as well as debugging database connectivity. Database connectedness plan is too a mutual Java programming exercise inward many Java programming courses on school, colleges as well as diverse grooming institutes. We accept been exploring some advanced concepts as well as best practices on JDBC inward my previous articles similar Why should yous purpose PreparedStatement inward Java as well as 4 JDBC functioning tips for Java application , which yous may similar if yous are on to a greater extent than advanced level. This uncomplicated coffee plan is intended for beginners inward Java which accept only started learning JDBC API.
If yous similar to read tutorials on database as well as hence yous may honor difference betwixt truncate as well as delete , 10 event of SQL SELECT queries as well as how to handle database transaction useful as well as interesting.
How to connect Oracle Database from Java Program using JDBC - code example
running this Java program to connect Oracle database brand certain your Oracle database server is running as well as yous accept JDBC sparse driver inward your classpath similar ojdbc6.jar or ojdbc6_g.jar.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
* Simple Java Program to connect Oracle database past times using Oracle JDBC sparse driver
* Make certain yous accept Oracle JDBC sparse driver inward your classpath earlier running this program
* @author
*/
public class OracleJdbcExample {
public static void main(String args[]) throws SQLException {
//URL of Oracle database server
String url = "jdbc:oracle:thin:@localhost:1632:DEVROOT32";
//properties for creating connectedness to Oracle database
Properties props = new Properties();
props.setProperty("user", "scott");
props.setProperty("password", "tiger");
//creating connectedness to Oracle database using JDBC
Connection conn = DriverManager.getConnection(url,props);
String sql ="select sysdate every bit current_day from dual";
//creating PreparedStatement object to execute query
PreparedStatement preStatement = conn.prepareStatement(sql);
ResultSet upshot = preStatement.executeQuery();
while(result.next()){
System.out.println("Current Date from Oracle : " + result.getString("current_day"));
}
System.out.println("done");
}
}
Output:
Current Date from Oracle : 2012-04-12 17:13:49
done
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
* Simple Java Program to connect Oracle database past times using Oracle JDBC sparse driver
* Make certain yous accept Oracle JDBC sparse driver inward your classpath earlier running this program
* @author
*/
public class OracleJdbcExample {
public static void main(String args[]) throws SQLException {
//URL of Oracle database server
String url = "jdbc:oracle:thin:@localhost:1632:DEVROOT32";
//properties for creating connectedness to Oracle database
Properties props = new Properties();
props.setProperty("user", "scott");
props.setProperty("password", "tiger");
//creating connectedness to Oracle database using JDBC
Connection conn = DriverManager.getConnection(url,props);
String sql ="select sysdate every bit current_day from dual";
//creating PreparedStatement object to execute query
PreparedStatement preStatement = conn.prepareStatement(sql);
ResultSet upshot = preStatement.executeQuery();
while(result.next()){
System.out.println("Current Date from Oracle : " + result.getString("current_day"));
}
System.out.println("done");
}
}
Output:
Current Date from Oracle : 2012-04-12 17:13:49
done
Error as well as Exception land connecting Oracle Database from Java Program:
1) Invalid Username as well as Password
Exception inward thread "main" java.sql.SQLException: ORA-01017: invalid username/password; logon denied
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:452)
This Error comes when username as well as password provided to Java plan connecting to Oracle database is non correct.
2)No suitable driver found
Exception inward thread "main" java.sql.SQLException: No suitable driver establish for
jdbc:oracle:thin:@localhost:1632:DEVROOT32
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
This Error comes when JDBC sparse driver for relevant Oracle version is non inward Classpath. e.g. ojdbc6.jar or ojdbc6_g.jar (compiled amongst javac -g amongst debug information) for Oracle 11g.
Also land reading information from ResultSet ensure that yous are using proper column index to avoid Exception inward thread "main" java.sql.SQLException: Invalid column index which comes when yous purpose invalid index similar naught to access ResultSet on diverse acquire as well as laid method.
In this Java plan Example nosotros accept seen how to connect to Oracle database using JDBC sparse driver , amongst sparse driver its much easier to connect to oracle database every bit yous don’t postulate to do data-sources similar yous do if yous purpose JDBC ODBC Driver. Let me know if yous expression upwards whatsoever number land connecting to Oracle database shape Java Program. Another worth noting signal is connecting Oracle database using SSL from Java Program, which may come across inward about other coffee tutorial.
Further Learning
JSP, Servlets as well as JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 as well as 2
Java Program to contrary String using recursion
0 Response to "Java Plan To Connect Oracle Database Amongst Event - Jdbc Tutorial Sample Code"
Post a Comment