I need to make an application in Java , which needs to use SQL database . I wil provide the users with the database file , but that won't run because to setup the database connection we first need to add a User DSN in Windows . Is there a way to add it( User DSN ) automatically when the applcation installs ?
-
With "SQL database", do you actually mean "Microsoft SQL Server"? There are a lot of different SQL database servers, the exact answer may depend on the vendor used. – BalusC Aug 07 '10 at 21:17
-
Why do you need an ODBC DSN in first place? Using a JDBC driver is the most straightforward choice. – BalusC Aug 07 '10 at 21:52
-
@BalusC , I have no experience in working with SQL alongwith a Java application . I once used Acess to create a database and I had to create a DSN connection , so I though it might be required , I am sorry for the mistake . I just need an app that can access database (SQL is preffered ) when installed on any machine , without the need to make special efforts by user like creating DSN. Can you help me ? – Gaurav Aug 07 '10 at 22:05
-
2You may find this [mini tutorial](http://stackoverflow.com/questions/2839321/java-connectivity-with-mysql/2840358#2840358) useful. To distribute your application, just include the JAR file in the classpath. – BalusC Aug 07 '10 at 22:18
3 Answers
Gaurav, the canonical way to do these things is by using JDBC. They provide a uniform, simple interface.

- 110,348
- 25
- 193
- 263
I wil provide the users with the database file , but that won't run because to setup the database connection we first need to add a User DSN in Windows.
I don't think that'll work unless there's a MySQL server listening on port 3306.
User DSN in Windows is not the way to go. (Nothing "platform independent" about that.)
The Java way to connect to any database is JDBC. Here's an example showing how to do it with MySQL. You'll need the Connector-J JAR in your CLASSPATH, of course.

- 305,152
- 44
- 369
- 561
If you're using Java and JDBC, your database connection has a URL.
It takes the basic form
jdbc:mysql://hostname:port/database
They're also going to need to know the JDBC driver classname for the database you're installing. So, you'll need to prompt them for those two things.
Your users are going to need to know those things about their data bases to use your app.
You could, as you suggest, use a JDBC / ODBC connector and set up an ODBC Dsname. But if you want good performance, you're probably better off using native JDBC. You can include the class library for the driver in your application class path and install it yourself.
The driver is called Connector / J for mySQL, and it works well.

- 103,626
- 17
- 118
- 172