not able to connect to mysql db using java - 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.

Related

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver even when jar files are placed at classpath

Trying to Execute this java file from command line without using Maven etc to connect to Mysql with jar files placed at a $HOME/Dependancy/*.jar
import java.sql.*;
public class MysqlCon
{
public static void main(String[] args)
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bpx","romeo","BPXmysql*123");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select name from product_tbl");
while(rs.next())
System.out.println(rs.getString(1)+" Get Product ");
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
still getting this error
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
tried to get refer previous answers in the similar context ,but all of them refer of adding jar files and add them to the class path while executing which I have already done
Not sure what I am doing wrong
jar files used
mysql-5.1.10.jar mysql-connector-java-5.1.13.jar mysql-connector-java-8.0.26.jar
Thanks in Advance
cmd line code
javac -cp '.:/home/blesson/scripts/utilities/core_java/Dependancy/' /home/blesson/scripts/utilities/core_java/MysqlCon.java
java -cp '.:/home/blesson/scripts/utilities/core_java/Dependancy/' /home/blesson/scripts/utilities/core_java/MysqlCon
place mysql-connector-java-8.0.26.jar same folder where your .java file exist
open command line in the same folder or change directory to where your .java file exist
compile
javac -cp .:/home/sts0020/Desktop/java/jdbc/mysql-connector-java-8.0.26.jar YOUR_CLASS_NAME.java
run
java -cp .:/home/sts0020/Desktop/java/jdbc/mysql-connector-java-8.0.26.jar YOUR_CLASS_NAME.java

MySql Connector/J 5.1.41 not connecting

I'm using MySQL Connector/J 5.1.41 to connect to a DB, here's the code I'm using to connect:
package DB;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class ConexionMYSQL {
public String db="floreria";
public String url= "jdbc:mysql:/localhost"+db;
public String user= "root";//aqui va el usuario del phpmyadmin
public String pwd="";
public Connection conectar(){
Connection link = null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
link = DriverManager.getConnection(this.url, this.user, this.pwd);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "ERROR: "+e);
}
return link;
}
}
I'm a newbie in Java and mysql connections, when I execute the code, It shows me the following exception:
ERROR:java.lang.ClassNotFoundException:org.gjt.mm.mysql.Driver
I've already checked that the jar file is in the classpath, I've reinstalled and replaced the jar file for MySql connector too, but stays the same. As I said, I'm new in this world, I barely know about JavaFX and mysql connections, so thats all info I think I can provide, here's an image from my classpath, maybe there's the problem:
Change the code like this : public String url= "jdbc:mysql://localhost/"+db;
Make sure that MySQL Connector/J 5.1.41 jar file placed in the
class path NOT the zip file.
unzip the MySQL Connector/J 5.1.41 jar file and make sure that
this Driver ( org.gjt.mm.mysql.Driver) is exist .
If not, check com.mysql.jdbc.Driver class is Exist and change the
Driver class as com.mysql.jdbc.Driver.
First of all you need to change url like this
public String url= "jdbc:mysql://localhost/"+db;
put a file with MySQL driver to your classpath in NetBeans, so the IDE know the driver class you want to load.
The other thing is a strange driver name org.gjt.mm.mysql.Driver, usually it is com.mysql.jdbc.Driver. Please try using the com.mysql.jdbc.Driver driver name and put a mysql-connector jar into your classpath. You can find the driver in MySQL JDBC Connector JAR, which you can download here:
http://dev.mysql.com/downloads/connector/j/
Right click on Libraries > Click on Add Library.
Scroll down to find MySQL JDBC driver.
Press Shift + F11. (Clean and Build)
Run
First : Change this line from : public String url= "jdbc:mysql:/localhost"+db; to public String url= "jdbc:mysql://localhost/"+db;
Second : This is a strange driver name : org.gjt.mm.mysql.Driver
This is the one I know of : com.mysql.jdbc.Driver so you can change line from : Class.forName("org.gjt.mm.mysql.Driver"); to this one :Class.forName("com.mysql.jdbc.Driver");
EDIT: Mistake on the driver name, I've corrected it.
Make sure the jar files are in the classpath.
Good luck.

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

Java to MySql Connection on Linux

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

Categories

Resources