Error in Java SQL [duplicate] - java

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

Related

Can not connect to sql database through java [duplicate]

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.

Java JDBC Driver stops working after initial execution [duplicate]

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

How to fix: No suitable driver found for jdbc:sqlserver [duplicate]

This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 3 years ago.
I am trying to connect to a MS SQL Server database running on the same machine as the application but I keep getting the error:
No suitable driver found for
jdbc:sqlserver://localhost:1433;database=SQLTEST;user=TEST;password=1234567890;encrypt=true;trustServerCertificate=false;loginTimeout=30;
I am running MS SQL Server 2017 and JDK1.8.x.
The first thing I did was creating a user in the following way in SSMS using T-SQL:
USE SQLTEST
CREATE ROLE TEST
GRANT SELECT, INSERT ON SCHEMA :: [dbo] TO TEST
CREATE LOGIN TEST_LOGIN WITH PASSWORD = '1234567890'
CREATE USER USER_TEST FROM LOGIN TEST_LOGIN
ALTER ROLE TEST ADD MEMBER USER_TEST
I then used Maven to add the driver to my project in the pom.xml file:
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.2.2.jre8</version>
</dependency>
Followed by adding and altering the example code given by Microsoft (note that this should become a JAX-RS REST-API, that is why the annotations are there):
#Path("t2")
public class T2 {
#GET
public String show() {
String r = "";
String connectionUrl =
"jdbc:sqlserver://localhost:1433;"
+ "database=SQLTEST;"
+ "user=TEST;"
+ "password=1234567890;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
+ "loginTimeout=30;";
try (Connection connection = DriverManager.getConnection(connectionUrl);) {
// Code here.
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
r = e.getMessage();
}
return r;
}
}
Running this results in the error as given in the start of this post. Did I forget something or did I do something wrong?
Looks similar to this issue:
java.sql.SQLException: No suitable driver found for jdbc:sqlserver
Doesnt look like you've got Class.forName("xxxx"); before your DriverManager.getConnection section in the try statement.

JAVA jdbc blob? [duplicate]

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.

Microsoft Access 2007 connectivity in java 8 [duplicate]

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.

Categories

Resources