Java, connect to mysql file without server - java

I wonder if it's possible to use the database file from mysql, without having a server running. Just copying the db file and place it somewhere, then use "jdbc:mysql://localhost:3306/table";
and change it to something like "jdbc:mysql://C:/Users/me/Desktop/table";
Will this work or is there a better way?

This is not possible with Mysql. Sqlite is a Serverless database designed for this purpose.

You're probably looking for an embedded database, i.e. as you say a library that is able to access a database on a file without a server.
MySQL has an embeddable version: http://www.mysql.com/oem/
You might want to check also H2: http://www.h2database.com/
Or Apache Derby: http://db.apache.org/derby/
Or HSQLDB: http://hsqldb.org/

Short answer: no.
Long answer: You'd have to basically recreate the MySQL daemon in Java. In particular, JDBC would have to know the structures inside of the file. The files are quite complicated, and this would be quite a pain.
This means you would have to basically write your own code capable of parsing and manipulating MySQL files. This would be a horribly complex task.
Yes, there is a better way. If you want to use MySQL, use MySQL.
Another option would be to use the embedded version of MySQL, or something like SQLite.

Related

Converting a MySQL database to SQLite single file in java

I have MySQL installed, but It takes more than 1 GB of C drive. However the database file is only some KB's. I want to help to transfer the database file into single file that is in the Java program data folder. Also i want to know how to change the connection to be that file instead of using JDBC?
Thank you.
So a fast edit after long time ago 😂
that day i meant that I want to use something like SQLite instead of MySQL
I think that clarifies it now
waiting for new answers.
First of all, you questions is structured in a way that i do not fully understand it. However let me make things clear.
In order for java program to connect to Database you need a driver ( typically JDBC ). The database HAS to be on some sort of "server" i.e apache, sql server, derby etc. If you want to save some HDD space i suggest looking into Derby Database, which is "integrated" database
EDIT:
Or you can just host external database on some hosting server ;), there are some free ones out there too !
You might want an embedded database, like h2 or java Derby.
Such an embedded database often is stored as file, without external database engine runnin (it might do that too though). The API remains JDBC.
What I needed was mySQLite, it's saved on the device as a single file and easily managed through java.
What you need to do is to export the mysql database
And use dbmigration app to convert it to sqlite then use your database as you wish

Using a local database for java application

I am currently undergoing a project which requires a database. So far uptil now I have been using a sql localhost database, I was wondering if there was an alternative to this.
Similar to micrsoft access database where I could read from the local database file instead?
It sounds like you are talking about an embedded database.
Take a look at: http://www.h2database.com/html/main.html
You could use Hypersonic or Derby; the latter is part of the JDK now. SQLite is another possibility.

The simplest Java SQL provider?

I need SQL functionalities for a Java JSE application, but dont need a whole SQL server, with things like listen on a port, connection string or even a standalone process to be runned or configured.
I also would prefer to work with files as storages, so that file path identifies DB data.
So given to the DB API the name of a file, I would need perform SQL with the file as DB storage, supporting tables, search, joins and inserts, without thinking to things like ports, external processes, server installation, ecc..
Without any other configurration action, since any other configurable feature is not needed.
Is there some library, preferrably installable as single .jar, that provides this functionality?
If there is not this library, which file-based DB is the simplest to configure and use within JSE, and which configuration steps are needed to perform a query in the provided DB and deploy it with (working) the java .jar application?
I suggest Derby db.apache.org/derby/
I like H2 Database very much. It compares very well with other database engines.
Sure, Hypersonic SQL or Derby, the database that comes bundled with Java 7, will both fill the bill. SQLite would be a third alternative.
you may want to check this out:
http://www.sqlite.org/
I have used HSQLDB in the past and liked it. Depending on what your needs are, you might also be interested in JoSQL which allows you to do SQL type of queries on java collections.

Is it good practice to package an SQL database inside a JAR?

I would like to create a Java library without any dependency, but it needs to use an embedded SQL database. Is it good practice to package both the binary database file and the driver to access it inside the JAR file? What are some advantages and disadvantages?
HSQLDB
More and more Java applications have their own 'in-memory' database that is launched upon application start. Check out hsqldb for an example.
Most obvious advantage is that you control the database and don't have to take different DB servers and their sql dialects into account.
Go on, it's a widely adopted practice used by Hibernate for example.
For a memory constrained devices:
Then do not use embedded database at all. Try using ThoughtWorks library XStream which serializes/deserializes objects to XML/jSon which can be stored in files. Very effective solution with a small memory foot print.
Your database will be read-only (since you cant repackage the jar at runtime). If that is fine then go nuts :)

Alternative of Storing data except databases like mysql,sql etc

I had completed my project Address Book in Java core, in which my data is stored in database (MySql).
I am facing a problem that when i run my program on other computer than tere is the requirement of creating the hole data base again.
So please tell me any alternative for storing my data without using any database software like mysql, sql etc.
You can use an in-memory database such as HSQLDB, Derby (a.k.a JavaDB), H2, ..
All of those can run without any additional software installation and can be made to act like just another library.
I would suggest using an embeddable, lightweight database such as SQLite. Check it out.
From the features page (under the section Suggested Uses For SQLite):
Application File Format. Rather than
using fopen() to write XML or some
proprietary format into disk files
used by your application, use an
SQLite database instead. You'll avoid
having to write and troubleshoot a
parser, your data will be more easily
accessible and cross-platform, and
your updates will be transactional.
The whole point of StackOverflow was so that you would not have to email around questions/answers :)
You could store data in a filesystem, memory (use serialisation etc) which are simple alternatives to DB. You can even use HSQLDB which can be run completely in memory
If you data is not so big, you may use simple txt file and store everything in it. Then load it in memory. But this will lead to changing the way you modify/query data.
Database software like mysql, sql etc provides an abstraction in terms of implementation effort. If you wish to avoid using the same, you can think of having your own database like XML or flat files. XML is still a better choice as XML parsers or handlers are available. Putting your data in your customised database/flat files will not be manageable in the long run.
Why don't you explore sqlite? It is file based, means you don't need to install it separately and still you have the standard SQL to retrieve or interact with the data? I think, sqlite will be a better choice.
Just use a prevayler (.org). Faster and simpler than using a database.
I assume from your question that you want some form of persistent storage to the local file system of the machine your application runs on. In addition to that, you need to decide on how the data in your application is to be used, and the volume of it. Do you need a database? Are you going to be searching the data different fields? Do you need a query language? Is the data small enough to fit in to a simple data structure in memory? How resilient does it need to be? The answers to these types of questions will help lead to the correct choice of storage. It could be that all you need is a simple CSV file, XML or similar. There are a host of lightweight databases such as SQLite, Berkelely DB, JavaDB etc - but whether or not you need the power of a database is up to your requirements.
A store that I'm using a lot these days is Neo4j. It's a graph database and is not only easy to use but also is completely in Java and is embedded. I much prefer it to a SQL alternative.
In addition of the others answers about embedded databases I was working on a objects database that directly serialize java objects without the need for ORM. Its name is Sofof and I use it in my projects. It has many features which are described in its website page.

Categories

Resources