Setting up GlassFish server - java

I'm developing a JSP project with NetBeans on a GlassFish server. The project uses a MS Access file as database. Where do I need to put the MDB file so JSP class can find it at runtime ?
My code
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
location = loc.getAbsolutePath().substring(0, loc.getAbsolutePath().length() - 2);
String filename = location + "\\myDB.mdb";
System.out.print(filename);
String database;
database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database += filename.trim() + ";DriverID=22;READONLY=true}";
c = (DBM) DriverManager.getConnection(database, "", "");

You should need to setup DSN which get connection. See below steps;
Open Windows' ODBC Data Source Administrator as follows:choose Start > Settings > Control Panel > Administrative Tools > Data Sources.
In the ODBC Data Source Administrator dialog box, click the System
DSN tab.
Click Add to add a new DSN to the list.
Scroll down and select the Microsoft Access (.MDB) driver.
Type in the name "yourDataSourceName" (no quotes, but leave the cases the same)
for the Data Source Name
Click CREATE and select a file to save the database to (I chose
"d:\java\test.mdb") - this creates a new blank MS Access
database.
Click "ok" all the way out.

It doesn't really matter where you put the MDB file on the machine. The important part is that Java requires JDBC, the Java world equivalent to ODBC. (not exactly equivalent but you get me drift). But MS Access doesn't support JDBC, only ODBC. You need to set up ODBC as Sai said. Sun a long time ago created the JDBC-ODBC bridge as a bridge until all DBs created JDBC drivers (but not everyone did... like Access). It was meant to go away a long time ago but it is still here and you need to use it and configure it. There are a lot of examples on how to do this if you google "jdbc odbc bridge" but here is a link to this site to start:
JDBC with ms-access?
But really, it is better for you to look this up. There are a lot of top quality sites that will show you. Look for a tutorial on Oracle first, then elsewhere. Stay away from India Rose or whatever it is called. It is the how not to do things.
I would suggest not using MS Access. Is there any reason that you are using MS Access other than familiarity? You would be better served to use a database that has native jdbc drivers. There are a lot of good quality databases having readily available JDBC drivers which are certainly much better quality and more stable than MS Access.
If you want something that is easy to start and use right out of the box, and has free query and database maintenance tools try MySQL. Just make sure you set it up to use the "InnoDB" option. That makes it behave as an 'acid' compliant database.
My preference is PostgreSQL, but it can be a little daunting at first to set up if you haven't used it before, and has no GUI tools built from the core team (it has some built from associate projects but nowhere near the number that is built is provided by MySQL). It has some configuration that needs to be done in order to allow TCP/IP connections, as well as some security configuration on how to allow users to connect. MySQL makes this less painful to the new user. In fact I think it is every bit as easy to use as MS Access. (FWIW I use Postgres because I think the DB engine is a better quality with more horsepower for bigger projects... like an open source Oracle).
There are other free ones out there but MySQL is likely one that you can use easily if you are moving from MS Access.

Related

How to allow other people to use desktop application that relies on database?

So I've created a java desktop application using Swing. It stores data entered into the application in a MySQL database (localhost). Now how would someone else be able to use the application ? Would they require a MySQL database as well ? What are the best practices for doing this. I do eventually hope to submit the entire project folder to github.
I guess I would have to ask first whether this program is a demo or classroom project or if it's a real application, because the answer would be different.
For a demo project, it would be fine to post your code to github, and also dump the database to a .sql file and commit that as well. MySQL ships with a handy tool called mysqldump that will do just that - export the entire contents of your database. Then a person can clone your github repo, install MySQL locally, and run your sql script to get a copy of your database. Once they follow those steps, they should be able to run a copy of your swing app on their machine just like you can. One caveat here though is it's best practice to avoid putting very large files (especially binary files) in git. I'm not sure what you need, but if you can put a small sql file out there that's definitely preferred.
That's a segue into the other answer which is hosting your MySQL database somewhere. For a real application, making copies just won't do. Then what you need to do is host your database centrally using a service like this one and allow your users to connect to it. You can still use mysqldump to get your database out there on the web after creating it on localhost. If you go this route though, you'll definitely want to avoid putting your database connection strings on github. Again, this scenario is really only useful if your app is intended to be used for real - don't bother with hosting if you don't need it.
Hope this helps!
Firstly, you should install mysql with a public ip, then alter the connection to mysql with the ip,the example follows below
String url = "jdbc:mysql://ip:3306/db";
Connection conn = DriverManager.getConnection(url, user, password);
if you want to run java program like exe file, package the java program to a jar file with jre

Where/When should I create a table that is the basis of my program?

I'm developing a desktop java program that takes advantage of an SQLite database to restore some data. I now how to create the database and create my table :
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:mokvoc.db");
stmt = c.createStatement();
String sql = "CREATE TABLE COMPANY " + ...
problem
The problem is that I want to execute this statement just once through the whole life time of the program and specially at the first time that the program runs or during the installation step. Please tell me how can I perform this and is it required to add a new JFrame or I don't now sth like that?
For deploying Java desktop apps., the best option is usually to install the app. using Java Web Start. JWS works on Windows, OS X & *nix.
JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or locale, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..
That last one is good for deploying the DB API itself. It might be used across a number of different applications.
Populating the DB would best be done as part of the .. above, as seen in the extension installer service. This is run once on install, and again on uninstall.
The ExtensionInstallerService is used by an extension installer to communicate with the JNLP Client. It provides the following type of functionality:
Access to prefered installation location, and other information about the JNLP Client
Manipulation of the JNLP Client's download screen
Methods for updating the JNLP Client with the installed code
A lot depends on your processes...for example, depending on you installation process, you could execute a specific piece of code which builds the database and tables when the application is installed. Many installers do provide the means to do this, but are typically focused on the platform they are designed for (such as executing a .exe on Windows)
OR
You could, when the application is launched, do a check for the existence of the database/tables and do a "first" run setup process
OR
You could pre-create the database and tables and package it as part of the installer...
Connect to the database.
Fetch database metadata from the connection.
You can query the available tables from the database metadata. If you don't find your tables your application is running for the first time. Initialize the database - create tables, insert seed data etc.

Backup and Restore MS SQL Server database through Java Program

I have developed an Java Swing Application with MS-SQL Server database and now i want to provide backup and restore option in my java swing application that is on click of a button it should backup the database and restore the database any possibilities of how can i do it through java. please help
Two ways to do that. Backup and restore are just sql commands so you'd do them just the same as as you'd do any other bit of sql, e.g. a sql insert statement, unfortunately you need to know a fair bit about the the system to just do them, and you'll get no progress indicator. Using SQLSMO is another possibility, not sure about hitting a .net dll from java though. You could use SQLDMO (pre.net), but you'll need to install the backwards compatibility tools, and you'll give yourself an upgrade headache as DMO (while it seems to work) is not supported from SQL2012. Both have an event you can tick a progress bar on, course if the back up is fairly quick you could get away with not bothering with that.
However, some more to think about. You can backup while the system is running, but then you'd don't have a clear point where the system was at when you did.
Restore requires exclusive access and a high level of privilege, so it's not something you hand out and you need to get everybody off the databases.
And last but far from least it would be very bad, if you inadvertently restored a version of the database that no longer matched the application....
Personally I'd say the people who were authorised to do this should be able to do it without your tool. Dumbing it down, means a lot of code to make sure they don't restore a sql2000 back up from the trial you put out eight versions ago.
We did something similar back up restore code is a bout 2% of the application. For instance we do a pre-backup check, using dbcc et al, to make sure they aren't successfully backing up a corrupt db...
You can write a simple script to backup your database and invoke it from java.
You really need to put a little effort in here.
Since you can connect to the database server already and send it SQL commands, read this for backing up using TSQL.
For database restore, read this.
You need to have the appropriate permissions and access to the hardware for this to work.

Java application and MySQL installation in a single package

How can I pack a Java application and MySQL installation files in a single exe file? Can I install the MySQL files automatically in background (or without any inputs from user)? This is just to simplify the installation procedure.
Java is cross platform, MySQL isn't, so you'd have to create various installers for multiple platforms with different MySQL binaries. If you want to include MySQL source code for non Windows systems, then that's another story... so I assume you want just an installation for Windows.
First of all, get an installation software that you'll feel comfortable with. There is a nice list of free and non free installers on Wikipedia.
Second thing, you can do a silent MySQL installation. How it's done is explained here.
But note that doing a silent MySQL installation without user's permission doesn't sound too good to me, since MySQL isn't exactly lightweight software and you might mess up something if a user already has MySQL somewhere installed.
So, by doing this, you have to be extra careful to check if port 3306 is already up and running (default MySQL port), and other sanity checks to see if there's a possibility of another instance lurking in the background.
It would be better if you at least informed your user that MySQL will be installed. Think about these details, because they might be dealbreakers so some of your users.
Use Java Web Start to launch the application.
JWS offers an ExtensionInstallerService that can be used for installing MySQL. Here is a small demo. of the ExtensionInstallerService.

How to create installer once finished with Java Desktop Application with MySQL DB?

I have finished writing a Java Desktop application with a mySQL database. I want to make the application run outside netbeans and let it be installed on other computers. I know about building the project and creating the runnable jar file, however this requires me to export the database itself to the other computer I want the application to run on.
My question is two parts:
1)Is there a way I can create a setup file that also installs the database and the application together?
2)Also my database path is hard coded, does that mean I have to change the code every time I install my application for someone, what is the better way to do that?
Thanks
Yes. You can use some setup builder, like InnoSetup, for example. Personally, however, I like giving my customers a zip file, which they extract wherever they like. The executable jar should be able to handle everything by itself (I like it where there is no need to install the software, just unpack and run).
If it is hardcoded, then yes (but, what do you mean by hardcoded? path to file? ip address?). You should use properties or configuration files for paths and other external things your software depends on. The software should read from those files. Upon startup check for presence of such file(s) - if missing, the user should be shown a window in which the config can be entered.
As for deploying MySQL with your code - consider using a server for that, so that your users are not forced to install MySQL, instead they connect to it over the net. If you need the database only for storing data locally, why not using SQLite or a similar, file-based db engine?
The above answers are just suggestions and more-less reflect the way I am thinking. I would be happy to hear from someone with more experience. Nonetheless, I hope the answers help a little :)
I agree with Sorrow.
If I have to use MySQL, it is normally over the net since I don't want to allow my clients pass through the hazzles of installing MySQL themselves. If however you am stuck with using MySQL locally, investigate MySQL unattended installations + NSIS Installer.
If you can use any db you want, I just use javadb/derby. It comes bundled with most Java installations these days and if not all you need is to add a jar file to you application.
As per 'hardcoding' paths, I really don't understand what you mean. You really don't have 'paths' as it were, I am assuming what you mean is connection string. You don't have to hardcode your connection string, just put some parameters in a properties file and construct your connection string from them.
1) Is there a way I can create a setup file that also installs the database and the application together?
See my answer to Java based Standalone application.
2) Also my database path is hard coded, does that mean I have to change the code every time I install my application for someone, what is the better way to do that?
Have the DB installer pop a JFileChooser to ask the user where they want to install the DB. Store that path using the JNLP API PersistenceService. Here is my demo. of the PersistenceService.

Categories