This question already has answers here:
Where can I download mysql jdbc jar from? [closed]
(3 answers)
Closed 3 months ago.
I have practically no experience with sql besides just learning about it yesterday in my java OOP class and trying to figure it out through YouTube. I am trying to use this free sql database website and be able to control it through java. Any tips on what I am doing wrong?
`
import java.sql.*;
public class Main {
public static void main(String[] args) throws Exception{
Connection connection = null;
Statement statement = null;;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://sql9.freesqldatabase.com:3306/sql9581604","sql9581604","ih8h2nhpxC");
}catch (Exception e) {
e.printStackTrace();
}
}
}
`
Error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:375)
at Main.main(Main.java:11)
Tried watching tutorials on YouTube but just keep getting all different errors.
You must put the jar file containing the jdbc connector in the same path of the code file.
Related
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 1 year ago.
I am currently making a backend reporting system (for a voting system assignment) using Java on VS Code, I am connecting to a MySQL database using the JDBC library in order to do calculations and stats and so on. So what happens is that once I create a project file and include the mysql-connector-java-8.0.25.jar in the referenced libraries, I can connect to the DB and retrieve data from the tables just fine, but after a few executions I no longer get output and it shows me the error "java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver".
Can anyone tell me why this is happening and how to fix this? There are no changes that I know of taking place in the Environment Variables (at least from what I can see in Windows path list) unless something is being overwritten somewhere or that it's a bug of some sort. Any advice would be greatly helpful, I've been unable to figure this out all day
This is what my ReportSystem.java looks like...
import java.sql.*;
public class ReportSystem
{
public static void main(String[] args)
{
//Test driver connection/registration
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ElectionDB","<username>","<password>");
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM ElectionDB.Votes");
int typeColumn = 1;
int districtColumn = 2;
//Output results line by line
while(result.next())
{
System.out.println(result.getString(typeColumn));
System.out.println(result.getString(districtColumn));
}
//Remember to close the connection
conn.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
My file structure as in the directory is as follows:
ReportSystem
>src > ReportSystem.java
> ReportSystem.class
>lib
>.vscode > settings.json
The JRE system library used is: [jdk-16.0.1]
The Referenced Libraries contains: [mysql-connector-java-8.0.25.jar]
Screenshot for context Project Setup in VS Code
I'm able to get result after running code 20 times continuously by clicking the run button. The only difference is the JDBC connection string, which is copied directly by right clicking the MYSQL connection:
So in my project, the connection string is like:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=username","<username>","<password>");
OR
You could try
String url="jdbc:mysql://localhost:3306/ElectionDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"
For your reference, OS Infomation:
MySQL: 8.0 // mysql-connector-java-8.0.25.jar
VSCode: 1.56.2 //
java.home: JDK16 // Debugger for Java: 0.33.1
So I've looked up a lot of different videos / tutorials. I've read through the MySQL connector J installation guide.. and I am still super confused!
I used the MySQL Installer to install all of the MySQL products.
Here is a pic of the installation.
This displays that connector J is installed, and its current location.
So I read that I need to 'set the classpath' -- these words literally haunt me at night x_x.. but it seems like something that really shouldn't be difficult. I went to my environment variables and noticed right away that there is nothing there currently called CLASSPATH or classpath or Classpath.. you get it. It's not there. So I created one, but I am positive that it's not right, or that is not what my problem is. Heres a pic: pic of current classpath
I've seen in a lot of videos that they say I 'must' download different external tools to get it to work, but that doesn't make sense to me, and the MySQL installation guide doesn't ever mention that, plus those videos are all possibly out dated.
I attempted to run this code:
import java.sql.*;
public class Main {
private static String connectionString = "jdbc:mysql://localhost:3306/test";
private static Connection connection;
private static Statement command;
private static ResultSet data;
public static void main(String[] args) {
// launch(args);
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionString);
command = connection.createStatement();
command.execute("INSERT INTO accounts VALUES (default, 'test1', 'password1', 2018-12-18)");
} catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
System.out.println("cnfe was thrown");
}catch(SQLException sqlE) {
sqlE.printStackTrace();
}
}
}
That returned two separate errors:
1
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.tjp.Main.main(Main.java:31)
2 -- this one occurs without the "forName" method.
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at com.tjp.Main.main(Main.java:33)
Any help will be greatly appreciated!! Thank you so much
Visit :
How to Add JARs to Project Build Paths in Eclipse (Java)
This question already has answers here:
Why do I get java.lang.AbstractMethodError when trying to load a blob in the db?
(14 answers)
Closed 5 years ago.
It's giving error
"Exception in thread "main" java.lang.AbstractMethodError:
Method com/mysql/jdbc/ServerPreparedStatement.setBinaryStream(ILjava/io/InputStream;)V is abstract
at com.mysql.jdbc.ServerPreparedStatement.setBinaryStream(ServerPreparedStatement.java)
at jdbc.demo.main(demo.java:21)".
I have added my code snippet as follow, can you please give me an insight what am I doing wrong?
import java.io.File;
import java.io.FileInputStream;
import java.sql.*;
public class demo{
public static void main(String args[]) throws Exception{
String url = "jdbc:mysql://localhost:3306/tutorial1";
String name = "root";
String pass = "rubyonrails$";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,name,pass);
String sql = "update binarylargeobject set resume=? where id=1";
PreparedStatement st = con.prepareStatement(sql);
File file = new File("C:\\Users\\USER\\Desktop\\vid.pdf");
FileInputStream input = new FileInputStream(file);
st.setBinaryStream(1,input);
st.executeUpdate();
}
}
AbstractMethodError that you happen to see in your case means that the MySQL JDBC Connector/J driver you use has not implemented the method setBinaryStream().
There is a nice article that I've come across when I tried to dig deeper here, you must take a look at it.
What is observed from the website above and various others is to use MySQL JDBC Connector/J version over 5.1.36 (a JDBC type 4 driver).
Hope you are able to solve your problem with the suggestions above.
This question already has an answer here:
Manipulating an Access database from Java without ODBC
(1 answer)
Closed 8 years ago.
I want to connect my database to msaccess 2007 using java, but I hear that the jdbc bridge is removed from java 8.
Please guide me that where is the problem in the following code.
import java.sql.*;
public class UserLogin
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// C:\\databaseFileName.accdb" - location of your database
String url = "JDBC:ODBC:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +
"C:\\Users\\Shakir\\Documents\\NetBeansProjects\\UserLogin\\me.accdb";
// specify url, username, pasword - make sure these are valid
Connection conn = DriverManager.getConnection(url);
System.out.println("Connection Succesfull");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
I hear that the jdbc bridge is removed from java 8.
Please guide me that where is the problem in the following code
The problem is precisely that the JDBC-ODBC Bridge has been removed from Java 8, so your code is trying to use a feature that is simply not available. Consider this approach instead:
Manipulating an Access database from Java without ODBC
ODBC Data Source in your Control Panel Settings and add new database with source.
This question already has answers here:
SQLException: No suitable driver found [duplicate]
(5 answers)
Closed 9 years ago.
I'm using putty to write a Java program that takes in SQL, but I'm getting this error:
No suitable driver found for jdbc:mysql://localhost:3306/Chinook
I'm not sure what's going wrong.
Here is my code:
import java.sql.*;
import java.io.IOException;
public class Q1{
public static void main (String args[]) throws IOException {
Connection conn=null;
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/Chinook";
System.setProperty(driver,"");
try{
conn = DriverManager.getConnection(url,"username","password");
System.out.println("Connected to the DB");
}
catch (SQLException ex){
System.out.println("SQLException:"+ ex.getMessage());
}
}
}
This line is pretty much self explanatory
: No suitable driver found for jdbc:mysql://localhost:3306/Chinook
You need to download a .jar file for jdbc i.e. jdbc.jar from HERE and then add it to your claspath ( project )
Also another thing is that you have jdbc driver but it could be the case that you are trying to access mysql withouth appropriate db for that type of database