java.lang.ClassNotFoundException - java

I've installed MaxDB in my local machine and I'm trying to make a connection to it using Java.
And I'm getting this error when running :
Exception in thread "main"
java.lang.ClassNotFoundException:
com.sap.dbtech.jdbc.DriverSapDB
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.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sap.maxdb.Hello.main(Hello.java:15)
This is the code I'm using :
package sap.maxdb;
import java.sql.*;
public class Hello
{
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
String username = "DBM";
String password = "azerty";
String dbname = "SAPDB";
Class.forName ("com.sap.dbtech.jdbc.DriverSapDB");
String url = "jdbc:sapdb://" + dbname;
Connection connection = DriverManager.getConnection (url, username, password);
Statement stmt = connection.createStatement ();
ResultSet resultSet = stmt.executeQuery ("SELECT * FROM HOTEL.CUSTOMER");
resultSet.next ();
String hello = resultSet.getString (1);
System.out.println (hello);
resultSet.close ();
stmt.close();
connection.close ();
}
}
I did like they said in their website :
set CLASSPATH=%CLASSPATH%;C:\Program
Files\sdb\programs\runtime\jar\sapdbc.jar
But I get always the same error.
I know that I'm missing something but can't find it °!°
Waiting for your help.
Thanks.

What does "install" mean? Basically, the db server must be installed and running.
Step two is to add the driver (which you can find under C:\Program Files\sdb\programs\runtime\jar\sapdbc.jar, assuming this applies to your installation too) to the build path of your project. If the driver class cannot be found, it'll raise a ClassNotFoundException as you experienced.
You don't mention which IDE you are using, but try to set your project buildpath from the context menu.

Try adding the following code to make sure that your application picks up the CLASSPATH that you specified:
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}

Copy the driver's jar file to the 'lib' folder of the server. Then restart the server.

Related

classnotfoundexception: com.mysql.jdbc.Driver for java program [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 4 years ago.
i have an problem could anyone help me? i'm trying to connect to mysql using java but get an error
the error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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)
code :
public static Connection c;
static String host = "localhost";
static String port = "3306";
static String database = "";
static String username = "root";
static String password = "";
public static void connect() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
c = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Error trace shows its not able to resolve "com.mysql.jdbc.Driver"
Check whether dependency for mysql.jar is provided in class path. If its maven project check pom.xml for mysql dependency provided
Just download "ojdbc7.jar" and import that jar to your project.

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception in linux and text editor [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 5 years ago.
My code looks like this-(working in normal text editor)
import java.sql.*;
import java.sql.DriverManager;
class JDBCTest {
private static final String url = "jdbc:mysql://localhost/learn";
private static final String user = "root";
private static final String password = "B!SHu12345";
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, password);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from bishu where id =101");
while(rs.next()){
String s=rs.getString("id");
System.out.println(s);
}
System.out.println("Success");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
At run time it gives an exception as follows-
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:195)
at JDBCTest.main(first.java:14)
Mysql connector was downloaded and saved in the path- Java/jre1.8.0_91/lib/ext/
I have gone through many similar solutions available, but wasn't able to find a valid one.
Add the MYSQL JDBC Driver from the library of your IDE.or place this JDBC driver file inside the lib directory of your project. Your question looks similar to this question

Trying to connect mySQL database with a java app keep getting stuck [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 5 years ago.
ok so i have a java fx app that will display info from a mysql database runing locally on the same computer. i keep getting driver not found. here is the code:
String databaseURL = "jdbc:mysql://localhost:3307/mysql?zeroDateTimeBehavior=convertToNull";
String user = "user";
String password = "password";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(databaseURL, user, password);
if (conn != null) {
System.out.println("Connected to the database");
}
} catch (ClassNotFoundException ex) {
System.out.println("Could not find database driver class");
ex.printStackTrace();
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
and here is the stack trace:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at got_map.GOT_Map.start(GOT_Map.java:130)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1468396900.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/584634336.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1960854798.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/501263526.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Apr 21, 2017 6:29:06 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.111 by JavaFX runtime of version 8.0.20
Any help would be much appreciated
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
...
at java.lang.Class.forName(Class.java:259)
at got_map.GOT_Map.start(GOT_Map.java:130)
This : Class.forName("com.mysql.jdbc.Driver"); is not enough.
Indeed, it rises a ClassNotFoundException if the class cannot be retrieved from the runtime classpath.
You need just to add in the runtime classpath the MySQL JDBC driver matching with the installed MySQL version.
The problem is in the line, Class.forName("com.mysql.jdbc.Driver");
The reason for getting java.lang.ClassNotFoundException can be an issue in loading the driver.Add the *.jar driver to your project if you haven't added it.

I keep getting this Exception when trying to run my program:

Die Datenbank 'C:\TEMP\derbyDB01' konnte nicht mit dem Klassenladeprogramm sun.misc.Launcher$AppClassLoader#253498 gestartet werden. Details können Sie der nächsten Ausnahme entnehmen.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.seeNextException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.bootDatabase(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection30.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection40.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Unknown Source)
at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:243)
at Importer.getCon(Importer.java:88)
this is the part of the code, causing the exception:
private static final String jdbc_driver = "org.apache.derby.jdbc.EmbeddedDriver";
private static Connection conn = null;
public void importToDB(String DB_URL, String[] header, List<Data> dataList, File csv ) throws SQLException, ClassNotFoundException {
stmt = null;
ResultSet rs = null;
String sql = null;
String tablename = spl.getTableName(csv);
Class.forName(jdbc_driver);
if (conn == null) {
conn = getCon(DB_URL);
}
...
public static Connection getCon(String DB_URL) throws SQLException {
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL);
System.out.println("Connected successfully...");
return conn;
}
the String DB_URL is given to the method by taking it from a JTextfield. the URL I use is correct, because the program worked with it, before adding the GUI. it is: "jdbc:derby:C:\TEMP\derbyDB01"
So what is causing so much problems in here?
According to Derby developer guide, you need to reverse the slashes in URL
jdbc:derby:c:/TEMP/derbyDB01
Sorry for taking so long to Update this.
Almost forgot I had it posted here. The problem was that I opened the database connection via an eclipse db plugin. When closing that plugin, it doesn't properly close the connection, which then produces in this exception when the program is trying do gain access again.

GWT Problem with MySQL Connection

I am working on a GWT project in which I need to make some MySQL queries. I have handled RPC properly and in the server-side I am trying to make a mysql connection but am running into an exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2412)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
.
.
.
more
.
.
.
I created a very simple java class to test out my code to confirm that my connection syntax and use is proper, and have confirmed that syntax is not a problem because with the very same code in a simple only main method java app I can create a connection and query the database properly. I have made sure to have mysql-connector-java-5.1.16-bin.jar in the classpath as well as the /lib folder in the WEB-INF folder.
Here is the class that I am using in order to create a connection:
public class DB_Connection {
protected Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql:///",
"",
"");
}
catch (Exception ex)
{
ex.printStackTrace();
}
return conn;
}
}
public class Login extends DB_Connection {
public User getUser(String email) {
User user = new User();
user.setUserEmail(email);
String query = "";
try {
Connection conn = getConnection();
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(query);
while(result.next()) {
user.setUserId(result.getInt("rvuser_id"));
System.out.println(user.getUserId());
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return user;
}
It seems my problem happens all the way at the beginning in DB_Connection. Does anyone know what is making this happen? It is strange to me, because as a standalone the code works fine.
Another thing I just realized that I am at home now and don't even have access to that database server as it is on a local network and I am not connected through VPN. So it must be failing somehow before it even attempts to make the connection.
Thanks!
com.google.appengine ..
Does it means you are deploying on Google App Engine?
If so, MySQL,JDBC and many other things are restricted.

Categories

Resources