Creating ODBC Data source java - java

I found this video which shows how to connect to access database :
http://www.youtube.com/watch?v=ujJ4H9RpC7c
My question is : Is it possible to create ODBC datasource programatically ?
or from command line or anything like that?
Thank you

It is not possible to create a windows ODBC DSN programmatically with pure Java. It is possible with C++ and other native approaches.
However, you can connect to an Access MDB file directly (via ODBC) using a JDBC URL of the form:
String jdbcUrl = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=c:/path/to/myaccessfile.mdb"
This way you do not need to have predefined DSN. You might also want to review the answers to this question:
How can I add a password to this JDBC:ODBC connection string that is trying to connect to an MS Access database

From the command line, you can use an utility named odbcconf.
I guess that if you need to do that programatically, you'll need to use WinAPI somehow.

Related

Accessing mysql database server from another computer?

we just started learning about sql statements and such, and right now i am totally confused on how to do this:
I need to access the database of another computer (we are using mysql workbench) using java codes
What i have done so far:
grant all privileges using '%'
i can already connect to the database (mysql) in my own computer (java codes and jdbc)
my problems are:
what are the steps do i need to do to be able to access the database from another computer, there are no direct answers in google...and now i am totally confused on how to do this because every site keep on telling me about cpanel, which i dont know how to have one...then this remote access that requires me to change my sql server configuration, i dont know how to find this...
CAN SOMEONE GIVE ME A STEP BY STEP SETTING/SETUP i need to do, to be able to access the database/connection from another computer? i only need the Normal steps, no need for safety/security setups and descriptive steps, i can do the rest of specific research on Google
SOLUTION
Hey Just configure that system IP in the JDBC connection call then you will be able top connect and retrieve the data in the Java Layer.
String DB_URL = "jdbc:mysql://localhost/EMP";
String USER = "username";
String PASS = "password";
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
Instead of "localhost" give your destination MySQL system IP address.
Just run the the mysql in one device then get that device Ip address, then on the other device use the http://IPADDRESS:8081/yourWebsite
for example instead of
instead of http://localhost:8081/yourWebsite = http://271.1.1.10:8081/yourWebsite
it will work from there, not need to configure anything. the other device will be using the same project and same database
note both devices should be on the same network

How to create and use and external derby Database

am getting started with Java database development and i wanted to know how to create and use database outside of the Derby server so for example instead of using this as a host url :
String host = "jdbc:derby://localhost:1527/Employees";
i want to use something like this :
String host = "jdbc:derby://c:/MyDb/Employees";
i dunno if that is correct or not, but this is what am trying to do cause i don't know how to use the database in the localhost after exporting the executable jar for the application
thank you
If you want to load database from the file system, you use:
String host = "jdbc:derby:c:/MyDb/Employees";
See the examples.

How to bundle an Access database with a Java application

Hi recently I had created a Java application included database with Microsoft Access. I had wrapped it to jar file using eclipse. I pass the jar file to my friend to try to use it. But my friend told me that that is no database connection. How can i include the microsoft access in the jar file. Which mean when my friend double click the jar file it will auto configure the microsoft access database? is that possible?
Actually you don't need a package access with your application, since all versions of windows include a copy of the jet database engine. In other words you can use windows scripting to open up an access database on a windows computer without having installed ms access at all. The component or database engine part is all that you need to open in read those access database files.
Here a windows script to open a access database and write a column out to a text file:
Set dbEng = CreateObject("DAO.DBEngine.36")
strMdbFile = "C:\Docs\MultiSelect.mdb"
Set db = dbEng.OpenDatabase(strMdbFile)
strQuery = "select * from contacts"
Set rs = db.OpenRecordset(strQuery)
rs.MoveFirst
If rs.EOF = True Then
Quit
End If
strTextOut = "C:\t5.txt"
Set fs = Wscript.CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(strTextOut, 2, True)
'2 = write, 1 = read
Do While rs.EOF = False
strOutText = rs("LastName")
ts.Writeline strOutText
rs.MoveNext
Loop
ts.Close
rs.Close
So there is no requirement to package or install the jet database engine width your application since that component is available in windows.
It's probably not too important, but I should point out that there's a distinct difference between ms access the developer's tool to let you write code, build forms, and build reports, and that of the database engine that access developers, vb6, vb.net and in your case Java can use to read an access database file. You don't need ms-access installed here, but only the database engine. That database engine is included with every copy of windows.
First of all: does your friend has a MS Access runtime, maybe he needs it? Does he configured the Microsoft Access ODBC Data Source?
Please take a look at following links: Jackcess - java library for reading and writing to MS Access file (no runtime needed), SQLLite - another file RDMS. Please consider to use the Apache Derby project - you can embed it into your application what gives you some advantages but requires more work. I don't know what app you want to implement so you will have to make a choice by yourself ;).
I don't think so. Your friend'll have to configure the odbc connection from the control panel. Also, I would suggest using a DB like derby or MySql than MS Access as the odbc driver is considered to have many bugs. Also using an embedded DB like derby wouldn't require your friend to configure anything at all.

Java MS Access Database Connection without Admin rights

For a JAVA project I am working on there is a requirement that I use a Microsoft Access Database running on a computer where I do not have administrative priveleges.
So, I have the Access DB file all ready, and I started doing some research - but everywhere I look mentions the need to use the Admin Tools (or Control Panel) to "setup an ODBC connection", sadly it looks like I don't have access to any of these OS level configurations.
Is there any alternative I could use to access the database without the need to start configuring the Operating System (running Windows XP).
Any help would be appreciated.
Thanks,
Jackcess is a pure Java library for reading from and writing to MS Access databases.
The connction URL may contain the full ODBC information:
Connection con = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/bd1.mdb"
);
No access to the control panel is necessary.

jdbc to access complete database

i want what should be given in DriverManager.getConnection() when i want to access all tables from ms-access
Have a look at DriverManager.getConnection(String url, Properties info). What problem doesn't this solve?
I think you want to connect to an MS Access DB without creating a DSN.
In any case this works fine with acess everytime.
DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/data/data.MDB","","");
Change the DBQ argument to your MDB file.

Categories