Using my below-given java code, I am trying to connect to DB2 database. Till yesterday, I was able to connect to DB2 database but suddenly today, I started to face below give the exception -
java.lang.ClassNotFoundException: com/ibm/db2/jcc/DB2Driver;
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.rolta.ntpc.Util.JdbcDB2Connection.getConnection(JdbcDB2Connection.java:37)
at com.rolta.ntpc.Util.ReadDataFromXLSFile.truncateTable(ReadDataFromXLSFile.java:155)
at com.rolta.ntpc.test.TestNTPCDataTransfer.storeData(TestNTPCDataTransfer.java:93)
at com.rolta.ntpc.test.TestNTPCDataTransfer.transferDataToDB2(TestNTPCDataTransfer.java:40)
at com.rolta.ntpc.test.TestNTPCDataTransfer.main(TestNTPCDataTransfer.java:32)
Property File having below-given information -
DB2Driver=com.ibm.db2.jcc.DB2Driver;
DB2ConnectionURL="jdbc:db2://100.38.0.172:50000/DREAMS";
DB2UserName="dbr";
DB2Password="Welcome#123";
My DB2 database version is 11.1 and my java code to connect to DB2 is -
public static Connection getConnection() {
Properties prop = LoadProperties.getProperties();
logger.info("Properties From Property File : ");
logger.info("DB2Driver : "+prop.getProperty("DB2Driver"));
logger.info("DB2ConnectionURL : "+prop.getProperty("DB2ConnectionURL"));
logger.info("DB2UserName : "+prop.getProperty("DB2UserName"));
logger.info("DB2Password : "+prop.getProperty("DB2Password"));
Connection connection = null;
try {
Class.forName(prop.getProperty("DB2Driver"));
connection = DriverManager.getConnection(prop.getProperty("DB2ConnectionURL"),
prop.getProperty("DB2UserName"),
prop.getProperty("DB2Password"));
if(connection != null) {
System.out.println("Connected successfully.");
} else {
System.out.println("Colud not Connect successfully.");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
I have below-given jar files in my project.
Can anyone please help me in knowing why this exception is coming and what is the solution?
Per comment thread, it was a change to the properties file .
The correct class name should be com.ibm.db2.jcc.DB2Driver
Related
I've searched through similar questions and didn't find a solution that worked for me. I'm running following in Netbeans > servlet:
public Connection getConnection()
{
Connection result = null;
BasicDataSource dataSource = getDataSource();
try {
result = dataSource.getConnection();
} catch (SQLException ex) {
Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
}
catch (Throwable e)
{
e.printStackTrace();
}
return result;
}
It works fine when executed in a Java project. Then I added this project's jar as a library to a servlet project in Netbeans - it throws exception:
"java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://127.0.0.1:3306/config'"
It looks like this "class ''" thing should point to a class name but where do I get it?
fixed that. had to put drivers into glassfish/lib directory
Hello im trying to connect to a mysql database using JDBC my code is below.I get an error as such No suitable driver found.Searching around I found that the usual error is syntax or missing the jar file from the class path.I tried both of these solutions and dont know what to do next it wont connect.Also to manage the databases I have WAMP and mySQL workbench installed not sure if its related.
package test.jdbc;
import java.sql.*;
public class jdbctester {
public static void main(String[] args)
{
try
{
Connection myconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/voting","root","Vanquish123");
Statement myStmt=myconn.createStatement();
ResultSet myRs=myStmt.executeQuery("select * from electoral");
/*
while(myRs.next())
{
System.out.println(myRs.getString("state")+","+myRs.getString("perDem"));
}
*/
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
}
Try this:
Class.forName("fully qualified driver class name");
Java Class.forName, JDBC connection loading driver
this post states that you should not need it but it will not hurt you to try.
you have to add "com.mysql.jdbc_5.1.5.jar" in to your project build path... go to project property>build Path> library> add external jar and add jar file.
Connection conn = null;
try {
// Register JDBC driver
Class.forName(DRIVER).newInstance();
// Open a connection
conn = DriverManager.getConnection(Local_URL + , USERNAME, PASSWORD);
System.out.println("Connected Database Successfully...\n\n");
} catch (Exception se) {
throw new AppException("Failed to create Local Database connection", se);
}
return conn;
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 7 years ago.
I have searched the internet trying to find solutions on using java and JDBC to connect to MySQL. Unfortunately I have found multiple answers and none that solved my problem.
I downloaded the JDBC from MySQL, and unzipped the file to find the .jar. I placed the .jar in my C:/Program Files (X86)/Java/JDK.../JRE/lib/ext folder. I set my environmental variable classpath (maybe CLASSPATH, ClassPath ??) to the following:
%CLASSPATH%;.;C:\Program Files (x86)\Java\jdk1.8.0_65\jre\lib\ext
I use the script I composed based on all the different solutions I have seen to get this:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySql {
public static void main(String[] args)
{
Connection con = getConnection();
if (con != null) {
System.out.println("Connection Made");
}
else {
System.out.println("Connection not made");
}
}
private static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "java", "java");
System.out.println("Connection Made");
conn.close();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
System.exit(0);
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
System.exit(0);
}
return con;
}
}
It compiles with javac MySql.java. Then when it runs (java MySql) I get com.mysql.jdbc. I have read that I don't need to register the driver, but when I remove Class.forName all I get is 'cannot find JDBC driver' error.
I can't narrow down my problem to either:
1). Classpath not setup correctly.
2). Improper java connection code.
3). Unable to locate MySQL server.
Any help would be appreciated.
Edit -
I placed the .jar file onto Desktop for testing purposes. Changed system classpath variable:
%CLASSPATH%;.;C:\User\User\Desktop\mysql-connector-java-5.1.38-bin.jar
Then when I add the trace for the error statement I get:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass<Unkown Source>
at java.lang.ClassLoader.loadclass (Unknown source)
at sun.misc.Launcher$AppClassLoader.loadclass(Unkown Source)
etc.
etc.
edit 2 - I have spent two days using Connect Java to a MySQL database as a resource and none of the instructions I have followed have solved my problem.
Instead of
System.out.println(e.getMessage());
Do this
e.printStackTrace();
You will see that the exception is:
java.lang.ClassNotFoundException: com.mysql.jdbc
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 MySql.getConnection(MySql.java:24)
at MySql.main(MySql.java:10)
Remove the Class.forName. You might get an access denied or some other error but it will solve the ClassNotFoundException. Here is the final edited version that should work:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySql {
public static void main(String[] args)
{
Connection con = getConnection();
if (con != null) {
System.out.println("Connection Made");
}
else {
System.out.println("Connection not made");
}
}
private static Connection getConnection() {
Connection con = null;
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "java", "java");
System.out.println("Connection Made");
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(0);
}
return con;
}
}
If you need Class.forName() you have to use correct class:
Class.forName("com.mysql.jdbc.Driver");
But with JDBC 4 that has become no longer needed.
Create full Exception stack traces and use the result to search here - most common errors have been solved here before.
NoSuitableDriverFound is a very strong indication your mysql-connector.jar (not the .zip....) is missing from your classpath when you run your code.
You could try like this:
Run java -cp .;C:\User\User\Desktop\mysql-connector-java-5.1.38-bin.jar MySql
I think you must add the path including the jar file name of the driver in your classpath. Like so:
%CLASSPATH%;.;C:\Program Files (x86)\Java\jdk1.8.0_65\jre\lib\ext\mysql-connector-java-xxx.jar
If your Java is version 6 or above you doesn't need Class.formName(...) code.
And for ensure that everything works, compile and execute your code in the follow way.
To compile:
java -cp PATH_TO_DRIVER; YOUR_CLASS.java
To execute:
java -cp PATH_TO_DRIVER; YOUR_CLASS
Change YOUR_CLASS to the name of your class and PATH_TO_DRIVER to the path where you download the MySQL driver.
Hope it helps!
Try this code!
public static void main(String[] argv) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/yourSCHEMAname,"login", "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!");
}
}
}
Cassandra Database has been installed on a server machine with following configurations :
cqlsh 4.1.1 | Cassandra 2.0.7.31 | CQL spec 3.1.1 | Thrift protocol 19.39.0
I wanted to connect to a keyspace say "X" via eclipse through java.
Following is my code :
package cassandraConnectivity;
import java.sql.DriverManager;
import java.sql.SQLException;
public class connect{
public static java.sql.Connection con = null;
public static void main(String[] a)throws ClassNotFoundException, SQLException{
try {
Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
con=DriverManager.getConnection("jdbc:cassandra:username/pswd#<IP>/<KS>");
System.out.println("cassandra connection established");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I have also added following jar files to my eclipse build path :
apache-cassandra-0.8.4.jar
apache-cassandra-cql-1.0.3.jar
apache-cassandra-thrift-0.8.4.jar
casssandra-clientutil-1.2.1.jar
cassandra-jdbc-1.2.5.jar
commons-lang-2.4.jar
guava-r08.jar
libthrift-0.6.jar
log4j-1.2.16.jar
slf4j-log4j12-1.6.1.jar
slf4j.api-1.6.1.jar
I have also disabled the firewall at the remote location where cassandra is installed
But despite this I am getting an error :
Exception in thread "main"
org.apache.cassandra.cql.jdbc.DriverResolverException:
java.net.ConnectException: Connection refused: connect at
org.apache.cassandra.cql.jdbc.CassandraConnection.(CassandraConnection.java:91)
at
org.apache.cassandra.cql.jdbc.CassandraDriver.connect(CassandraDriver.java:86)
at java.sql.DriverManager.getConnection(Unknown Source) at
java.sql.DriverManager.getConnection(Unknown Source) at
cassandraConnectivity.connect.main(connect.java:15)
Also it is not able to find the source for the above jar files that were added externally
Kindly let me know where i am going wrong
The JDBC driver is very old. You should use the new native Java driver, which you can get here.
You may want to use an established driver - something like Astyanax: https://github.com/Netflix/astyanax
The getting started page has some pretty straightforward examples:
AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
.forCluster("ClusterName")
.forKeyspace("KeyspaceName")
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
)
.withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
.setPort(9160)
.setMaxConnsPerHost(1)
.setSeeds("127.0.0.1:9160")
)
.withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());
context.start();
Keyspace keyspace = context.getClient();
http://motionsharing.com/site/download/acefad55f1e81e456d71a7448b278915
Cannot connect to MySQl database from Eclipse DataSource Explorer.
I tune appropriate driver but I cannot to connect.
P.S.: I can connect to derby db and I can connect to mysql db but only from code, but I want to connect to mysql db from Data Source Explorer to get posivbilities to create tables and so on.
What is wrong ?
I can connect through code.
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loading success!");
String url = "jdbc:mysql://localhost:3306/mydb";
String name = "root";
String password = "root";
try {
Connection con = DriverManager.getConnection(url, name, password);
System.out.println("Connected.");
con.close();
System.out.println("Disconnected.");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
You need to download MySQL JDBC driver and add it to the settings.
You will see these screens when you try to create a new connection profile in Datasource Explorer. In the last step, you'll have to point to the right downloaded JAR file.
In Driver Properties make sure to use:
URL: jdbc:mysql://localhost:3306/schema_name
Database: schema_name
So Eclipse will show the tables en the Data Source Explorer.