C#, Java SQLite Reading / Writing not possible - java

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.

Related

Accessing Connected Database DB in Eclipse with Java Code

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.

How to rewrite csv file on client with java web application

I am new in java web application, (Java EE, JSF).
I tried to change the contents of a csv file on the client computer with a java web application, so that the client does not have to download a new file, because the file is already in the set to be used for applications in the client. so I just wanted to rewrite the csv file.
Could it be done in java web application? If yes, please give me an example. I am very grateful if there is a better solution.
No, absolutely not. You can't change the contents of a file on a client computer from a web browser. The best you could do is have them upload a version of a file and then send them another version to download. Giving write access to the filesystem would be a massive security hole.
Place the CSV on a network share somewhere. The client can edit it, the back-end server can edit it. Requires more infrastructure, of course, but may work depending on the type of application.
Not that I think this is what you want, but if the 'client computer' was also acting as the server (i.e. was the host running Jetty/Tomcat/whatever), then you could modify files on it using the java IO api.
Again, very likely not what you want, just saying it would work.

Creating Derby Databases on Webpages

I have a Java Applet which displays 2D table data from a Derby database. I am also using Netbeans. I want this application on a webpage. I have uploaded the html document and all the .class files into the proper directory on the server. What remains is accessing the database.
I would like to use Embedded Derby so that my application contains the database (is my understanding of that fact correct?). Then I should be able to upload the html and jar/class files to the server and my application will be deployed. However I am confused on the following:
How do I create/setup the database to live on the webpage?
What would my connection string be?
Is there a better way to do this altogether in NetBeans?
All help is greatly appreciated. Let me know if I can make my question clearer.
You can package your Derby database itself inside a JAR file as part of your applet, and then it will be effectively part of your applet and downloaded when your applet is downloaded.
See this related question: Distribute a database made with JavaDB with Java program
Note that this will create a read-only database. If you actually need to update your database from your applet, you'll need a more sophisticated approach (e.g., package the initial data into the applet, then when the applet starts up, request access to local storage on the workstation and create the writeable database there).
For complete documentation on building and deploying a database-in-a-jar, the documentation is here: http://db.apache.org/derby/docs/10.9/devguide/cdevdeploy15325.html

Implement a Java desktop application that needs to access confidential data

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.

Store data between Program Runs Java

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

Categories

Resources