This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 1 year ago.
EDIT (fixed):
I found out how to fix this issue, it apperently had something to do with the fact I used gradle to build my project (which is standard for modding with FabricMC). Because of that, I had to add the MySQL Connector/J to the dependencies in the build.gradle file. After that I built another project and the SQL connection worked!
I didn't need to add it as a library or dependency afterward. I also didn't have to load the driver using Class.forName();
dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
implementation 'mysql:mysql-connector-java:8.0.27'
}
Im writing a Minecraft mod using the Fabric API in Intellij Idea and need to communicate with a database that's on another server (which is using DirectAdmin).
I've tried a lot of solutions suggested from different questions, like loading the driver first or installing the Database Manager plugin. Those sadly don't seem to solve the problem.
I have also quadruple-checked if I'm using the correct path to the database, which I seem to be doing.
(Im using Java 16, in case that helps)
I tried make the connection, but keep getting the same error:
java.sql.SQLException: No suitable driver found for jdbc
I have included the mysql-connector-java-8.0.27.jar library and am still getting the error.
After that I also added it as a dependency, but it still doesn't work.
Am I missing something here? I've been reading a lot of these posts but none of the solutions seem to work.
Something I'm suspecting is that I have to add some sort of library to the server the database is on as well, but I don't know how I would do that.
This is the code I use to make the connection. I call the connect() method from my Main class, which is in a try/catch
package nl.egelblad.tutorial.sql;
import java.sql.*;
public class MySQL {
private String host = "185.102.34.56";
private String port = "3306";
private String database = "my_db";
private String username = "db_username";
private String password = "db_password";
private Connection connection;
public boolean isConnected() {
return (connection == null ? false : true);
}
public void connect() throws ClassNotFoundException, SQLException {
if (!isConnected()) {
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?useSSL=false&autoReconnect=true", username, password);
}
}
public void disconnect() {
if (isConnected()) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public Connection getConnection() {
return connection;
}
}
You have to load driver at the beginning (only one time) :
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// Or "com.mysql.jdbc.Driver" for some old mysql driver
} catch(ClassNotFoundException e) {
getLogger().error("Failed to load driver");
e.printStackTrace();
}
Then, you should check that the given jar is exported with good dependencies. Depending or your minecraft version, maybe it's already include. But it's possible that it's not.
So, you have to change your export settings to include the jar mysql-connector-java-8.0.27.jar in your own.
Related
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 20 days ago.
enter image description here
There is a JDBC Driver in settings of plugin DBNavigator and inside these settings everything is ok as shown on screen. Database is connecting using this plugin without any isssue. But when I try to make a connection I receive SQLException.
Here is the code
public class TestDB {
private final static String uName = "root";
private final static String uPass = "root";
private final static String sqlUrl = "jdbc:mysql://localhost:3306/mydbtest";
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection(sqlUrl, uName, uPass)) {
System.out.println("Connected");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
I tried to configure path to JDBC Driver in plugin manually but it didn't change anything. I still receive SQLexcepption.
enter image description here
I've read also that it is possible to copy *jar file of driver to classpath. But why do I have to do this if such driver has already installed in DBNavigator? The question is whether it somehow would be working in this configuration only with plugin?
In case it wouldn't be working like that, what should I do to fix it?
I added *jar to File->Project Structure-> Modules in Intelij Idea. Now everything is working fine.
I am trying the following code to make connection with my database online, hosted on ElephantSQL.
private static Connection getConnection() {
try {
Class.forName("org.postgresql.Driver");
}
catch (ClassNotFoundException e) {
System.out.println("Jar not found "+e.getMessage());
}
//dbUrl is given this way
String dbUrl = "jdbc:postgres://database:password#manny.db.elephantsql.com:5432/database";
String username = "database";
String password = "password";
try {
return DriverManager.getConnection(dbUrl,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
But I am getting the following error:
java.sql.SQLException: No suitable driver found for < url >
I tried all the things from the similar questions which I found here
Question 1
and
Question 2.
But nothing worked and I am stuck. I would appreciate any help.
As documented in the manual the URL for Postgres must be structured like this:
jdbc:postgresql://host:port/database
The prefix jdbc:postgres needs to be jdbc:postgresql and the part database:password#manny.db.elephantsql.com:5432 in your URL is wrong. It's hard to tell what exactly the hostname is, but I guess you need to use:
jdbc:postgresql://manny.db.elephantsql.com:5432/database
It seems that your driver jar file is not in classpath.
One of the option is put driver jar file in lib directory of your project and add jar file in classpath ( or buildpath in eclipse )
you can download dirver file from given URL
https://jdbc.postgresql.org/download.html
This question already has an answer here:
Manipulating an Access database from Java without ODBC
(1 answer)
Closed 6 years ago.
I want to connect Java 8 with Access but the following error occurs and I don't know how to fix it. I always get this error:
java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://C:/Users/Ghazi/workspace/java w access/login.accdb
I added 4 libraries:
hsqldb.jar
jackcess-2.0.7.jar
org.apache.commons.lang-2.6-source.jar
org.apache.commons.loggin-1.1.1-source.jar
This is my code
import java.sql.*;
public class DbConnection {
Connection con;
Statement st;
DbConnection(){
dbconnect();
}
//-----------------------
public void dbconnect(){
try
{
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Ghazi/workspace/java w access/login.accdb");
Statement stment = conn.createStatement();
}
catch(Exception err)
{
System.out.println(err);
}
}
//--------------------------
public static void main(String[]args){
DbConnection ob=new DbConnection();
}//end main
}
Try adding "Class.forName():
public void dbconnect(){
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Ghazi/workspace/java w access/login.accdb");
Statement stment = conn.createStatement();
}
catch(Exception err) {
System.out.println(err);
}
}
The basic problem is that earlier versions of Java/JDBC used ODBC to connect to MS-Access ... and the ODBC driver has been removed from Java 8.
Two alternatives are to use:
1) UCanAccess: http://ucanaccess.sourceforge.net/site.html
... or ...
2) Jackcess (Jackcess 2.0: New crunchy outside, same yummy filling!): http://jackcess.sourceforge.net/
If that doesn't work:
3) Please specify what what IDE you're using (Eclipse, etc - if applicable)
4) Make sure your jackcess-2.0.7.jar is explicitly included in the CLASSPATH (how to do this depends on your IDE)
5) Consider using a directory without spaces in the name
I added 4 libraries
You need five (5) libraries. You forgot to add the "ucanaccess-x.y.z.jar" file itself.
For detailed instructions, see
Manipulating an Access database from Java without ODBC
I am having errors with connecting to a MS Access Database (CDCollection.accdb) from Java. The IDE we are using is Netbeans. I have done research on possible solutions as to the error, but due to being inexperienced in the topic, I am not too sure how to solve it. The rest of my class are having the same problem and the teacher does no know how to solve the problem either.
My class is now in the final chapter, Learning Unit 10: Accessing a Database from a Java program, of Funworks's exploring IT: Java Programming. Grade 11.
I would post links to the three sources I have found most useful, but Stack Overflow says I need to have 10 reputation points to post more than 2 links. I have searched both Stack Overflow and the web to find the solution to this error, but those were more advanced than where I currently am.
Edit: Here are the links, thanks to those who upvoted me. The solution they suggest makes sense, but does not seem to work...
http://community.microfocus.com/borland/managetrack/starteam/w/knowledge_base/16933.error-java-sql-sqlexception-microsoft-odbc-driver-manager-data-source-name-not-found-and-no-default-driver-specified.aspx
http://www.coderanch.com/t/440574/JDBC/databases/java-sql-SQLException-ODBC-Driver
http://www.codeproject.com/Articles/35018/Access-MS-Access-Databases-from-Java
From what I understand, it is due to the school computers and my laptop all being 64-bit and ODBC (Open DataBase Connectivity) being 32-bit. My teacher does not know how to solve this problem, as it is her first time encountering it. When the grade 11s of last year reached this section of the book, the school computers were still 32-bit.
According to what I've read, I need to go to the OBDC Data Source Administrator and, under the System DSN tab, add "Microsoft Access Driver (*.mdb, *.accdb)". I noted that it was already there under the User DSN tab.
Find the OBDC Data Source Administrator at C:\Windows\SysWOW64\odbcad32.exe or Control Panel\System and Security\Administrative Tools\Data Sources (ODBC). The first is preferable, as it seems to have more Drivers available even though the Control Panel shortcut targets the same executable file.
However, I still get the same error once I have added the driver.
Here is my code:
The DB class
package cd;
import java.sql.*;
public class DB
{
private static final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ={CDCollection.accdb}";
private Connection connection;
private PreparedStatement statement;
private ResultSet resultSet;
public DB()
{
try
{
Class.forName(driver);
System.out.println("Driver successfully loaded");
}
catch (ClassNotFoundException c)
{
System.out.println("Unable to load driver");
System.out.println(c);
}
try
{
connection = DriverManager.getConnection(url);
System.out.println("Connection successful");
}
catch (SQLException e)
{
System.out.println("Unable to connect");
System.out.println(e);
}
}
}
And the UseDB class:
package cd;
import java.sql.*;
public class UseDB
{
static DB db = new DB();
public static void main(String[] args)
{
}
}
For the
DBQ={CDCollection.accdb}
I have tried the full path to the file (C:\Users\\Documents\IT\Java\Databases\CD\CDCollection.accdb), using the double backslash due to the first marking an escape character. I have also tried with forward slashes (/) and I have tried without a path, as it is in the code, and placing the database in the \CD (the project folder), \CD\src (the source folder), \CD\src\cd (the package folder).
Yet, despite this, I and my classmates still get the same output every single we run the program:
run:
Driver successfully loaded
Unable to connect
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
BUILD SUCCESSFUL (total time: 0 seconds)
Could I please have help? Keep in mind that my class has only just started this module today; we have only been doing Access and SQL for 1 month and have been doing Java for a year and a half.
Thank you!
I would suggest you adding a SYSTEM DATASOURCE at ODBC configuration panel. Let's suppose you name it cdcollection. Then your url variable just needs to be:
private static final String url = "jdbc:odbc:cdcollection";
Here's a a nice STEP BY STEP article on how to do just that.
Welcome to StackOverflow.
So I just started learning about databases this week and one of the things I need to be able to do is connect to my mySQL database that I created using Java. I have done some research and I have tried to find the correct way of doing this I just can't seem to figure out how. Here is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Menu
{
public void menu()
{
Connection conn;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "gym";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "password";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
}
catch (Exception e)
{
System.out.println("NO CONNECTION =(");
System.out.println(e.getMessage());
}
}
}
Now the problem is, is that every time I run this code, the "No Connection =( " appears and then it says the error is: "com.mysql.jdbc.Driver". Can somebody please help me and say what I am doing wrong? Thank you. Much appreciated.
Your error means that your library path doesn't contain the jar that contains the com.mysql.jdbc.Driver class.
You don't have to change anything in your code. If you are running it via Eclipse, you should add mysql-connector-java-x.x.x-bin.jar to your build path (where x.x.x is the version of the jar).
All JDBC connection classes need their respective drivers which are normally supplied as jar files from the database vendor, add the relevant database driver to your classpath.
The .jar file will be available from the vendors site, in this case : http://www.mysql.com/products/connector/ and then to add this to the classpath of your project in the ide of your choice. Here is a guide for eclipse : http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)
Try and run again once you have this.