I am trying to connect to MS-Access using JDBC:ODBC:
public boolean connectToAccess(String accessFilePath) {
//Get connection to database
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
myConnection = DriverManager.getConnection("jdbc: odbc: driver={Microsoft Access Driver (*.mdb)};DBQ=" + accessFilePath);
} catch (Exception ex) {
System.out.println(ex);
return false;
}
return true;
}
I get the error:
"No suitable driver found for jdbc: odbc: driver={Microsoft Access Driver (*.mdb)};DBQ=file.mdb"
Why?
Can you suggest another way of reading access files in Java?
Take those spaces out of the connection string and see if that helps. I'd also recommend printing the stack trace.
public boolean connectToAccess(String accessFilePath) {
//Get connection to database
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
myConnection = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + accessFilePath);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
The other way to read Access files is using the Jackcess library.
Try to create a DSN for the Access database from odbcad32. Another issue may be, the driver is not installed on your machine or you have insufficient privileges.
Related
I am having a problem with the UCanAccess driver namely that when I attempt to connect to a database using the following code
public static void main(String[] args) {
//establish connection with database in order to execute sql queries
try {
conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\IT_PAT_Program_LOCALONLY\\IT_Pat_Database.accdb;showschema=true");
System.out.println("Connection Established");
} catch (SQLException ex) {
System.out.println("Could Not Connect to database\n"+ex);
}
//closes the database connection at program shutdown
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
conn.close();
System.out.println("Shutdown Succesful");
} catch (SQLException ex) {
System.out.println("An exception occured\n"+ex);
}
}
});
}
I am met with the following error:
Could Not Connect to database
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.0 invalid authorization specification - not found: Admin
The database is also connected as a persistence unit however since I'm unaware of any way to make use of that from within the code (Google has been no help) this method seems to be my only option.
We can not see where you are doing the authorization in your code.
user ?
password ?
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn=DriverManager.getConnection(
"jdbc:ucanaccess://<mdb or accdb file path>",user, password);
public void connect()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:msql://23.249.225.135:3306/";
Connection conn = DriverManager.getConnection(url,"s****d","1*****-");
System.out.println("DB works");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
I have the JDBC driver in the /lib folder in my class path.
Keep getting:
No suitable driver found for jdbc:msql://23.249.225.135:3306/
Any ideas?
You've misspelled mysql in the url is msql.
The version of your mysql database doesn't support the version of your driver or vice-versa.
Look for the compatibility matrix here: Chapter 2 Connector/J Versions
I'm trying to connect to my sql database on localhost.
With the following code :
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(ClassNotFoundException e) {
System.out.println("JdbcOdbcDriver wasn't found");
e.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/testBase", "root", "");
Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("");
}
catch(SQLException e){ System.out.println(e); }
finally {
if(connection != null) {
try { connection.close(); } catch (SQLException e) { e.printStackTrace(); }
}
}
I have 2 questions related to this code :
I am using the sun.jdbc.odbc.JdbcOdbcDriver driver because it seem that it was the only available on my system, however I've read that it's a windows component... Is there a way to make it platform independent (or is it already) ?
The second catch outputs a
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost/
Which is quite annoying and I don't seem to be able to find a way to work...
Thanks for your help !
When you generate jar, you can include all its dependencies.
So, you can use any driver you need and include it when you generate jar for the application.
Now you're cross platform.
About the question #2, that's because you don't have the mysqLconnector at lib folder.
You can download it here: http://dev.mysql.com/downloads/connector/j/
Just include it, if you're using IDE, you can look for a option "include jar/folder" and add it, then, try again; if you aren't in a IDE ... you have to set it on classpath.
the last days I was trying to learn how to access mySQL databases via Java.
I am able to load the driver and get a connection to the database ( at least I think so, since I don't get an exception there..)
the code is:
import java.sql.*;
public class test
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("driver loaded...");
}
catch(ClassNotFoundException e){
System.out.println("Error in loading the driver..."+e);
System.exit(0);
}
try
{
Connection dbConnection= DriverManager.getConnection("jdbc:odbc:test","root","password");
System.out.println("Connection successful...");
Statement stmt = dbConntection.createStatement();
stmt.executeUpdate("create table Accounts ( name char(20) )");
}
catch(SQLException e)
{
System.out.println("database-ConnectionError: "+e);
System.exit(0);
}
}
}
When I execute it, it says:
driver loaded... Connection successful...
database-ConnectionError: java.sql.SQLException: [MySQL][ODBC 5.2(w) Driver][mysqld-5.5.31]No database selected
I really don't know the problem, because I thought the database is selected during the "getConnection" process....
I tried to select a database by adding this line:
stmt.executeUpdate("use test;");
after creating the Statement.
unfortunately it didn't work because I got another exception which said I should check on the syntax. I don't understand that either because in my commandline it works just fine...
I don't know if it is possible to use these type of commands via Java so if it isn't, please forgive my mistake.
I hope you can help me and I didn't miss the solution during my own search!
Already Thanks to all who reply and use their time on my problems!
ps. If I forgot to point out some important infos ( I don't think i did) please ask:)
edit: I also tried to create a new database during runtime
stmt.executeUpdate("CREATE DATABASE test;");
this actually works, but the database won't be selected either...
Before you can add a table, you first have to select a database.
you can create a new database with:
CREATE DATABASE database_name
you can connect to a specific database with:
String url = "jdbc:mysql://localhost/databasename";
String username = "test";
String password = "test";
Connection connection = DriverManager.getConnection(url, username, password);
Firstly, I am considering my answer to show you another better way for connection with MySQL Database, it's much easier and less nu-expected Exception(s).
You need to do some steps:
Download Connector/J and add it to your class path(if you are using an IDE there is add the .jar to the library, or there is many tuts on YouTube).
Create your database in your MySQL program.
See this example below example below I made for you demonstrates how to connect and execute queries on MySQL :
import java.sql.*;
public class MySqlConnection {
private String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
private String MYSQL_URL = "jdbc:mysql://localhost:3306/test";
private Connection con;
private Statement st;
private ResultSet rs;
public MySqlConnection() {
try {
Class.forName(MYSQL_DRIVER);
System.out.println("Class Loaded....");
con = DriverManager.getConnection(MYSQL_URL,"","");
System.out.println("Connected to the database....");
st = con.createStatement();
int c =st.executeUpdate("CREATE TABLE Accounts (Name VARCHAR(30))");
System.out.println("Table have been created.");
System.out.println(c+" Row(s) have been affected");
con.close();
} catch(ClassNotFoundException ex) {
System.out.println("ClassNotFoundException:\n"+ex.toString());
ex.printStackTrace();
} catch(SQLException ex) {
System.out.println("SQLException:\n"+ex.toString());
ex.printStackTrace();
}
}
public static void main(String...args) {
new MySqlConnection();
}
}
Here is your updated example, which works for me.
public static void main(String[] args) throws InstantiationException,
IllegalAccessException {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("driver loaded...");
} catch (ClassNotFoundException e) {
System.out.println("Error in loading the driver..." + e);
System.exit(0);
}
try {
Connection dbConnection = DriverManager
.getConnection("jdbc:mysql://localhost/test?user=root&password=password");
System.out.println("Connection successful...");
Statement stmt = dbConnection.createStatement();
stmt.executeUpdate("create table Accounts ( name char(20) )");
} catch (SQLException e) {
System.out.println("database-ConnectionError: " + e);
System.exit(0);
}
}
Make sure you have added a proper mysql-connector to your build path. I used the: mysql-connector-java-5.1.24-bin.jar
static final String DB_URL = "jdbc:mysql://localhost:3306/sys";
Use database name in the URL.
It worked for me
i'm trying to connect with jdbc to a specific schema in mysql server, the schema name is mining
when i'm trying to connect i get access to all the DB and therefore the executing statements apply to all the schemes in my db instead only to "mining"
this is how i establish a connection:
public class Mining {
Connection conn;
void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
this.conn = DriverManager.getConnection(
"jdbc:mysql://localhost/?currentSchema=mining","admin","admin" );
//I ALSO TRIED THIS: "jdbc:mysql://localhost/mining","admin","admin"
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
any thoughts?
thanks!
try,
private Connection connect = DriverManager
.getConnection("jdbc:mysql://localhost/mining?"
+ "user=admin&password=admin");
String databaseURL = jdbc:mysql://hostName:portNumber/schemaName?
Connection connectionObj = DriverManager.getConnection(databaseURL,userName,password);