Error in connecting to AS400 DB2 server JDBC - java

I am trying to connect to the AS400 DB2 database using the JDBC in a java program but I am unable to connect as it is giving a SQLException stating No suitable driver found for ​jdbc:as400://192.168.1.11
Whereas I added No suitable driver found for ​jdbc:as400://192.168.1.11
I added the following libraries to my project: db2jcc_licence_cu.jar, db2jcc4.jar, jt400-6.4.jar.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class ReadAllTables {
public static void main(String args[]) throws Exception {
//Class.forName ("com.ibm.as400.access.AS400JDBCDriver");
DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
// Enable logging
// DriverManager.setLogStream(System.err);
System.out.println("Getting Connection");
Connection c = DriverManager.getConnection ("​jdbc:as400://ipaddress","username","password");
DatabaseMetaData md = c.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
System.out.println(rs.getString(3));
}
}
}
This is what the error I am getting:
Getting Connection
Exception in thread "main" java.sql.SQLException: No suitable driver found for ​jdbc:as400://192.168.1.11
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.test.ReadAllTables.main(ReadAllTables.java:18)
Thanks in advance

You must have jt400.jar in your classpath for the AS/400 JDBC driver to work.
Version 7.10 is in Maven or download the latest from https://sourceforge.net/projects/jt400/ or copy it from IFS. The native CC driver is only used when running locally on the AS/400.
Given your current list of jars it sounds like your CLASSPATH variable is incorrect (or if you use a runnable jar, your Class-Path line in the manifest file is wrong). If you launch with RUNJVA the mechanism is different.

Related

JDBC driver error and cannot connect to MySQL [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
What is a classpath and how do I set it?
(10 answers)
Closed 5 months ago.
I keep getting the error messages when I run my code attempting to connect to a MySQL server
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:375)
This is my code. I've made sure to add connector/J to the classpath and still no change.
import java.sql.*;
public class App {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/vehicle", "root", "root"); // For MySQL only
vehicle focus = new vehicle("Ford", "Focus", 200, 2004, 3000);
System.out.println(focus.getMake());
}
}
First of all, loading the JDBC driver using Class.forName is not necessary since JDBC 4.0 which was made available in 2006 (the driver registers its implementation as service provider and will be loaded by the ServiceLoader).
Just remove this line and it should work. If you need the class name, its a good idea to consult the documentation of Connector/J. Take care to read the docs for the correct driver version because the class name changed (currently it's com.mysql.cj.jdbc.Driver).
Update
As Mark Rotteveel pointed out, Class.forName("com.mysql.jdbc.Driver") will not lead to a ClassNotFoundException when used with newer version of Connector/J. At least the newest driver produces a warning:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new
driver class is `com.mysql.cj.jdbc.Driver'. The driver is
automatically registered via the SPI and manual loading of the driver
class is generally unnecessary.
Knowing this, removing Class.forName won't help since the exception simply means, that the driver is not on the class path.

Unable to connect to Oracle 11g using jdbc

I'm trying to connect to Oracle 11g using jdbc as below but its giving me error. I had downloaded and kept jars in eclipse (ojdbc6dms.jar and ojdbc6dms_g.jar), but still its giving error
public static Connection getDatabaseConnection() throws SQLException,
InstantiationException, IllegalAccessException,
ClassNotFoundException {
Connection con = null;
// initialising drive
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// load driver
con = DriverManager.getConnection(
"jdbc:oracle:thin:#10.16.52.79:1521:TST", "rep",
"Appe");
String dbName = con.getMetaData().getDatabaseProductName();
System.out.println(" Conected to DB " + dbName);
return con;
} // insertRecordsToDB
Exception in thread "main" java.lang.NoClassDefFoundError: oracle/dms/console/DMSConsole
at oracle.jdbc.driver.DMSFactory.<clinit>(DMSFactory.java:45)
at oracle.jdbc.driver.PhysicalConnection.createDMSSensors(PhysicalConnection.java:4203)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:629)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:218)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:29)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:538)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at
Just wondering in which jar has class oracle/dms/console/DMSConsole . Because when i checked those jars, this class is not available
The JDBC driver download page for Oracle 11g has the file ojdbc6dms.jar which contains oracle.jdbc.driver.DMSFactory.
EDIT: As per #JavaGeek's comment below, this jar does not actually contain the DMSConsole class. The required class is available in the Oracle Server installation, in the location $ORACLE_HOME/oc4j/lib/dms.jar. Server downloads are also available at Oracle TechNetwork

Connecting to remote Mapr Hive via JDBC

This question is similar, but not the same, as Hive JDBC getConnection does not return . Yet this is about a remote connection. Also the metastore is present in the directory in which the hiveserver2 was started.
We have a running mapr cluster on a remote machine. I would like to connect to Hive on this cluster using Java JDBC.
Hence we started the hive server:
/opt/mapr/hive/hive-0.11/bin/hiveserver2
The output of the server process does not contain any error messages. It listens on port 10000 as reported by netstat.
I try to connect to the server as described in https://cwiki.apache.org/confluence/display/Hive/HiveClient, thereby replacing localhost by the server name where the hiveserver2 is running:
Connection con =
DriverManager.getConnection("jdbc:hive://myserver.example.com:10000/default", "", "");
Yet the program hangs exactly at this statement. It does not seem to get a connection.
Possibly I need to supply a username and password?
Initially I had used the driver org.apache.hadoop.hive.jdbc.HiveDriver.
Yet it seems like I should be using the driver org.apache.hive.jdbc.HiveDriver if the hive2 server is running. Now I am getting the following Exception:
Exception in thread "main" java.sql.SQLException: Could not establish connection to jdbc:hive2://myserver.example.com:10000/default: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null)
at org.apache.hive.jdbc.HiveConnection.openSession(HiveConnection.java:246)
at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:132)
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at HiveJdbcClient.main(HiveJdbcClient.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null)
at org.apache.thrift.TApplicationException.read(TApplicationException.java:108)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:71)
at org.apache.hive.service.cli.thrift.TCLIService$Client.recv_OpenSession(TCLIService.java:144)
at org.apache.hive.service.cli.thrift.TCLIService$Client.OpenSession(TCLIService.java:131)
at org.apache.hive.jdbc.HiveConnection.openSession(HiveConnection.java:237)
... 10 more
I had the same problem and was able to get around it by adding the correct dependency to my pom.xml file. I was getting the latest apache release of hive from maven central and switched to using the cdh4 release from the cloudera repo. So, what you are seeing may be a symptom of having the wrong hive-jdbc dependency. Here's the maven snippet I added to my pom file:
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
...
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>0.10.0-cdh4.3.2</version>
</dependency>
Here's a link about the cloudera repo.
Also, adding the ";auth=noSasl" to the URL made my application hang so I removed it.
I think you need to specify the username. Also itshould be hive2 not hive since you are using hiveserver2.
Try modifying your connection url:
Connection con =
DriverManager.getConnection("jdbc:hive2://myserver.example.com:10000/default", "<user>", "");
Its given in the link Hive2
Hope this helps...!!!
I also had same problem.
Please check if server is reachable on the port 10000 from the client (server and port are enabled no firewall is restricting) also check hiveserver is up and running. if yes then it should work.
following code work for me for mapr hive.
if you have any query related mapr, please refer answers.mapr.com, this contain most of the information which you might be requiring.
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import org.apache.log4j.Logger;
import java.io.*;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.*;
import org.apache.hadoop.io.SequenceFile.Writer;
import org.apache.hadoop.io.*;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.*;
public class HiveJdbcClient {
//private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
/**
* #param args
* #throws SQLException
**/
private static Logger mLogger = Logger.getLogger(HiveJdbcClient.class);
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
HiveJdbcClient myJob = new HiveJdbcClient();
myJob.execute();
}
public void execute() throws SQLException {
//mLogger.info("Start HiveJob");
System.out.println("Start HiveJob");
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive://myserver:10000/default", "", "");
Statement stmt = con.createStatement();
String sql = "SHOW TABLES";
//String tableName = "testHiveDriverTable";
// ResultSet res1 = stmt.executeQuery("create table " + tableName + " (key int, value string)");
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
//mLogger.info("HiveJob executed!");
System.out.println("HiveJob executed!");
}
}
You can use the beeline client to connect to hive using JDBC.
It would be some thing like: beeline !connect jdbc:hive2://localhost:10000
check the link:
http://dwbitechguru.blogspot.ca/2014/11/how-to-connect-to-hadoop-hive-using.html
In my case adding the: ;auth=noSasl to the JDBC connect string solved the endless waiting for the connection !
jdbc:hive2://server:10000/default;auth=noSasl
You should get hive-service-X.XX.X-cdhX.X.X.jar here :https://repository.cloudera.com/artifactory/cloudera-repos/org/apache/hive/;
It works fine to me.

MySQL jdbc driver and Eclipse problems

I am having problems loading the MySQL JDBC driver. I have tried everything mentioned here, but am still running into problems.
The error I get is this:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Main.main(Main.java:12)
My code is as follows:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Main {
public static void main(String args[]) throws Exception {
//accessing the driver
Class.forName("com.mysql.jdbc.driver");
// creating variable to pass the connection information into
Connection dbcon = DriverManager.getConnection(
"jdbc:mysql//localhost:3306/test", "root", "root");
PreparedStatement statement = dbcon
.prepareStatement("select * from names");
ResultSet result = statement.executeQuery();
while (result.next()) {
System.out.println(result.getString(2));
}
}
}
As you can see above I have placed the driver in the lib folder and I have also put it into the Apache tomcat lib folder, because that was suggested as well.
Thank you for any help you can give me.
Andrew
The class name inside the MySql connectorJ JAR file is Driver. so you need to change
Class.forName("com.mysql.jdbc.driver");
to
Class.forName("com.mysql.jdbc.Driver");
It tells you the problem on the first line of your stacktrace:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
The driver name is com.mysql.jdbc.Driver (capital D).
problem in loading the mysql connection library.please check the proper mysql library is load successfully.because when library load successfully then the library package display all the external loaded library.

getting class not found exception

Hi friends i am facing the following error while trying to connect to a database through java code:
Exception in thread "Main Thread" java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.example.model.Driver.main(Driver.java:13)
My java code is:
package com.example.model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Driver {
public static void main(String args[]) throws ClassNotFoundException,
SQLException {
Class.forName("oracle.jdbc.OracleDriver");
// or you can use:
// DriverManager.registerDriver(
// new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:#127.0.0.1:1521:ORCL", "scott", "tiger");
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select * from customer");
while (rset.next())
System.out.println(rset.getString(1));
rset.close();
stmt.close();
conn.close();
}
}
As per i came to know it could be due to class path issue but don't know how to resolve it.
I set my class path to
C:\bea\user_project\workspace\wlserver_10.3\server\ext\jdbc\oracle\11g\ojdbc5.jar;
I am using weblogic 10.3 workspace and weblogic 10.3 server.
The java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver definitely means that the mentioned class is not on the class path. And because ojdbc5.jar has it, the whole question is: "how did you set your classpath"?
Here is what I get on my machine using your code (using the default package):
$ cat > Driver.java
...
$ javac Driver.java
$ java -cp /home/pascal/opt/Oracle/Middleware/wlserver_10.3/server/ext/jdbc/oracle/11g/ojdbc5.jar:. Driver
Exception in thread "main" java.sql.SQLException: The Network Adapter could not establish the connection
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:131)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:197)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:525)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:508)
at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:203)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Driver.main(Driver.java:15)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:328)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:421)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:634)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:208)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
... 7 more
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.(Socket.java:375)
at java.net.Socket.(Socket.java:189)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:127)
at oracle.net.nt.ConnOption.connect(ConnOption.java:126)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:306)
... 12 more
The driver is found (I get an exception because I'm not running any Oracle server but this is another story).
I will try to put it in the weblogic's lib folder... and restart the server, so you will be sure that it is a classpath problem...
You should download the oracle drivers from oracle and put the .jar file in the project's CLASSPATH...
You can download it at Oracle's drivers download page

Categories

Resources