JAVA jdbc blob? [duplicate] - 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.

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

IntelliJ - Problem connecting to PostgreSQL

I am new to PostgreSQL (I normally use other database engines), and I also do not use Java often.
My Problem is that I get the following exception:
java.sql.SQLException: No suitable driver found for DATABASE_NAME
java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at
java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
I followed this tutorial: http://www.postgresqltutorial.com/postgresql-jdbc/connecting-to-postgresql-database/ and added postgresql-42.2.5.jar as a library.
The problem is that adding the driver as a library, as can be seen in the screenshot, has no effect.
So my question is: how do I connect to a PostgreSQL database using Java and the latest IntelliJ?
Any help would be appreciated.
UPDATE 1:
UPDATE 2:
Since the code has been requested: I have replaced the original code by a minimal code that will cause the error:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class IngestData
{
protected static String url;
protected static final String user = "user";
protected static final String password = "password";
public static void main(String[] args)
{
Connection connection = null;
url = args[args.length-1];
try
{
connection = DriverManager.getConnection(url, user, password);
System.out.println("SUCCESS");
} catch (SQLException e)
{
System.out.println("ERROR");
e.printStackTrace();
}
}
}
The console output is:
ERROR
java.sql.SQLException: No suitable driver found for http://127.0.0.1:10282/db01617792
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at IngestData.main(IngestData.java:17)
Process finished with exit code 0
Here is the link to the git repository containing the code:
https://github.com/ka6555/StackOverflow-Postgresql-Problem.git
UPDATE 3:
I found the error:
I need to change
protected static String url;
to
protected static String url = "jdbc:postgresql://";
and
url = args[args.length-1];
to
url += args[args.length-1];
While this solves my original problem, the program is now stuck executing the following line:
connection = DriverManager.getConnection(url, user, password);
There is no error but the program will simply run like with an endless loop never going beyond this code line.
UPDATE 4:
I have fixed all problems now.
It seems like you are missing the postgres jar file in your project dependencies.
Open the Project Structure (Ctrl+Alt+Shift+S on Windows)
Select modules / dependencies tab
You should see something like the following:
If the postgres dependency is missing:
Klick on the + sign on the right side of the screenshot
Choose Library/Project Library and your postgres jar file
Your code should now run. Let me know if it helps.
Note: Please provide your minmal working code on GitHub for a quicker response.
The main problem was that I used a command line parameter as the database url without prefixing it with jdbc:postgresql://. Additionally, I had to reinstall postgresql because of some odd behavior I could not figure out the reason for.
This is the message you get when the URL syntax is incorrect.
This is the requirement.

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.

Error in Java SQL [duplicate]

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

Categories

Resources