Java to MySql Connection on Linux - java

I googled this topic but I have yet to find the answer. I am on AWS Amazon instance running Java trying to run simple java to mysql database. I can access db fine through the mysql command. The error is driving me insane.
Note I am running under /development which happens to have the jar file.
[ec2-user#ip-172-31-16-232 development]$ ls
cProgram.c Java2MySql.class Java2MySql.java mysql-connector-java-5.1.12.jar
[ec2-user#ip-172-31-16-232 development]$ echo $JAVA_HOME
/usr/lib/jvm/java
[ec2-user#ip-172-31-16-232 development]$ java -cp /home/ec2-user/development/mysql-connector-java-5.1.12.jar Java2MySql.java
[ec2-user#ip-172-31-16-232 development]$ java Java2MySql
Where is your MySQL JDBC Driver?
MySQL JDBC Driver Registered!
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/smarteregsBlog
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at Java2MySql.main(Java2MySql.java:21)
Closing connection
Java2MySql.java
public class Java2MySql
{
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/myBlog";
String driver = "com.mysql.jdbc.Driver";
String userName = "sam";
String password = "Yoo!";
Connection conn = null;
try {
Class.forName(driver);
}catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
}
System.out.println("MySQL JDBC Driver Registered!");
try{
conn = DriverManager.getConnection(url,userName,password);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
System.out.println("Closing connection");
conn.close();
} catch (Throwable ignore){}
}
}

It seems you provide the jar only at compile time and not providing the jar class path when you try to execute the code:
javac -cp /home/ec2-user/development/mysql-connector-java-5.1.12.jar Java2MySql.java
I assume it to be a typo javac and not java
You need to provide the jar path when executing the code as well. So change this
java Java2MySql
to
java -cp .:/home/ec2-user/development/mysql-connector-java-5.1.12.jar Java2MySql
Note: mysql-connector-java-5.1.12.jar is only required at runtime in your case because you are loading the class dynamically.

Make sure the mysql connector jar is located in your classpath. Copying it in same folder as your class file wont work.
Or you can simply copy it to lib folder of your jdk.

Well while compiling the class you don't need to specify the jar in this case so simple this should do the job
javac Java2MySql.java
Change this
java -cp /home/ec2-user/development/mysql-connector-java-5.1.12.jar Java2MySql.java
to
java -cp .:pathOfTheDriverJar/mysql-connector-java-5.1.12.jar Java2MySql

Related

NoClassDefFoundError when the jar exists, specifically for mysql-connector-java-8.0.20.jar

I want to make sure I can connect to the MySQL server I have running locally with a simple Java class.
To test the connection I want to use the MysqlDataSource class that comes with the mysql-connector-java-8.0.20.jar
I'm on Ubuntu 18.04 LTS and have the jar located at:/usr/share/java/mysql-connector-java-8.0.20.jar.
Then I run the compiler command: $ javac -cp "/usr/share/java/mysql-connector-java-8.0.20.jar" App.java
Then: $ java App
And get the following output error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mysql/cj/jdbc/MysqlDataSource
at App.main(App.java:15)...
Output of $ mysql --version:
mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper
This is all my App.java file contains:
import java.sql.Connection;
import java.sql.SQLException;
import com.mysql.cj.jdbc.MysqlDataSource;
public class App {
public static void main(String[] args) {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("root");
dataSource.setPassword("test");
dataSource.setServerName("localhost");
Connection conn;
try {
conn = dataSource.getConnection();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}```
Instead of java App, you must specify a classpath to include the jar at runtime. Like,
java -cp "/usr/share/java/mysql-connector-java-8.0.20.jar:." App

exe from jar can't access MySQL from localhost

I have an GUI application that connect to MySQL through xampp. Running with netbeans is fine, but when I wrap it to exe using Launch4j from the jar, it can't connect to the database (it reaches the exception even though the interface still working)
I've choose custom classpath and it contains the mysql library.
And this is what throws the exception:
try
{
// create our mysql database connection
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/vietcombank.db?useUnicode=yes&characterEncoding=UTF-8","admin", "123456789");
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
The output of the exception is com.mysql.jdbc.Driver. But if I run the jar itself by cmd with direct location as java -jar "D:\Network Programming\ServerATM\dist\ServerATM.jar" it run just fine. Is it because Launch4j didn't include the libraries?
Thank you.
instead of this :
private String url = "jdbc:mysql://localhost/your-database-name";
Try to use this :
private String url = "jdbc:mysql://127.0.0.1/your-database-name";
Move the executable file you created to the dist folder, it worked for me

Java SQLite3 Connection Failing

I am having some trouble connecting my java script to my SQLite3 database.
I have my directory holding my script as followed
C:/PROG/JavaPROG/programs/java_database
Inside this directory I have 3 files
Query.java, Query.class and the database query.db
My java code looks as followed
import java.sql.*;
public class Query3 {
public static void main(String[] args) {
Connection conn = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:C:/PROG/JavaPROG/programs/java_database/query.db");
System.out.println("Connection Success");
} catch(Exception log) {
System.out.println("Connection Failed: " + log);
}
}
}
my classpath does contain
C:\Program Files\Java\jdk1.8.0\bin
and inside that bin directory is the sqlite jdbc driver
sqlite-jdbc-3.7.2.jar
I can connect my script to PostgreSQL database but I can't see why it isn't connecting to the SQLite3, I am getting the following error message
java.lang.ClassNotFoundException: org.sqlite.JDBC
have I done something wrong?
You need to explicitly provide the sqlite-jdbc-3.7.2.jar file in your classpath while compiling and running your classes using the -cp option. The java and javac commands will only look for .class files in the classpath and not jars.
Try this :
javac -cp <path_to_jar/sqlite-jdbc-3.7.2.jar>;. <your_class_name>.java
And if you have more than one thirdparty library, you can use a wildcard :
javac -cp <path_to_jar/*>;. <your_class_name>.java
Note that ; is the separator used in Windows. If you are on unix based systems, you need to use : instead of ;. Java maybe platform independant but the java and javac commands are not.
Also note that the . tells the java and javac commands to look in the current directory for classes. Don't forget to provide the classpath to the jars when running your program using the java command.
I have found the answer to fix this.
compile the script as normal
javac Query.java
Then specify the driver when executing
java -classpath ".;sqlite-jdbc-3.7.2.jar" Query
And it will run fine

not able to connect to mysql db using java

Here is the code that i am using to connect to MysSQL database using java.....
I am getting a runtime error...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Java2MySql
{
public static void main(String[] args)
{
String url = "jdbc:mysql://localhost:3306/";
String dbName = "java_test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "plsdonthack";
try
{
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
I am running it like this :
java -cp "$HOME/OOPS-Files/Code/learn/Driver/mysql-connector-java-5.1.34-bin.jar" Java2MySql
I have the .jar file there.
Here is the Error Message That i get when i run it.
I have physically gone and checked the folder com/mysql/jdbc and have found the file called Driver.java there....
Error: Could not find or load main class Java2MySql
The MySQL connector library Jar file needs to be in your classpath.
See : http://dev.mysql.com/downloads/connector/j/
Information how to set the classpath can be found in this posting Setting multiple jars in java classpath
In your case however it looks like you forgot to add your own code to the classpath. As you set the classpath with
java -cp "$HOME/OOPS-Files/Code/learn/Driver/mysql-connector-java-5.1.34-bin.jar" Java2MySql
your own code is not on the classpath anymore. So you need to include "." in the classpath (assuming that you run the java command from the same directory where your compiled classes are sitting.
java -cp "$HOME/OOPS-Files/Code/learn/Driver/mysql-connector-java-5.1.34-bin.jar:." Java2MySql
Place your MySQL connector jar in the lib folder, which you can find under,
WebContent -> WEB-INF folder. It should work.

How to run java program in linux terminal using classpath

i am new to linux terminal and does not know how to run java file using classpath.
class Test{
public static void main(String[] args){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException cnfe) {
System.err.println("Couldn't find driver class!");
}
}
}
The program compiled but unable to run.I have "mysql.jar" in the same directory. But i am unable to run this file. How can i run this file using classpath
Use:
java -cp ./:mysql.jar Test
Error is "ClassNotFound"?
You must include all the class files your application needs
java -classpath mysql.jar:. Test

Categories

Resources