JAVA - SQL online database - java

I'm trying to write a code where i need to keep some data in database tables.
I have learned how to use SQL. My problem is that I want this program to run on different computers while having access to the same database.
Any suggestions ?
Thanks

You'll have to run an SQL server on a computer (ie: with mySQL, SQL Server, etc.). You will run queries against the tables that are stored on this database. There are already a lot of resources online with making a database connection to a server.

Just keep database system running in a machine, and instead of pointing to localhost in your JDBC URL, point to the machine's IP Address.

I would personally recommend setting up a script on a host machine and letting it communicate with the database, and have your program communicate with the script. This way, you won't have to keep your database login credentials in your Java program, which could be dangerous.

Related

Using the same database at the same time in different programs

I'm using the H2 Database Engine for java to have access to a database in my java programs. I developed many java programs which use the same database. The problem is that whenever I start such a program while another is already running it can't access the database because it is opened by the other program.
Is there a way to let both programs have a connection to the database? Whenever one program has to query the database the database should execute the query. In case it is executing the query of the other program the query should be executed directly after the query of the other program. Since my queries don't take long time the user wouldn't recognize that his program has to wait for a moment and everything would be fine.
H2 server mode is what you want.
You need to have at least started the server this way for example:
org.h2.tools.Server.createTcpServer().start();
Then replace all the jdbc url with jdbc:h2://yourhost/yourdb, keeping in mind yourdb.h2.db will be located where the server was started. I strongly advise not to use absolute path in your jdbc url, as it will give away the database path in case of hacking.
Last but not least: using the server mode for all has a performance penalty. You might want to use mixed mode so that the 1st client will have almost embedded performances.
To do this, just replace for this client the url with jdbc:h2:yourdb;AUTO_SERVER=TRUE. You could decide to use the same url for all clients as well: the 1st one to connect will be using embedded mode, the other clients will be using tcp performance.
Note that if you're using H2 > 1.4.*, you need to give absolute or relative path like this: jdbc:h2:./yourdb;AUTO_SERVER=TRUE. Note the ./

Is it possible to save database file in a project folder of Java?

This post is the continue of my previous question in here. So I had a look into how mySQL works with Java, but I noticed that the computer must have a database server to connect to the application. So what will happen when my software is ready and users want to run in a different computers? Can't I save the database file in the directory of the software, so any copy of the program will be connected to its independent database to save and parse data from it?
Just to make it clear, in a part of my software, I needs to keep record of previous interactions. Like a history table.
Would using JSON a better option in this case?
In a real world generally database servers are installed on a machine and softwares are installed on different machine.
We let software know the database configuration like database URL /database Name /username/Passwords etc (through property file or through JNDI configurationS).Then java program can connect to database with the help of JDBC driver.
Note:- one Database Server can Host many databases.
If you want to distribute your software without having dependency on client database. Then I would recommend you to use some inmemory DB.This DB you can embed with your software.(alternatively you can write logic that if client database can't be found then use inMemory DB..something like this).
H2 db is my favorite one and it also supports persistent mode and it support s many DB dialects including MYSQL .

SQLITE manager on a server

I was wondering if it was possible to get an SQLITE manager database onto a server? What i mean by this is so that when i complete my program in a java project and make an installer for that project. Anyone who downloads it from any computer running on any operating system who has administration rights can access the database without having connection issues. In addition anyone who logs in to the program can do so with full database connection.
Is there a way to do this? I also don't want the user who is going to install the program to install any additional programs for the database. In addition i would like this program to work on any computer who might not have the SQLITEManager plugin on there firefox web browser or even have firefox installed on there computer.
I would also like the admin to be able to edit anything on the online server the database should be on.
If this is not possible on SQLITE Manager could you recommend a database which can do this but also use and work on the code already made for the sqlite programs?
SQLite databases are just flat-files. What this means in layman's terms:
I download your database as a file.
I can modify it as I please.
I'm not forced to synchronize to the newer version of the flat-file.
You have a choice: You either write your code to force periodic synchronization of the SQLite flat-file to your clients, or you use a dedicated DBMS on your server, such as MySQL, and force your clients to connect to that.

prevent access to mysql database from unauthorized software

i am writing an application in java and i want to enable it to access a mysql remote server.
my problem is that if the application have the user name and password someone can take them and use my db with a different software.
is there a way of preventing it ?
UPDATE
i found this workaround for connecting to a remote MySQL database from an android device.
you put a service in the middle. like a php page that code the data in JSON format which the android app gets via http.
here is the example i found :
Connecting to MySQL database
Having the username and password is designed specifically to grant access to the database. That's the whole point.
You could go to some extra lengths like restricting database connectivity to specific hosts, so at least your paying customers get access to the database and no else can access it, but your customers might choose to use different software to access the database. There's no way around it except licensing terms.
What you could do is run an intermediary program on your own hardware that connects to the database and restrict access to the database to software that is under your direct administrative control. Your program would validate all requests from software under control of your customers and allow the queries that you want to allow and refuse (and log) the queries you do not have to allow. (You do not need to send raw SQL data back and forth -- you can do any amount of processing on the data and queries.)
You can setup JDBC Data Source on your application server. Here you can see example for Oracle Glassfish.
So, your credential won't be used in your code/resources.
If you are saying that you have an application trying to access a MySQL remotely (not in the same box), then i think you need not worry, as the connection that will be established by your application codes will not expose the username and password when it is trying to authenticate and authorize itself to the MySQL server.
You can limit the access to the MySQL-server so that only certain IP-addresses or IP-ranges have access to it.
As a side note, make sure that the user you use in your application only has the needed permissions to the database. For example, the user might not need to be able to create or delete tables. You can even specify permissions for the user on table and column level.

a desktop application storing data?

Say I have created a desktop application in Java to keep notes. I used MySQL for storage. On my computer it connects to database I have created(using my root username and password). But what if I want to distribute my program? Are the other users should have MySQL installed on their system? If they have to isnt tat a problem that I have my MySQL username and password embedded in code?
In general, I am asking how can I make a desktop program in Java which can store data from its users??
Sounds like you want SQLite, There is another SO question here about it.
SQLite as IanNorton mentioned is a good alternative. Other good alternatives are Apache Derby or the H2 database, both providing an embedded (install-free) java database.
Does MySQL have its own embedded version?
Normal "embedded MySQL" is a native code library (libmysqld) that is not really suitable for use with Java. However, there is something called mysql-je that purports to be a Java compatible wrapper. The problem is that it is based on a rather old version of MySQL, and hasn't been updated since 2006.
There are also postings on the MySQL forums about using embedded MySQL with Java, but there's no sign that it is supported.
So I think you'd be better of going with a one of the alternatives; e.g. SQLite, Derby or H2.
No need of multiple installations of MYSQL database if all the machines where you want to access are in a LAN. In that case you can have single database installation and access it from multiple systems using the IP address of the database machine. We can access a remote database through Internet as well, using the IP Address of the machine in which database exists..

Categories