exe from jar can't access MySQL from localhost - java

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

Related

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.

I am Connecting to data Base After Making Jar File in java But "ClassNotFoundException" after making jar file Below is code

Here IS Code Any Suggestions When i am trying to access the data base
it shows error after making jar file
File f = new File("E:\\DB\\**\\***.mdb");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
JOptionPane.showMessageDialog(null, "Driver loded succesfully");
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+f.getAbsolutePath();
connection = DriverManager.getConnection( database ,"","");
JOptionPane.showMessageDialog(null, "connection is"+connection);
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"erroris"+ e);;
}
ClassNotFoundException means that your program can't find the odbc driver class, try including the jar file in your path.
Its Only java Compatibility Issue . ODBC connection is not available in new versions of Java . So it work on old version on which i made. Thanks to
#Gord Thompson

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

Java JDBC | Cannot run from console

I have developed a program where I do some database connections and send some queries with JDBC.
I have used MySQL, NetBeans 6.9 under Ubuntu 11.04 as platform. When I run the app from NetBeans, it works perfectly but when I try to run it from terminal I get SQL Exception. This is the function that produces that SQL Exception. The program terminates before "Establish is ending" line.
public Connection Establish(String iname, String ipassword) throws SQLException
{
System.out.println("Establish...");
if(conn == null)
{
conn = DriverManager.getConnection("jdbc:mysql://localhost/ANU",
iname, ipassword);
}
else
System.out.println("Connection Already Established!");
System.out.println("Establish is ending...");
return conn;
} // End of Establish
Make sure the MySQL Connector .jar file is in your classpath environment variable. IDEs, like NetBeans, sometimes help you out with putting .jar files in the classpath while you're in the IDE. You'll either need to run your app with the -cp option, or add it to your classpath environment variable.

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