I have created a CRUD Application the connecting method of the application is given below. I have tested it on my computer and is working fine, but while tesing on another computer where MS Access is not installed it is throwing NullPointerException.
So what should I do in order to rectify this problem? Are there any libraries for connecting to .mdb files?
These should also run on Linux. I can convert the .mdb file into Open Office Base if Libraries are available...
void DoConnect()
{
try{
String current = new java.io.File( "." ).getCanonicalPath();
current+="\\DataBases\\Quiz.mdb";
String host = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+current+";";
String uName = "";
String uPass = "";
con=new Connection[Size];
stmt=new Statement[Size];
for(int i=0;i<Size;i++)
{
con[i]=DriverManager.getConnection(host, uName, uPass);
stmt[i]=con[i].createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
ts=stmt[0].executeQuery("SELECT * FROM Quiz");
ts.first();
rs=stmt[1].executeQuery("SELECT ANSW FROM Quiz");
System.out.print(rs.getString("STM1"));
}catch (IOException | SQLException err) {
}
}
Are there any libraries for connecting to mdb files ?
Yes, there are. The Jet database engine is included with Windows, but only a 32-bit version is available. If your application is running as a 64-bit process then you'll need to have the 64-bit version of the Access Database Engine (a.k.a. ACE) installed on the machine. You can download the Access Database Engine here:
http://www.microsoft.com/en-us/download/details.aspx?id=13255
Note that to use the Access Database Engine you may have to tweak your connection string to something like...
String host = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+current+";";
...and if that doesn't work on 32-bit machines without the Access Database Engine installed (i.e., machines with just the Jet database engine) then your code may have to
try the Jet connection string first (i.e., your original connection string), and if that fails then
try the ACE connection string (i.e., the one in this answer).
Connecting to Access is not an easy task if you don't have access installed on all client computers .
Moreover Access databases are huge in size .
So I am currently using H2 database which is very easy to use .
Regarding the size , After I copied a 140 Mb Access database to H2 the file was ONLY 732 Kb !
More info can be found here
Related
I want to read my cell values (in Excel file) using Java. In the process I am using ODBC to give the Excel file as my data source. I wrote the user, password, URL and all properties code in a separate .properties file, for my Java program to read and connect to the ODBC. It works fine when I run it on my local machine, but fails to when I run it on server. The error I receive is:
[Microsoft][ODBC Driver Manager] Data source name not found and no
default driver specified
The server is a 64-bit machine working on Windows 2012. I checked my admin tools in control panel (of server). It has two ODBC's, one for 32-bit, the other 64-bit.
What's the reason for my code running only on my local machine and not on the server?
Is it because of there being two tools, and the program being confused on which one to look for?
myDB=jdbc:odbc:Driver={Microsoft Excel Driver (*.xls,
*.xlsx)};DBQ=c:/data.xls;READONLY=true;DriverID=22;
this is the code I wrote in the .properties file. user & pwd fields are blank.
Is there something I am missing or is it something else?
I think that the best bet here is to create an ODBC DSN from the control panel and then try to connect through it :
java.sql.Connection c = java.sql.DriverManager.getConnection( "jdbc:odbc:exsh", "", "" );
Whereas “exsh” is the name of DSN which points at a spreadsheet file which is going to be processed.
Without the DSN the connection string should be :
"jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=c:/data.xls"
Remember that since you are on a 64bit system you probably need to install the 64-Bit OLEDB Provider for ODBC (MSDASQL) .
I am using eclipse for JAVA development in Windows 7 and I put my project in D:\workspace.
The following code is trying to connect to a SQLite database, While the jdbc address is jdbc:sqlite:sample.db, where is the location that JAVA is looking for sample.db ?
public class Sample{
public static void main(String[] args) throws ClassNotFoundException{
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try{
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
System.out.println("I got connection.");
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
I have see there is example for using absolute paths, But I'd like to know where to put the DB file while I use Relative path.
Also, Does the file location be difference if I put the Class in some package ?
By default the db file will be created in root of project directory.
Other path formats you can use along with sql lite are:
jdbc:sqlite://dirA/dirB/dbfile
jdbc:sqlite:/DRIVE:/dirA/dirB/dbfile
jdbc:sqlite:///COMPUTERNAME/shareA/dirB/dbfile
SQLite supports on-memory database management, which does not create any database files. To use a memory database in your Java code, get the database connection as follows:
Connection connection = DriverManager.getConnection("jdbc:sqlite::memory:");
If you want physical SQLite database you need to create SQLite database using SQLite manager or Mozilla or chrome add-on plugin.And give path like that
Windows
Connection connection = DriverManager.getConnection("jdbc:sqlite:C:/work/mydatabase.db");
Linux, Mac OS X
Connection connection = DriverManager.getConnection("jdbc:sqlite:/home/leo/work/mydatabase.db");
Hi I have the below code to connect to MS Access database on Windows 7 OS. I have changed the Data Source short cut to point to 64bit odbc then 32 bit. But still getting the error as
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at TestDBConnection.main(TestDBConnection.java:21)
And my code is :
import java.sql.Connection;
import java.sql.DriverManager;
public class TestDBConnection {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
System.out.println("filename");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database =
"jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\Test\\Tests.mdb";
Connection conn = DriverManager.getConnection(database, "", "");
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
How ever I have SQL Workbench tool through which I can connect to it but not through java code.
Please need help badly as I am struggling with this from past 3 hours searching on Google.
If your Java app is running in a 64-bit Java Virtual Machine (JVM) then DRIVER={Microsoft Access Driver (*.mdb)} is not going to work because there is no 64-bit version of the Jet database engine. You can...
Download and install the 64-bit version of the Microsoft Access Database Engine from here, and then use DRIVER={Microsoft Access Driver (*.mdb, *.accdb)} in your code.
... or ...
Run your Java app in a 32-bit JVM and continue to use the existing DRIVER= string. The related answer here might prove helpful if you choose this option.
... or ...
Use the UCanAccess JDBC driver for Access databases. It is a free, open-source, pure Java implementation so it works on both 32-bit and 64-bit systems, both Windows and non-Windows. It also works with Java 8 (which has dropped the JDBC-ODBC Bridge). For more details, see:
Manipulating an Access database from Java without ODBC
You can install the 64 ODBC drivers for Access available from Microsoft
http://www.microsoft.com/en-us/download/details.aspx?id=13255
1) you will have to configure System dsn (Driver Microsoft Access Driver(.mdb,.accdb))
2) link .mdb database in above configuration
and write below code.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:systemdsnname";
Connection conn = DriverManager.getConnection(database, "", "");
Attempting to connect from Java 6 console app to Microsoft SQL Server 2008 R2 on an Microsoft Windows Server 2008 R2 64bit system via an ODBC System DSN using SQL Server Native Client 10.0. The following source code:
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String srcURL = "jdbc:odbc:FOO";
if (dbc == null)
{
dbc = DriverManager.getConnection(srcURL);
dbc.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
}
else
{
dbc.close();
dbc = DriverManager.getConnection(srcURL);
dbc.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
}
}
catch (ClassNotFoundException cx)
{
System.out.println("class not found");
}
catch (SQLException sx)
{
System.out.println("SQL Exception: " + sx);
log.info("SQL Exception: " + sx);
}
Throws error
java.sql.exception [Microsoft] [ODBC Driver Manager] invalid string buffer length
Maddeningly, the same code, with and ODBC System DSN configured in the exact same way, WORKS with MS Server 2008 32bit (non-R2) and MS SQL Server 2008 R2. The Microsoft ODBC driver dlls between the two systems are different versions, 6.0.xxxx vs 6.1.xxxx, which I suspect is the culprit.
Yeah the ODBC Manager version should be the problem. Below is the problem I ran into and the solution I thought of, hope it might be of help to someone else too.
When trying to run query against System ODBC DSN (MS Access .mdb file) from an app deployed to Jboss 4.x, I get same error: "SQL state [S1090]; error code [0]; [Microsoft][ODBC Driver Manager] Invalid string or buffer length" in Windows Server R2.
I reproduced the same error on 2 different Windows Server R2 machines. On Windows Server Standard (I guess R1) and Windows 7 Professional x64 the problem is not reproducible.
Further more, on the same Windows Server R2 when trying to connect directly (from a standalone app), I don't get this problem. If the application won't connect/detect the data source, you'd get an error saying that there is no such DSN name or it's not found. The same error message is thrown when trying to send an empty query to ODBC Data Source (registered Data Source Name - DSN). So i guess the ODBC gets an empty query which tries to execute against the DS and the result is: Invalid string or buffer length.
Since I can read the .mdb file registered as a ODBC DS with given DSN, and I don't get this error when querying it from the standalone app, I'm going to make a standalone app that will read the .mdb file through ODBC and write its content to a .csv file, which the Jboss apps will read.
If anyone finds a better solution, please let me know.
A issue of JDBC-ODBC bridge native codes.
The native codes invoke ODBC function SQLGetData with a invalid BufferLength parameter.
This problem happens on 64bit jvm only. As I know, it can happen on all jdks: since 1.0 to 1.7.
The BufferLength is a 8 bytes SQLLEN parameter. The high 4 bytes is left uninitialized in 64bit jvm, that the root cause.
Currently there is no workaround, and Oracle refuse to fix this issue, even though I reported it via oracle metalink website.
this is a java bug, upgrade to java 1.7.70 at least.
I want to write a Java program that automates the work that the ODBC Data Source Administrator does in Windows.
That is, given an ODBC connection name and a path to the database on the hard drive, I want it to create the connection.
I really have no idea where to even begin with this. I looked at this but it said it was for C and I don't think that's very helpful. If anyone could point me in the right direction for this at all, I would appreciate it.
(I realize this question is REALLY vague, but that's all the information I was given.)
The answer to the question is that you don't need a registered DSN.
Here is an example of using a ODBC connection (not JDBC) from Java, using the system ODBC driver. Instead of editing the registry to create a registered DSN, your alternative is to use a un-registered DSN, like so:
Driver=sun.jdbc.odbc.JdbcOdbcDriver
Source=jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Dir/DB/MyDB.mdb;
Or, using SQL Server:
Driver=sun.jdbc.odbc.JdbcOdbcDriver
Source=jdbc:odbc:Driver={SQL Server};SERVER=127.1;DATABASE=MyDB;UID=sa;PWD=mypass
All ODBC configuration is in Windows registry or odbc.ini in Linux (I haven't used ODBC on other platforms). At first you must create such configuration using ODBC manager, then check what was saved in configuration and write program that do the same. If you work with Windows 32 bit, then check registry at HKEY_LOCAL_MACHINE\SOFTWARE\ODBC.
Windows 64 bit have different configurations for 32 bit apps and 64 bit apps (just look for odbc.ini string in registry).
I think Java is not the best language to change something in Windows registry, but with Java you can create .reg text file that can be imported by regedit.exe, or you can use other language like Python with win32 extensions (Active Python has it by default).
You will want to look into using JDBC.
String driver ="sun.jdbc.odbc.JdbcOdbcDriver"
String url = "jdbc:odbc:Driver={Microsoft Access Text Driver (*.txt, *.csv)};DBQ=C:/CSVFolder
query = select * from myfile.csv
Check this one out..
Java Database Connectivity (JDBC) supports ODBC-based databases and provides a independent database.
Connection connection = null;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
connection = DriverManager.getConnection("connection string", "userName", "password" );
} catch (SQLException e) {
}
return connection;
I've never had to connect to MS SQL Server before. I've always used DB2 or Derby, MYSQL and everything was always the same for creating a connection. This is what I had to do for SQL Server.
private final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
url="jdbc:odbc:;DRIVER={SQL Server};SERVER="+server+","+port+";DATABASE="+dbName;
connection = DriverManager.getConnection(url,user,password);