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.
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
This question already has answers here:
No suitable driver found for jdbc mysql? [duplicate]
(6 answers)
Closed 3 years ago.
I've successfully connected to the database through intellij, but I can't figure out how to run sql statements in the main. I have a database class that can run all of the SQL statements but can't get it to work as if I were to write functions that can insert and delete.
public static void main (String[] args)
{
Connection conn;
{
try {
conn = DriverManager.getConnection("jdbc:mysql://35.247.87.196:4406","username","password");
Statement stmt=conn.createStatement();
String strselect="select * from EmployeeTable";
System.out.println("The sql statement is: "+strselect+"\n");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
and I get the exception
java.sql.SQLException: No suitable driver found for jdbc:mysql://35.247.87.196:3306
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at DataGenerator.main(DataGenerator.java:30)
Out of the box, java doesn't support any databases. You need to plug in drivers for database engines in order to make it work.
How do you do that? Just make sure the 'JDBC driver' is on the classpath. There is no need to do anything but that. So, find the JDBC driver for your db engine and ensure that that jar is on your classpath when you run this.
As a side note, to help you debug things in the future: Exceptions have at least 4 interesting bits of info in them (the type, the message, the trace, and the causal chain). Printing all that is hard, so don't try. Either [A] handle the exception (and printing it or logging it is NOT handling it!) or just throw it onwards. your main method should generally have throws Exception tacked on. It also saves a huge amount of written code (no need for a bunch of try/catch blocks with a crappy 'print the error' code in the catch block!
So, definitely, delete the try, the catch, the print, and add 'throws Exception' to your method instead.
I have to create a very simple batch Java application (an application that run into shell) and that perform some query on an Oracle database using JDBC and I never done it.
I am following this tutorial: http://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html
So I have done in this way to allocate a new Connection object for my application:
import java.sql.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World !!!");
String partitaIVA = args[0];
String nomePDF = args[1];
Connection conn = null;
Statement stmt = null;
try {
// Step 1: Allocate a database "Connection" object
conn = DriverManager.getConnection("jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB
} catch(SQLException ex) {
ex.printStackTrace();
}
}
}
The problem is that when I run the application it seems that the SQLException is thrown because enter into the catch block and print the following error message in the console:
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Main.main(Main.java:16)
Why? What cause this problem and how can I fix this issue? What am I missing?
Tnx
You need to have an appropriate ojdbc.jar in you classpath. E.g. see Oracle JDBC ojdbc6 Jar as a Maven Dependency
The problem should be that the linked tutorial describes how to connect to a Mysql, but you're trying to connect to an oracle - therefore you need the oracle driver in your classpath.
How about following oracle's documentation for its Driver. http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html
In the above code, you didn't registered the driver class.
Class.forName ("oracle.jdbc.OracleDriver");
If you are following instructions from the given link.
In chapter 2.2 you have instructions how to instal MySql drivers.
If you are using an Oracle database, then you'll need to instal Oracle JDBC drivers.
If you don't want affect your JDK installation like in tutorial, you can load driver dynamicaly
I am trying to create a program that will read a database and output it. I have followed the tutorial at http://www.codeproject.com/Articles/35018/Access-MS-Access-Databases-from-Java. But when I run my program, nothing happens. Not even errors...
I probably have missed something, but I don't know what it could be. Here's my code:
import java.sql.*;
public class ReadDB {
public static void ReadDB() {
try{
//Source: http://www.codeproject.com/Articles/35018/Access-MS-Access-Databases-from-Java
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=ratingdb.accdb;";
Connection conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement();
//Read Table
String selTable = "SELECT * FROM RATINGS";
s.execute(selTable);
ResultSet rs = s.getResultSet();
while((rs!=null) && (rs.next()))
{
System.out.println(rs.getString(1) + " : " + rs.getString(2));
}
s.close();
conn.close();
} catch(Exception e){
System.out.println ("Unable to connect to the database");
System.out.println ("Exception: " + e.getMessage());
}
}
}
After Black Panther's comment, I got this error:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x13f4 Thread 0x1204 DBC 0x42170d4
EDIT: A new error occured: Exception: [Microsoft][ODBC Microsoft Access Driver] Not a valid file name. Does this mean that the driver I use does not exist?
Even if it throws the exceptions you wouldn't be able to see them because you have handled the errors in a wrong way. You shouldn't just catch the exceptions and move on. That defies the whole concept of exception handling.
You should do something like this for your exceptions to be printed in the console.
try {
//code that throws exceptions
} catch(Exception e) {
e.printStackTrace(); //prints the error to the console and you missed it
}
EDIT:
It seems you are having some permissions issue.
An Excerpt from the site http://support.sas.com/kb/40/228.html
This problem occurs for several reasons, including not having permissions on an ODBC registry key. In such a case, change the permissions on the registry key as follows:
Start the registry editor using the regedit command:
select Start ► Run and enter regedit.
If your SAS PC Files Server is on a 64-bit machine, expand the following key:
HKEY_LOCAL_MACHINE ► SOFTWARE ► WOW6432NODE ► ODBC.
If your SAS PC Files Server is on a 32-bit machine, expand the following key:
HKEY_LOCAL_MACHINE ► SOFTWARE ► ODBC.
Right-click the ODBC folder and select Permissions.
Make sure that the logon ID that is running the SAS process has full control.
The problem might also occur due to older ODBC drivers from Microsoft, particularly from Office 2007.
To install the newer ODBC drivers, go to Microsoft Access Database Engine 2010 Redistributable.
If you have 32-bit Microsoft Office, download the AccessDatabaseEngine.exe file.
Download the other ODBC driver only if you have 64-bit version of Microsoft Office.
If you are trying to create an .mdb file and use it then do this
go to
File -> Options -> General, and set the Default File Format to Access 2002-2003
And change your database URL to
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=ratingdb.mdb;";
to use the .mdb file.
To use an .accdb file try doing this,
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=ratingdb.accdb;";
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