command to export mysql database to a file - java

I am want to export database table to a file.
I am using the following code, but it gives the following error.
String filename="D:/backup.txt";
I already create
st.executeQuery("select * from tamil into outfile'"+filename+"' fields terminated by ','");
But the error is:
java.sql.SQLException: Can't
create/write to file 'D:\backup.txt'
(Errcode: 13)
help me to clear the error
Thanks

But then you need to think how to put all this data into database.
You should consider mysqldump (or similar for other databases) http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
Ever tried phpMyAdmin? You can easily manipulate your database with it.

Doesn't that error simply mean that you weren't allowed to write that file? Check if you can create a file with that name, in that location, for example using Notepad or by copying a file.

You need to remember that it will save it to the file on the computer that is running the database and NOT the computer running the java program. So for example, if the database computer is a Linux machine then
String filename="D:/backup.txt";
is definitely not going to work. So if you keep this in mind you can test why you cant write to D:/backup.txt.

The thing is, this error points to the fact your program is not able to access resources (files). Strange as it seems -you're in a windows environment-, you might check if the user which is executing your java app is allowed to create files in D:\
Also, as fazo stated, you'd consider using mysqldump, smart stuff which handles the whole process of setting data and schemas as well, into a file.

Related

How to make an installation file from java project and derby database?

I read couple of topics but didn't find a good answer.
I used netBeans IDE and created a program (using GUI)
and using a derby database (threw the netBeans IDE)
and now when I'm finished I want to create one single file that I can send to my friends that contains the app and also the database.
if there an option like this (like creating a runnable jar file + database included).
if not, is there a free-web-hosting to upload my database in?
if there is, please add a tutorial that explains how to do it.
and how to use this databse in my program (what url to get the Connection)
(the database is not big, only 3 tables with some lines in them).
*all of the url-paths I used are project-based (in the project folder - the databse url, files url and so...)
Thank you in advance, sorry for the English mistakes.

How do I store data in Java without need of extra software?

I am currently developing a program in Java using NetBeans that manages expenses. In this program I used MySQL to store data. Now I want to ask that if I give the program to one of my friend, he would also have to install MySQL using the same password that I used. Is there a way in which he will not be required to install MySQL?
Now suppose if my friend already has MySQL, but with a different password. My program would not work in that case, and it would be hard to replace my password with his password in the code. Is there a way to make the program run on his PC?
Earlier once, I have used an Oxford dictionary program. That time I did not have Microsoft Access installed. When I installed Microsoft Access I came to know that all the words of the dictionary are stored in a Microsoft Access file. How can I do this? I.e. how can I use MySQL/Microsoft Access to store data without the need to install either of them?
You can use an in-memory database like H2 Database if you don't require a large amount of data
But I think you should make your db connection configurable by using a properties file
If you want everyone to be able to use the database, you need to run it on a server that people can access through the internet.
However if you don't care about them using the same database and just want them to use their own, you could for example create a small file named "config.ini" or something like that and put the login information (like the password) in there.
Then just read it in your code and use the info you read to log into your database.
That way, every new user will only have to change the config.ini file without ever having to touch the code.
The best solution in my opinion would be SQLite as it is light, and data can be stored locally in a desired location in a single file. H2 is more likely to be a developer tool.
This solution does not require additional software to be present on the user machine. Of course it has its limits, but as a simply storage for program dynamic data it is a good solution. It is worth mentioning that Android applications also can store their data in LiteSQL. It is a bit different approach there, but the principle stays the same.

Using HSQLDB as a portable database with Spring, Hibernate

I am newbie in hsqldb.
In the project I am using Spring 4, Hibernate 5 and HSQLDB.
I am having some specific task and I am trying to use HSQLDB as a portable database, which can be transferred to a flash drive or another computer.
I already have an sql-script with all tables and basic-needed data.
I have four questions that are haunting me.
(I'm sorry in advance if these questions are very stupid):
I need to make the script run at the first launch of the program, and in the other launсhes it must to check if database already exist and (if it already exists)only update data in it. (the program would be used in many computers and the database must be created after the first launch).
How can I do this? Is it possible? Can you give some basic advice or example about how I can to do that?
I am trying to find some information about saving all database info in some file in the file-system. Can you give me please some valid examples about saving hsqldb data in file and about using this file after another launch.
Can I place this file in my project.jar file and to work with all data from it update it e.t.c. ?
What is the best practice to make my database portable(for specific tasks) and where should I keep it? In file, in my project jar.e.t.c.?
Thanks in advance for your answers!
For data storage, you use a file: database. The JDBC connection URL is in the form jdbc:hsqldb:file:<file path>. HSQLDB will save all the data to file.
After connecting to the database you execute the SQL statements in your script one by one. If the tables already exist, the CREATE TABLE statements throw an error. This shows you don't have to execute them.
Because it's up to you when to keep the existing data and when to update it based on the existing contents of the database, you execute some SQL statements to decide. There is no automatic way to do this.
You can put an HSQLDB database in a jar but it cannot be updated. Jars are read-only.
The databases are fully portable. You can place them in a subdirectory of the user's home directory with the ~ symbol. See http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html#dpc_variables_url and the rest of this page for details.

Execute sql server script from java

I am using sql server, my issue is when I change any stored procedure or database table changes I need to make track of it and need to update it on each and every server when my related java code is uploaded.
So I am finding something tool or something if possible with java code that if I give stored procedure or database changes file(i.e .sql file) it executes it on sql server.
May be my question is silly but I really want to know about is this possible with any tool or through code?
If I understand you correctly, the scenario would be:
Read .sql file using one of the Readers
Execute SQL commands using JDBC and Statement class.

Application wont write to MS DB

I have created a Java desktop application which reads and should write to a Microsoft Access DB.
The application works fine before I convert it to a .JAR after which it can only read from the DB but doesn't write to it.
Any ideas on how to solve this issue?
I am guessing you've included the database file in the JAR file itself. Simply put, although you can get a URL to read a file from inside a JAR, you can't write to one. You're going to need to take the database (MDB file?) out of the JAR and put it in on the actual filesystem if you want to write to it.

Categories