Not able to load OracleDriver class in simple java class - jdbc

// DbConnector.java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class DbConnector {
public static void main(String[] argv) {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#172.17.119.108:1526:GESSDEV", "username",
"password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
I have a jdbc jar with name odbc14.jar
javac -classpath odbc14.jar; DbConnector.java
compilation successfull
java -classpath odbc14.jar; DbConnector
OUTPUT ::
-------- Oracle JDBC Connection Testing ------
Where is your Oracle JDBC Driver?
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at DbConnector.main(DbConnector.java:13)
Please help me out to resolve this
I have sorted out the above problem. Now I'm gettin new exception
-------- Oracle JDBC Connection Testing ------
Oracle JDBC Driver Registered!
Connection Failed! Check output console
java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:420)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(DriverManager.java:525)

The JDBC driver for use with ojdbc14.jar is oracle.jdbc.OracleDriver.
See: Loading JDBC Driver Class - ojdbc14.jar

Put your ojdbc14.jar in <jdk_home>\jre\lib\ext and ADD in CLASSPATH variable then you do not have to specify the classpath while compiling or running
If you are using java6 then oracle.jdbc.driver.OracleDriver is okay but it is deprecated. So it's better to use oracle.jdbc.OracleDriver

Just put the odbc14.jar this JAR in required JRE path like "C:\Program Files\Java\jdk1.7.0_65\jre\lib\ext".
This should solve your issue.

you should
try {
Class.forName("oracle.jdbc.OracleDriver");
}
and place the odbc14.jar in class path (if already not done)

Only you need to put into appsclassloader that is your classpath. I am not sure, why we need to put into jre/lib/ext

Related

source not found exception in derby.jar classes & Exception in thread "main" java.lang.NoSuchMethodError for derby jar

Kindly forgive the poor presentation and unethical coding lines if any
I'm trying to create my first jdbc class using embedded derby driver, I have created a maven project and added the following dependency
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
</dependency>
I created a derby database in eclipse database developer, the ping to the database was successful, created a table and inserted values into it.
When I run the below java program
import java.sql.*;
import org.apache.derby.jdbc.*;
public class SiteDao implements SiteDaoInterface {
public static void main(String[] args)
{
String url ="jdbc:derby:C://Users//shash//MyDB;create=true";
String username="xyz";
String password="xyz";
String query="select * from practise.abc";
//Class.forName("org.apache.derby.jdbc.ClientDriver")
try {
//Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
Connection con=DriverManager.getConnection(url,username,password);
Statement st= con.createStatement();
ResultSet rs=st.executeQuery(query);
int i,j;
while( rs.next())
{
i=rs.getInt(1);
j=rs.getInt(2);
System.out.println("data is"+i+","+j);
}
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I'm encountering the below error.
Exception in thread "main" java.lang.NoSuchMethodError: 'void org.apache.derby.iapi.services.i18n.MessageService.setFinder(org.apache.derby.iapi.services.i18n.BundleFinder)'
at org.apache.derby.impl.services.monitor.BaseMonitor.runWithState(Unknown Source)
at org.apache.derby.impl.services.monitor.FileMonitor.<init>(Unknown Source)
at org.apache.derby.iapi.services.monitor.Monitor.startMonitor(Unknown Source)
at org.apache.derby.iapi.jdbc.JDBCBoot.boot(Unknown Source)
at org.apache.derby.jdbc.EmbeddedDriver.boot(Unknown Source)
at org.apache.derby.jdbc.EmbeddedDriver.<clinit>(Unknown Source)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:377)
at com.smedia.dao.SiteDao.main(SiteDao.java:17)
I tried to go through the class mentioned in the error but encountered the below issue
I tried many solutions offered on internet but it didn't work
I'm using java 15.0.1 and even tried using derby-10.15.2.0 as suggested in some stackoverflow solutions but it didn't work (moreover there is no EmbeddedDriver class in 10.15.x.x version to my surprise)
Please let me know where am I ruining the code?

How to establish connection with HIVE using JDBC?

HOW TO CONFIGURE JDBC WITH HIVE
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class table {
private static String driverName = "org.apache.hadoop.hive.mysql.jdbc.Driver";
public static void main(String[] args) throws SQLException {
// Register driver and create driver instance
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:1000/default", "", "");
Statement stmt = con.createStatement();
stmt.executeQuery("CREATE DATABASE userdb");
// System.out.println(“Database userdb created successfully”);
con.close();
}
}
akshay#akshay:~$ javac table.java
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
akshay#akshay:~$ java table
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
java.lang.ClassNotFoundException: org.apache.hadoop.hive.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at table.main(table.java:14)
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:1000/default
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at table.main(table.java:20)
My hive-site.xml contains
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>MySQL JDBC driver class</description>
</property>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost/metastore?createDatabaseIfNotExist=true</value>
<description>metadata is stored in a MySQL server </description>
</property>
I have configured meta store of hive with MySQL. So what should my ConnectionURL and Drivername in JAVA connection code?
I am not getting where I am going wrong. Please provide solution for above problem.
For running hive queries using JDBC API's, you need to start your hiveserver2 first. Configure the thrift server port in your hive-site.xml file as shown below
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
<description>TCP port number to listen on, default 10000</description>
</property>
Start the hiveserver2 using the command
cd $HIVE_HOME/bin
./hiveserver2
Also you need to add the below dependencies to your project.
Hive-jdbc-*-standalone.jar
hive-jdb-*.jar
hive-metastore-*.jar
hive-service-*.jar
After that try running the program. You can refer to this blog for more information on step by step procedure to run hive queries using java programs.

could not make connection with hive?

i am making connection with hive using java code but i am getting below error -
log4j:WARN No appenders could be found for logger (org.apache.thrift.transport.TSaslTransport).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.sql.SQLException: Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:Got exception: org.apache.hadoop.security.AccessControlException Permission denied: user=anonymous, access=WRITE, inode="/":oodles:supergroup:drwxr-xr-x
at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkFsPermission(FSPermissionChecker.java:271)
at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:257)
at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:238)
at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(FSPermissionChecker.java:179)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.checkPermission(FSNamesystem.java:5904)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.checkPermission(FSNamesystem.java:5886)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.checkAncestorAccess(FSNamesystem.java:5860)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.mkdirsInternal(FSNamesystem.java:3793)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.mkdirsInt(FSNamesystem.java:3763)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.mkdirs(FSNamesystem.java:3737)
at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.mkdirs(NameNodeRpcServer.java:778)
at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.mkdirs(ClientNamenodeProtocolServerSideTranslatorPB.java:573)
at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod(ClientNamenodeProtocolProtos.java)
at org.apache.hadoop.ipc.ProtobufRpcEngine$Server$ProtoBufRpcInvoker.call(ProtobufRpcEngine.java:585)
at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:928)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2013)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2009)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1614)
at org.apache.hadoop.ipc.Server$Handler.run(Server.java:2007)
)
at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:275)
at com.oodles.example.HiveJdbcClient.main(HiveJdbcClient.java:23)
My Java code is below
package com.oodles.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class HiveJdbcClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
Statement stmt = con.createStatement();
String tableName = "testHiveDriverTable";
stmt.execute("drop table if exists " + tableName);
stmt.execute("create table " + tableName + " (key int, value string)");
System.out.println("success!");
stmt.close();
con.close();
}
}
and my other concern is that , whenever i make connection without starting hadoop services it gives error
log4j:WARN No appenders could be found for logger (org.apache.thrift.transport.TSaslTransport).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.sql.SQLException: Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:Got exception: java.net.ConnectException Call From oodles-Latitude-3540/127.0.1.1 to localhost:9000 failed on connection exception: java.net.ConnectException: Connection refused; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused)
at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:275)
at com.oodles.example.HiveJdbcClient.main(HiveJdbcClient.java:21)
later issue gets resolved if i start hadoop services ,so i want to ask is it mandatory to start hadoop services in order to make connection with hive?
Since you have not mentioned what Hive version you are using, but base on the Driver name and connection URl I am assuming you are using Hive 0.11 or above.
So in Hive 0.11 or above you need to mention a username in the Connection URL:
DriverManager.getConnection("jdbc:hive2://localhost:10000/default", <user_name>, "")
NOTE: This user should have read+write permissions in HDFS.
Regarding you second query:
I am quite sure that Hadoop services are not required just for connection. I have never tried that.
Its my assumption, since we need to mention a database in the connection URL , which is a directory in HDFS. So it might need NAMENODE service to check the existence of that directory.
Hope it helps...!!!

JDBC simple program

This is my simple JDBC program but I am not able to establish connection. The class path is set up with ojdbc6.jar.
package Demo;
import java.sql.*;
public class JdbcDemo {
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
System.out.println("hi");
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("hi1");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:system","scott","tiger");
}
}
This is the output and the exception I am getting:
Exception in thread "main" java.sql.SQLRecoverableException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:101)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:173)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:229)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:458)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:411)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:490)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:202)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:465)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Demo.JdbcDemo.main(JdbcDemo.java:13)
Please check the following
Have you started Oracle TNS Listener, if not start by lsnrctl utility.
Have you put the correct port
Is your hostname correct in the database server?
The issue could be any one of the above.

Oracle ArrayIndexOutOfBoundsException: -1

For special needs, I use the unzipped jar format ojdbc6.jar.
The code source of my connection to the database oracle is :
import java.sql.DriverManager;
import java.sql.SQLException;
....
try
{
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:#x.x.x.x:1521:dev";
String login = "dev";
String password = "dev";
Class.forName(driver);
DriverManager.getConnection(url, login, password); // Error is generated by this line
}
catch (SQLException e)
{
System.err.println("error");
}
.....
When i launch the program, i have this exception :
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
at oracle.jdbc.driver.T4CTTIoauthenticate.setSessionFields(T4CTTIoauthenticate.java:999)
at oracle.jdbc.driver.T4CTTIoauthenticate.<init>(T4CTTIoauthenticate.java:235)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:370)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
.......
I use
ojdbc6.jar (11.2.0.2)
JRE 1.6.0_24
Oracle 11g
The database is accessible and the data connection are correct.
I was looking into the same error above for a RMI server issue. I added -Djava.security.policy=$RMIPATH"/policy.txt" to my runtime script and created a policy.txt file that stated.
grant {
permission java.security.AllPermission;
};
I hate disabling security but it works... My runtime is 1.8.31

Resources