MySql Connector/J 5.1.41 not connecting - java

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.

Related

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

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.

Exception: No suitable driver found for jdbc:mysql

I am trying to connect to a mysql database by using this simple code.
import java.sql.*;
public class OdbcAccessConnection_1 {
public static void main(String [] args) {
Connection con = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
// Connect with a url string
con = DriverManager.getConnection("jdbc:mysql://localhost/books","root","1234");
System.out.println("Connection ok.");
con.close();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
e.printStackTrace();
}
}
}
All it does is tell me if the connection is working. There is no problem with my database and this code/connection work on netbeans. The StackTrace i am getting is -
the java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/books
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at OdbcAccessConnection_1.main(OdbcAccessConnection_1.java:13)
I am working on 64 bit windows 7 and using 5.1 versions of the Connector/ODBC driver 64 bit. On the ODBC all seems to connect and the test was successful. But when i run the code i get the stack trace above. I am missing something very simple so any input and help would be very much appreciated.
Thank you:)
Go to Run menu in netbeans or whatever IDE you are using
=>Set Project Configuration then Customize.Then choose the Libraries on left dropdown menu
Add your appropriate driver file either jar or folder.
Click OK.
jdbc:mysql://localhost/books is a URL that you use to connect to MySQL directly, using the MySQL JDBC driver. The URL used by the JDBC/ODBC driver is different (see http://docs.oracle.com/javase/1.3/docs/guide/jdbc/getstart/bridge.doc.html).
The usage of this JDBC/ODBC bridge is discouraged, and should only be used to access a database that doesn't provide any JDBC driver. This is not the case of MySQL. Use Connector/J, their JDBC driver. Once you have this driver in your classpath, you can use the URL you're currently using, and remove the JDBC/ODBC driver from your classpath (and its loading from your code).
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books","root","1234");
This error creeped on me because I forgot to add the Class.forName line. The mysql driver jar was on the classpath but no one implicitly loads the driver class, so the session factory can't find any driver classes loaded. Thus the purpose of this line.
In your case, you're loading the wrong thing. It should be Class.forName("com.mysql.jdbc.Driver") if you intend to use it with a jdbc:mysql:// connection.
best solution bhai logo -:
go to JCreator configure menu then click to options then JDK profiles then double click to whatever the version u r using automatically mention there then click to add archive then go to that path -> C:\Program Files\MySQL\MySQL Tools for 5.0\java\lib\mysql-connector-java-5.0.4-bin.jar press ok.

Not SQL Driver Found - but Driver JAR File is implemented

I connect to a database:
void connectToDataBase(){
dataManager_ref = new DataBaseConfigurationManager();
try
{
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dataBase","root","");
System.out.println("Connection successful");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
I implemented a JAR File for the driver:
mysql-connector-java-5.1.17-bin.jar
and imported it into the servlet
import java.sql.DriverManager;
this isnt the first time I use this database (tho the first time with Java EE web). This time I get the following exception:
No suitable driver found for jdbc:mysql://localhost:3306/dataBase
The application is running on a glassfish server 3.1, can I even use a database on a mysql server here? Can somebody help please
thanks in advance,
Daniel
You sometimes need to load the Driver class explicitly in order for the DriverManager to be aware of it.
Try this
Class.forName("com.mysql.jdbc.Driver");
Before you call the DriverManager
You can add a CLASSPATH variable in Environmental System variables, and set the path to your connector, path including name of connector.jar.
Also mysql-connector-java-5.1.17-bin.jar is showing some incompatibilities in accessing. it gave me lots of errors, so i had to go bac to 5.0.x versions

java.sql.SQLException: No suitable driver on Mac OS X Attempting to use Derby

I am getting a java.sql.SQLException: No suitable driver when I am trying to connect to a database with Java. I am on Mac OS 10.5 using the NetBeans IDE. It seems to be having trouble with the EmbeddedDriver, but I'm not sure what I am missing:
public class A
{
Connection conn = null;
public A(String URL, String username, String password) throws SQLException
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
conn = DriverManager.getConnection(URL, username, password);
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
invalidate();
}
catch (ClassNotFoundException classNotFound)
{
classNotFound.printStackTrace();
invalidate();
}
}
}
"No suitable driver" usually means that the URL you've supplied to connect has incorrect syntax. What is your URL?
The server version would have a host and port; I believe the embedded URL should be "jdbc:derby:flixnet", according to these docs: http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
Use "org.apache.derby.jdbc.ClientDriver".
As i see you are accessing a derby server not an embedded database.
It is declared as a constant here:
final String DATABASE_URL = "jdbc:derby://localhost:1527/flixnet";
Don't ask about the name... I got this URL by right-clicking the database in NetBeans and then going to Properties -> Database URL.
And I have added the derby.jar and derbyclient.jar files from /Applications/NetBeans/glassfish-v3-prelude/javadb/lib/derby.jar and derbyclient.jar from the same directory.
Did you add the derbyclient.jar to the "Libraries" folder in your Netbeans project?
I had the same problem.
Try this:
Make sure you update your JDK and Netbeans to latest version
Right click your project's Libraries
Choosing "Add Library..."
Find and choose "Java DB Driver", then click "Add Library"
That's how I solved the problem, I hope this will help some new programmer like me:)
PS It will add "derbyclient.jar" for you

Categories

Resources