Why driver is still missing? - java

I am trying to use Java DB for app only storage.
Environment Variables:
CLASSPATH:
%JAVA_HOME%\jre\lib;%DERBY_HOME%\lib\derby.jar
DERBY_HOME:
C:\Program Files\Java\jdk1.7.0_25\db
Path:
%DERBY_HOME%\bin
No changes in eclipse configurations was done and i cannot include it to build path(it somehow will be considered as third party library, which is not allowed)
When i type "sysinfo" in cmd it tells me Db is instaled and list packages and other info.
Code:
public class Main {
private static String dbURL = "jdbc:derby:AssertDB;user=me;password=mine;create=true";
private static Connection con = null;
public static void main(String[] args) {
setDBSystemDir();
createConnection();
CookiesTable pTable = new CookiesTable(con);
try {
pTable.createTable();
pTable.populateTable();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
// load files
// parse files to tables
// retrive SQL statment
// print result
}
private static void setDBSystemDir() {
// Decide on the db system directory: <userhome>/.addressbook/
String userHomeDir = System.getProperty("user.home", ".");
String systemDir = userHomeDir + "/.asertdb";
// Set the db system directory.
System.setProperty("derby.system.home", systemDir);
}
private static void createConnection() {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
// Get a connection
con = DriverManager.getConnection(dbURL);
} catch (Exception except) {
System.out.println("DRIVER DRROOOOOP");
except.printStackTrace();
}
}
}
Exception:
java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
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: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:190)
at com.tuto.p4.Main.createConnection(Main.java:44)
at com.tuto.p4.Main.main(Main.java:13)
Exception in thread "main" java.lang.NullPointerException
at com.tuto.p4.CookiesTable.createTable(CookiesTable.java:29)
at com.tuto.p4.Main.main(Main.java:16)

Refering to the documentation there are two things to do in case you get a java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver if you run the SampleApp.
Configure Embedded Derby
Verify Derby
As you said that you checked sysinfo I would try it with adding derby.jar and derbytools.jar even if the tools are optional.
Or you can compare the SampleApp to your App....

Include derbytools.jar, derbyclient.jar as well.

Try putting System.out.println(System.getProperty("java.class.path"));
before Class.forName(...).newInstance()
The output must contain a valid path to derby.jar.

Related

Inserting Data into MySQL using Java

I try to insert data into MySQL using Java and I have error all the time.
I use this video to understand how to do to the connection.
This is my Java code:
import java.sql.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
//DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/try1-progrem", "root", "123456");
Statement st = conn.createStatement();
String username = "kola";
String password = "24688642";
String insert = "INSERT INTO login VALUES ('"+username+"','"+password+"')";
st.executeUpdate(insert);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Get the error:
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/try1-progrem
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:11)
And here to SQL image: sql data:
** I learned from YouTube how to make the connection, if there is a good guild, I will glad to take him.
*EDIT1:
build
To fix this you should follow the steps below:
You need to download the correct ConnectorJ from the link below.
After downloading the correct installer for your machine you will need to run the installer to install only the correct jdbc connector for you. (A series of prompts will lead you to selecting the connector and not all the MySQL downloads)
Then you can put the .jar file (connectorJDBC file) into your classpath appropriately and this should fix your problem as I encountered a similar problem while trying to connect to a SQL server with java.
Hopefully this fixes your problem, comment if it doesn't. :)
ConnectorJ Download

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

Connecting to MySQL using JDBC [duplicate]

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!");
}
}
}

Apache Spark MySQL JavaRDD.foreachPartition - why I getting ClassNotFoundException

I would like to save data from each partition to MySQL Database. For doing that I created Class which implements VoidFunction<> :
public class DatabaseSaveFunction implements VoidFunction<Iterator<String>> {
/**
*
*/
private static final long serialVersionUID = -7039277486852158360L;
public void call(Iterator<String> it) {
Connection connect = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://"
+ "xxx.us-west-2.rds.amazonaws.com" + "/"
+ "xxx", "xxx", "xxx");
preparedStatement = connect
.prepareStatement("insert into testdatabase.test values (default, ?)");
while (it.hasNext()) {
String outputElement = it.next();
preparedStatement.setString(1, "" + outputElement.length());
preparedStatement.executeUpdate();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connect.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
And in my main method class I'm calling:
output.foreachPartition(new DatabaseSaveFunction());
I'm getting following error:
15/05/06 15:34:00 WARN scheduler.TaskSetManager: Lost task 0.0 in stage 1.0 (TID 4, ip-172-31-36-44.us-west-2.compute.internal): java.lang.ClassNotFoundException: DatabaseSaveFunction
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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
Worker log:
15/05/06 15:34:00 ERROR executor.Executor: Exception in task 1.0 in stage 1.0 (TID 5)
java.lang.ClassNotFoundException: DatabaseSaveFunction
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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
Can anybody tell me what I'm doing wrong ? I would be very grateful for that.
Export the external class to jar and add that like sc.addJar("/path/x.jar") where sc is JavaSparkContext in your main. Then you wont get this error. The error is because you spark program is not able to find the class. Moreover in spark 1.3 and greater you can simple use a map options of jdbc and then use load("jdbc", options) to create a data frame and load data from any RDBMS. its really handy. I am not sure if this method works for connecting any RDBMS into spark. Please tell me if you have any other question.

Getting error while Connecting to oracle XE with NetBeans IDE

I am getting the following error while executing this code
import java.sql.*;
public class DatabaseConnectivityTest {
public static void main(String args[]) throws ClassNotFoundException
{
Connection conn=null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Trying to connect to database");
conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","hr","hr");
System.out.println("Connected");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
The error that i am getting is
Exception in thread "main" 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:276)
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:169)
at DatabaseConnectivityTest.main(DatabaseConnectivityTest.java:10)
right click on your project folder --> Properties --> Java Build Path --> Libriaries --> Add External Jar --> Your sql connector jar file directory
i hope i was able to help you :)
this is due to you have not included oracle driver in the classpath
If you are using eclipse then try the following
right click on your project
click buildpath->configure build path
click libraries tab
click add external jars and give the path of oracledriver jar file
Try this code:
import java.sql.*;
public class DatabaseConnectivityTest {
public static void main(String args[]){
Connection conn=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e) {
System.err.println("The driver is not loaded properly");
}
try
{
System.out.println("Trying to connect to database");
conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","hr","hr");
System.out.println("Connected");
}
catch(SQLException e)
{
System.err.println("Error while connecting!");
}
}
}
This code will give you clear idea where your program is failing. If you are getting a message "The driver is not loaded properly then add them to the build path. Download the correct jar file and add them to your project.

Categories

Resources