I successfully connected to a DB in eclipse through the Database Development perspective and am able to run queries in the SQL File Editor. However, I would like to write java code to automatically run a query and then extract the data to a flat file. What is the easiest way to do this? Since I am already connected to the database, can I bypass the java code that involves connecting to the database.
No, the Java JVM also needs to be able to connect to the database. There are many examples of JDBC connections on the internet, including this one.
Once you have got the data you needed, you should make use of FileWriter in order to write the information to file.
Related
So I've got a java application which is running 24/7 writing into a database file. I'm using jdbc to write into the .db file.
I've got a C# Server which is supposed to read from that .db file and send information via networking to a client. Unfortunately this is only possible while the java application is not filling the .db with datas.
When the Java application is not activated the C# server is reading from that file and sending datas via networking flawlessly.
When the datacollector is activated and writing permantently to the database file my C# server cant read from it. In that case the client kills himself with a stackoverflow.
I'm using: jdbc for my java datacollector.
I'm using: System.Data.SQLite for my C# server.
I assumed that there will be only problems when 2 programs are writing to the .db file simultaneously, but since only my java application is writing to the file I thought this is going to work out.
Any ideas what I'm doing wrong?
According to https://www.sqlite.org/faq.html#q5
When your Java application is INSERTing into SQLite the database file is locked. So it cannot be accessed by another process. Exclusive lock is held until transaction is commited.
I don't think SQLite is suitable for this type of usage. I suggest you use real DBMS.
I have created a Web Application using Java JSP and for a Database I used MySql server 5.5. I am running on windows and the application works just fine. My question is, where is the actual physical database stored, i can not find it anywhere on my computer, and the reason why i am asking is portability, if i copy and paste and run the project on different machine, there is a problem connecting to the database, i can recreate the schema of the database, but the actual data stored in the database i can not find it.
Please help
Use mysqldump to backup your database:
The mysqldump client is a backup program originally written by Igor
Romanenko. It can be used to dump a database or a collection of
databases for backup or transfer to another SQL server (not
necessarily a MySQL server). The dump typically contains SQL
statements to create the table, populate it, or both. However,
mysqldump can also be used to generate files in CSV, other delimited
text, or XML format.
An alternative option if you don't want to use the commandline is to use a program like HeidiSQL
I have a Java desktop application (a runnable jar) that is being made available to clients within a CD. Then "autorun.inf" automatically launches the jar and the application runs from the CD (therefore running in a read-only environment).
This application needs to access some known data and use it somehow. Since the application is standalone, this data should be distributed with the application in some kind of files.
Now comes the challenging goals:
This data are just columns of numbers, similar to database table exports. And the most convenient way to access it would be exactly like if it was a database table where one can do SQL queries to this files.
This data should be confidential to the user clients. Although the data must be distributed with the application, it cannot be legible to users. So, the data must be encrypted or obfuscated somehow.
So I kindly ask for your help mainly to point me some ideas that will help me understand and discuss with you which is the best way to implement such features.
Thank you very much in advance,
Alves
Edit #1:
Using an embedded database engine is a great idea and would solve both problems at the same time. But it raises some questions for me:
Does the database engine needs to write files somewhere? I've done some tests with HSQLDB and I think it does. That would be an issue since the application is running from a CD.
If it really needs to have such files, can I release them also with the application, instead of being created on the fly?
Just to confirm, if I use an encrypted database, I have to put somewhere on the application the secret Key that will make me decrypt it, right?
One possibility would be the use of an encrypted SQLite database.
SQLite is a relational database management system, where the databases are files.
For encryption in SQLite look here: http://www.hwaci.com/sw/sqlite/see.html
Your goal is absolutely impossible and it's rather obvious why: Your application obviously needs to read the encrypted data (otherwise why bother including it), hence has to decrypt it, hence has to store the keys somewhere.
Well there you are: You sent the encrypted data AND the key to decrypt it to the client - what's to stop them from doing the same thing your application does?
Yes it may stop some people from getting your data, but it's nowhere safe against someone who has even a slight understanding of what's going on
You could use SQLite with the SQLite Encryption Extension.
The SQLite database file could be created at the first start of you application.
You can simply test if the SQLite database file already exists:
if (!new File(database_filename).exists()) {
createNewDatabase();
}
The path to your SQLite database is stored in the database_filename variable.
If the file doesn't exist already, you can save the file for example in the user's home dir.
I'm using the following code in one of my applications to create the database with an external SQL script file.
public final void createNewDatabase() {
InputStream inputStream = getClass().getResourceAsStream("database_create_script.sql");
String sqlCommand = new Scanner(inputStream).useDelimiter("\\A").next();
try {
Statement statement = connection.createStatement();
statement.executeUpdate(sqlCommand);
} catch (SQLException ex) {
ex.printStackTrace();
}
System.out.println("Database created");
}
H2 Database supports encryption, too. As much as I like SQLIte, for a Java application I'd choose H2.
Short Version: I need to store some data between runs of a java program.The data will be of the form of a table.Is there anything that can let do something like a sql query in java??THE SOLUTION MUST BE ABLE TO RUN ON AN OFFLINE COMPUTER.
Long Version: The user will be entering some data daily and i want something like a sql table in java. The program will run on a computer that is NOT CONNECTED TO THE INTERNET and so i need a truly local way to store data(Lots of it).Also preferably the data should be stored in such a way that it is not easily accessible to the end user(as in ,he should not be able to double click the file and simply read its contents)
Major Constraint: On searching online i found many people were using localhost to solve similar problems but that facility is not available to me as i CANNOT INSTALL ANYTHING on the target computer.
If a simple data file is not good enough, how about using SQLite with a JDBC backend? It will allow you to have an SQL database stored in a regular file with no dependency on any kind of server. Alternatively, there are many other embedded DB engines that you could use, depending on your needs.
EDIT:
By the way, most (if not all) DB engines that I know of do not obfuscate the data before storing them in the filesystem. The data will be fragmented, but parts of it will be visible if you force an editor to open the file (e.g. using "Open with..." in Windows).
There is also nothing to stop your user from accessing the data using the command line utility of the selected DB engine. If you want to obfuscate the data you have to do it in your code. Keep in mind that this will not stop a determined person - if your application can read it offline, so can everyone else.
Use an embedded database (like Apache Derby, HSQLDB, H2) so that you don't have to run a database server on the machine. The data will be stored locally on the target machine and it won't be human readable.
You have several options:
Store it in an xml-file
Store it in an local installed database
You can install a database like mysql or use a in memory database like sqlite or hbase or apache derby, which is included in java 6
I'm considering using HSQLDB version 1.8.x in a desktop app for storing local data. From what I can see, the database is stored on disk as a number of SQL statements to create the tables, insert the data, etc.
Is there a simple way I can hide this from users? I'm don't necessarily need it to be completely encrypted, etc - I'd just like to prevent the casual user from simply opening the file and seeing the structure of the database.
You could embed your database files within a jar file and connect to them using the notation:
jdbc:hsqldb:res:<path in jar>
Check out the Advanced Topics section of the HSQLDB guide for more information on this. However, I've never tried it so am not 100% sure it will work ...
The solution I've gone with for now is to call:
db.update("SET SCRIPTFORMAT COMPRESSED;");
to store the .script file in a human-unreadable form and:
db.update("SET PASSWORD password;");
to prevent more savvy users from opening the DB using their own HSQLDB client.
Unfortunately I was not able to execute below command
db.update("SET scriptformat COMPRESSED");
And was getting this error
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: SCRIPTFORMAT
This error solved with this command
db.update("SET FILES SCRIPT FORMAT COMPRESSED");
I am using HSQLDB 2.3.3