I'm developing a software with NetBeans and I'm using MySQL as my Database server. I' planning to use two buttons as, "Backup Database" and "Restore Database" to respective functions. How to accomplish these functions? And for both functions, it would be awesome if File Chooser window is used for the functions too. Thanks in advance! :)
What about creating a dump and saving it? and then running it when you want to restore?
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
EDIT:
Well since you say you dont really know how to achieve this then ill be more specific.
mysqldumpl must be run from a commandline for this pls read this link:
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
your code should look something like this:
String yourCommand = "mysqldump -h localhost -u [user] -p[database password] -c --add-drop-table --add-locks --all --quick --lock-tables [name of the database] > sqldump.sql";
Runtime.getRuntime().exec(yourCommand);
After that you should have succefully saved a file with all the data of your database
the last part of the string "sqldump.sql" is the name of the file, you can set your own name with file chooser, and replace that name with the one from the user, google will help you with that.
Well first get that done
Post your code when you have it running and then we can tackle the restoring of the DB
Related
I am currently training to be an application developer and am in my second month.
Now I got a task that felt impossible for me.
I have also googled since yesterday but did not find anything.
I have to insert the data from a database that I access with SQL Explorer into a .csv file. and save it on my machine
It should work automatically, but for testing it doesn't have to.
I use Eclipse and program with Java, have seen something on the Internet that it works with MySQL, but the DB2 database is connected with the SQL Explorer.
My plan is a solution, programming in java to update everyday the csv thats basically my task
Sorry if it doesn't fit here on Stackoverflow, I'm totally lost because I'm still in the trial period.
Greetings from Germany
IBM Data Server Client and driver types.
The IBM Data Server Driver Package, for example, contains the Command line processor plus utility which can run various db2 commands, statements and scripts including the Db2 EXPORT command.
Usage:
From the OS command line:
clpplus -nw user/password#host:port/database #s1.sql
The contents of the s1.sql file used as a parameter above:
SET ECHO ON;
EXPORT TO "full\path\to\my_file.csv" of del
SELECT *
FROM MYTABLE;
EXIT
You may use whatever valid SELECT statement in the EXPORT command.
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
I had created a database using mysql workbench and i saved that database in one of the local drives. So in netbeans I need to design a database which is same as the earlier (created by using mysql workbench). So I would like to import that database into netbeans. Can anyone explain the procedure to do so?
A couple of days back I faced the same problem and found a easy solution. Though this question is asked 2 years back, I am posting this answer because it still doesn't have an answer here, so that it can provide help to other people with same problem in future.
I assume you know how to configure MySQL server. If no, visit https://netbeans.org/kb/docs/ide/mysql.html and follow the 'Configuring MySQL Server Properties' part.
After you create the server, right click on the 'MySQL Server'->'Create new database'. Give the database same name as the existing database has.
Now create a connection to the newly created database. If you dont know how, see the 'Starting the MySQL Server' and 'Creating and Connecting to the Database Instance' from https://netbeans.org/kb/docs/ide/mysql.html.
Now Expand the newly created connection which will look like 'jdbc:mysql://localhost:(portNumber)/(databaseName)'. Expand the database name node and right click 'table' and select 'Execute Command'
A new window will open. Assuming that you have a .sql file(Or may be a file without extension but consisting of SQL commands. You can export the database in file using your current admin tool if you have not already). Open that file in editor(notepad) and copy paste everything in the new opened command window in Netbeans.
Before Executing, just add one line before which states which database to use to execute query on.
USE <newly_created_database_name>;
as shown in the screenshot below (Red mark), it should be before the first line of the copy pasted SQL commands.
Now Execute the commands using the 'run SQL' button (green mark in image). To check, expand the tables node to see all the tables in original database are created.
Now you have your original database in place to work with.
You can use the built-in tools.
With your NetBeans project up, click new file and search for a persistence unit.
Follow the prompts entering the corresponding information(ie, host info, username, password, etc).
Once completed, go back in your project view and click on the services tab. You should see the database connection there.
To add them to your project, go back to your project tab and add a new file of type "Entities Classes from Database" the next prompts will ask you what tables to import and such.
Once completed, NetBeans will auto import all tables with getters and setters.
Here is a succinct example from Official Documentation: https://platform.netbeans.org/tutorials/nbm-crud.html#creating-app
I guess I'm just not "getting it". If I don't have SQLite already installed on my computer, and I want to write a Java app that uses an embedded database, and I download/import the SQLiteJDBC JAR into my project, is that all I need? Or, do I need to first install SQLite before and create a database file for my SQLiteJDBC code to connect to and run queries from?
If that's the case, and its not sufficient to just download/import SQLiteJDBC, then doesn't that mean that I'll have to make sure SQLite is installed on every system that I want to run my Java app on? And doesn't that defeat the purpose of a portable/embedded database?
Basically, I'm getting hung up on the SQLiteJDBC tutorials because:
They don't tell you how HelloWorld.sqlite gets created (does SQLiteJDBC create it for you, do you have to create it in SQLite first from the command prompt, etc.); and
They never clarify whether SQLiteJDBC is dependent on SQLite for the API calls to work
Any help here is greatly appreciated!
You have to put the SQLLite JAR in the CLASSPATH of your app. There's no "install" beyond that.
Maybe this tutorial can help you.
Here's another that shows how to create a database and tables.
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.