how to connect mysql database to desktop java application using jdbc ?
the environment is ubuntu 12.04 Lts i tried so many ways but all of them throw an exception on executing
Class.forName("com.mysql.jdbc.Driver");
the exception say :
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
the full code is :
import java.sql.*;
public class CRUDForGoods {
private Connection connection ;
public Vector<GoodsStore> goods ;
public CRUDForGoods(){
try{
DriverManager.registerDriver(new JdbcOdbcDriver());
connection = DriverManager.getConnection("jdbc:odbc:dbName","root", "root");
}
catch(Exception ex) {
System.out.println("##connection");
ex.printStackTrace();
}
}
com.mysql.jdbc.Driver not found while executing the code.
You need mysql connector to get it run (the jar includes com.mysql.jdbc.Driver). You can find it here
Download and include the jar file in your classpath.
Related
Since yesterday I have been trying to figure out how to fix this SQLException, and at this point I'm empty. I have checked dozens of sites, the whole documentation for Microsoft and Azure on how to connect with databases, and I keep getting the same error.
I've added a CLASSPATH like CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 8.2 for SQL Server\sqljdbc_8.2\enu\mssql-jdbc-8.2.2.jre11.jar as informed on Using the JDBC Driver documentation. I've downloaded the JDBC driver from Microsoft themselves and dumped the files on a folder in C:\Program Files\Microsoft JDBC DRIVER 8.4 for SQL Server as their documentation suggests. My maven and java are working fine, and I have an example class that I've got from SQLExamples by Microsoft themselves, this one:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// As informed on:
// https://www.microsoft.com/en-us/sql-server/developer-get-started/java/windows/step/2.html
public class App {
public static void main(String[] args) {
String connectionUrl = "jdbc:sqlserver:[AzureAddressHere];"
+ "database=[DatabaseHere];"
+ "user=[UserHere];"
+ "password=[PasswordHere]";
try {
// Load SQL Server JDBC driver and establish connection.
System.out.print("Connecting to SQL Server ... ");
try (Connection connection = DriverManager.getConnection(connectionUrl)) {
System.out.println("Done.");
}
} catch (SQLException e) {
System.out.println();
e.printStackTrace();
}
}
}
I've also added the references to the jar file to the pom.xml dependencies list:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.1.jre11</version>
</dependency>
I've also found a mention in a website that the jar file sits in Maven's library folder, so I've copied the jar file to the lib folder as well.
Yet, whenever I try to run the app, I get the java.sql.SQLException: No suitable driver found for [AzureInformation].
Any ideas on what I could be missing here?
Turns out that the issue was the lack of two // in the address. facepalm
So, I just set this line to: String connectionUrl = "jdbc:sqlserver://[AzureAddressHere];" and it finally connected.
To be fair to myself here, this error is so completely unclear and unhelpful.
I had developed a Java Application Project using Netbeans IDE 8.2 on Ubuntu14. How can I have its installers for Linux Systems using Netbeans IDE(.deb) Native Packaging.
I had packaged it by following instruction in this link
But, this tutorial above mentioned is NetBeans 7.4 native packaging in Windows. But I followed this tutorial in Netbeans8.2 in Ubuntu(my system). And I got .deb file in specified folder in the tutorial mentioned above.
The Project-name-1.0.deb file could be successfully installed in my Ubuntu System, but while running this installed application it shows error: "java.sql.SQLException: Opening db:'DB-NAME.sqlite' : Permission denied"
My project uses sqlite DB for data storage. I had used JAR sqlite-JDBC-3.19.3.jar for its Library.
I had used Netbeans IDE 8.2 in Ubuntu and created the new Java Application Project in it.
Now, how can I package this Java Application project to get its installer for Linux(.deb) without this Permission denied error
The java Connect.java class is as follows
import java.sql.*;
import javax.swing.JOptionPane;
public class Connect {
Connection con = null;
Statement stmt = null;
public static Statement ConnectDB() {
try {
Connection conn = null;
Statement stmt = null;
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:JavaApp3DB.sqlite");
stmt = conn.createStatement();
return stmt;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
return null;
}
}
The application execute successfully in NetBeans without any permission denied error. But, it shows the error when the Packaged (.deb) installer when installed and executed in Ubuntu.
Change this jdbc:sqlite:JavaApp3DB.sqlite
to this jdbc:sqlite:JavaApp3DB.db
When you want to connect to your database you need an address and this address mention to a file we call database.db not .sqlite.
I am trying to make a java application which would use MySQL as database. I have successfully done this number of times using Windows but I am facing problems with Mac OS. Even after placing the "my sql connector" jar file in lib folder and setting proper classpath, I am getting an exception of
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
Screenshot of code along with exception.
Please Help.
As you want to connect to MySQL, you need to use the right Driver class - see MySQL Documentation on MySQL Connector/J
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
}
}
Please note, your JDBC url is also wrong, or do you try to connect via odbc? (MySQL JDBC URL for example: jdbc:mysql://localhost:3306/mydatabase , driver class: com.mysql.jdbc.Driver)
Trying to connect my java web application to mysql database using tomcat 8 server and getting same error every time: No suitable driver found for jdbc
this is my method that is trying to connect to database
public String kreiraj() {
String g="com.mysql.jdbc.Driver";
//Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting database...");
String url = "jdbc:mysql://localhost:3306/fc";
String user = "root";
String password = "";
try (Connection connection = DriverManager.getConnection(url, user, password)) {
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
return "index"; }
If I start same code as standard java application it work and it connects to database without any problems.
So I figured that the problem must be something with tomcat 8 server. I tried every possible solution there is and im still getting same error.
I tried this things:
adding mysql connector jar file to build path
adding mysql connector to WEB-INF/lib folder of my web application
adding mysql connector to tomcat 8 lib folder
tried using Class.forName("com.mysql.jdbc.Driver"); but getting classnotfound error
Nothing seems to work and im starting to lose my mind. If anyone know solution it would be great. Ty
I am using the following code to connect to a derby database in a dyamic web project but it does not work. If I use the same code in a normal java project it works can you guys help me out.
private Connection connection = null;
public DBconnection(){
createConnection();
}
public void createConnection(){
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
this.connection = DriverManager.getConnection("jdbc:derby://localhost:1527/student");
}catch (Exception e){
e.printStackTrace();
}
}
public Connection getConnection(){
return this.connection;
}
Note:
I have included all the derby libraries in my dynamic web project and also I have I have started the derby server everything works but this code does not connect to the database. Also I am running the dynamic project on a tomcat server the port for that is 8080 and the port for the derby server is 1527.
Please copy the derbybyclient.jar file which is present in derby installation lib folder into the lib folder of your tomcat installation.
Rest of the code is fine.
For more information you can refer this link