How to read database from project folder - java

Hello and good morning!
I have created a folder named db and imported a database file into that folder in my project in eclipse. When I try to stablish the connection I get an error saying unknown database “users” I understand the URL is set to localhost so the driver doesn’t see the database. Fix I’m looking for is
1. Where in the computer is localhost located so I can put my database file there?
2. How can I read and write to the database located at my project location or any other location than localhost?
I’m using JDBC and the MySQL server is running.
Thanks in advance

A MySQL database runs as a process somewhere (the local computer or remote server). The file or folder you have may be the data itself, but it's just sitting there, doing nothing. You'll need to load it in MySQL to use it, a process known as "import".
If you happen to have a file with "data", you need to somehow IMPORT it into the database server. If you are using MySQL server, then:
Is it installed in your local computer?
Is it running?
Do you have a username/password to connect to it?
Did you create a new schema/database to store your data?
If you are good with the above, then you can start importing the data into your database, tables, indexes, etc.
Once the import is finished then you can start querying the data as you wish, by connecting to the MySQL server using JDBC or any other technology you want.

Related

Is it necessary to install mysql to save and retrieve sql data on another computer locally?

I have made an java application which utilises SQL database through MYSql . But I want to transfer that exe file to another computer. I want that the data is stored and can be accessed locally on that computer without installing Mysql on that computer through the Java application . Is there any solution to this problem ?
If you don't want to install any Sql server and still want to save your sql data on your new computer, the answer is no.
Your program needs to connect to a database server to CRUD your database. Java application could not do it by itself.
If you write your program to connect to MySQL, your users must connect to some MySQL server if you want it to run. So each user must either have their own copy of the MySQL server or connect to an existing shared one. So if that's the way your application works, you will have to install a MySQL server on each user's machine. (This is difficult to do in an integrated installer).
(A big selling point of DBMS systems is the sharing of data between multiple users on different machines.)
Other JDBC- and SQL- compliant database systems exist that run entirely within a Java application. Those database systems are single-user, but you can package their .jar files with the rest of your installer. The H2 in-memory database is an open source example. You may have to make some changes to your SQL to use an in-memory database, but you'll be able to use the same framework.
Some developers create programs with both standalone and shared modes. They do that by testing their programs both with MySQL (or some other database server) and their chosen in-memory database. If you do that, your users will be able to use personal, locally shared, or cloud-based data.
Of course, the users of shared or cloud-based data will have to know the JDBC connection strings for their servers. And you'll have to make sure they can put the connection strings they provide into the right properties file, when they "go shared".

Using Jaybird to connect to a Firebird database located in a computer in a local network

I'm creating a java application that connects to a Firebird database and retrieve some data. The program is done, and it runs well in my computer. The problem is that, in the computer I need to run the application, the database is in another computer in the local network and I have no idea of which path should I put into the connection string of jaybird to get it running. Let me explain a bit:
I need to run my application in a computer X, but the actual database is not in a drive in the computer, but in a computer in the local network. Something like this (this is an example, and its reproduced at my home, not at the actual site I need to get it to work):
So the path that it gives me for the database is something like:
I know that if my database is in my drive ( for example, in C:\Users\john\Desktop ), my connection to the database using Jaybird should be something like this:
static final String FB_DB_URL = "jdbc:firebirdsql://localhost:3050/C:\\Users\\john\\Desktop\\TEST1.FDB";
FB_connection = DriverManager.getConnection( FB_DB_URL , FB_USER , FB_PASS);
Actually, that is how I managed to make it work in my computer. But in this case I have no clue of how should the path be. I tried something like:
static final String FB_DB_URL = "jdbc:firebirdsql://192.168.0.101:3050/\\\\DESKTOP-1UFA09I\\Documents\\TEST1.FDB";
With ‘192.168.0.101’ being the ipv4 address of the computer where the database is. But this gave me an exception saying that the application was unable to find the database. I think this is the closest I was to find the solution, but I had no luck.
Some extra information:
Both computers had the firewall off, and could send ping to each other, so they are, in fact, connected.
I'm using Firebird 2.5 and jaybird 2.2.1.
I also tried the following with no luck:
static final String FB_DB_URL = "jdbc:firebirdsql://192.168.0.101:3050/C:\\users\\john2\\Documents\\TEST1.FDB";
The folder is shared (or at least, that’s what I think) because I can get access to it via windows, and the database file gives read/write permissions to everyone.
I'm sure that Firebird is running in both PCs.
Any help in the matter would be greatly appreciated. I'm sure the problem is in the path, because, as I said, the application works well in with the database in my computer. Also, if you could give me a solution that doesn’t require the IP of the computer where the DB is stored that would be awesome, but I think I can get that information anyways.
EDIT
For documenting purposes the solution to the problem was the following:
the right way of making the path to the database is:
"jdbc:firebirdsql://192.168.0.101:3050/C:\\folder1\\folder2\\TEST1.FDB";
the exception occurred because, apparently, jaybird has some permission issues with the users folder in the server. As my DB was located in the documents folder, it was causing some problems when jaybird tried to get access to it. the solution is to move the DB to another folder in C://
To connect to a remote database you need to know the following things:
The hostname or IP address of the Firebird server
The port number of the Firebird server (default 3050)
User name and password
Alias of the database or the full path of the database
You seem to have the first three items covered, so the problem is with the fourth. You think the database is located in C:\users\john2\Documents\TEST1.FDB. If that is the case, then the JDBC url is:
jdbc:firebirdsql://192.168.0.101:3050/C:\\users\\john2\\Documents\\TEST1.FDB
However, the user running the Firebird server process by default has no access to user folders for security reasons.
I suggest that you move the database to a location outside of the user folder, and make sure the account running the Firebird server service has read and write access (NETWORK SERVICE or LOCAL SYSTEM, I forgot which and I currently don't have access to Windows machine) .
Other notes:
Locations of a Firebird database should not be shared, because accessing a database remotely should be done through a Firebird server
You should really update to Jaybird 2.2.11; 2.2.1 is almost 4 years old and a lot has been fixed since
If you are really using Firebird 2.5(.0), then upgrade to 2.5.6, or consider upgrading to 3.0
It looks like you want to connect to the Firebird server in a client/server mode and pass the filename of the database to it. The "host:port" part connects to the server, what follows the next "/" is the name of the database file.
If you use 192.168.0.101:3050 you have to make sure the server runs on the other machine having the IP 192.168.0.101. Since the server is already on the other machine, you need to give it local name of the database name on that machine, like in
static final String FB_DB_URL = "jdbc:firebirdsql://192.168.0.101:3050/C:\\users\\john2\\Documents\\TEST1.FDB";
To not use the IP you'd need to give that machine a name that can be DNS resolved in the local network, which is not difficult, an entry in the hosts name would suffice. The port 3050 is actually the default port so you can omit it.
Since this doesn't work I guess the server is not running on the target machine, only the database file is there. In this case you should connect to you local server but let it know the network reachable database file name, like in
static final String FB_DB_URL = "jdbc:firebirdsql://localhost/\\\\DESKTOP-1UFA09I\\Documents\\TEST1.FDB";
It should allow the local Firebird server to open the \\DESKTOP-1UFA09I\Documents\TEST1.FDB file over the local network - of course if the network share DESKTOP-1UFA09I points to the right location and the account under which the local Firebird server has sufficient access rights on the other machine.

Creating Database in MySQL in Java netbeans in different machines

I have created a standalone application as a school project. The major problem we are encountering is that since it has Java as front-end and MySQL as back-end (compulsory), and we have created a database that solely belongs to one computer, we cannot run the same project on different machines because it won't have the required database, the tables, or the same username and password we used to connect to MySQL.
So my question is **How can I connect to MySQL server in different Machines? **
For database and Tables, I could run a sql file, but that will happen when I would be connected to the MySQL server. Also I am developing the project at my home computer, and I want to run the project on different computers who are connected to my computer by no means .
You a following options
Shared drive : Attach database stored on remote shared drive to a local SQL Server read here
Connect to remote SQL Server instance from local computer - better if they are in same LAN - Steps here
If you want to package DB with app where you have predefined data and you dont want to save transactional data - use inMemory DB. They will load when you application starts up.
Already your application can work using different machine.
You have only to change the localhost string in your connection string with the IP of the MySQL machine :
DriverManager.getConnection("jdbc:mysql://localhost/database?"+ "user=sqluser&password=sqluserpw");
And make sure that remote access are enabled in your MySQL configuration. You can find more about it here.
Instead of using localhost in your database connection string, you should use the ip or aname of the database host
e.g.
jdbc:mysql://IP:3306/db?user=user&password=password
jdbc:mysql://A-NAME/db?user=user&password=password
Just make sure that port 3306 is open and that you have access on the system the databasse is hosted on

Another instance of Derby may have already booted the database using embedded db

I'm using a derby db in my JavaFX app, everything works fine, but when I connect to my db in netbeans to check out some records, and then start my app again, I get this error:
Another instance of Derby may have already booted the database
I disconnect from the db but I still get the exception, it is resolved when I restart my pc.
How can I resolve this?
In embedded mode only one process is allowed to access the Derby database files. If you open the database with netbeans to see what's going on, then your JavaFX application would be the second process accessing the database files in embedded mode. But the files are still open in netbeans.
During development it is often necessary to view the database contents at the same time your application accesses the database. You can start a Derby Network Server and access the database in client mode instead embedded. Be sure to switch the connection strings for both netbeans and your JavaFX application.
When you deploy your application you can easily switch back to embedded mode by changing the connection string in your configuration.

Desktop Database with Server without installation

i have Java-Application that growth a lot over the past month.
All configurations from the programm and all the logs are stored in .txt or .log files.
The programm should change now. The user could install the programm on his own pc but the programm itself should get all the configurations from a central server also write the logs into a central place.
the problem is that we can't install a software on the server, we just have file read/write access to some shares.
so i'm looking for a database that stores the database file on a server without a server installation.
this is new for me so i have some problems with the technics. i checked H2, but i just see a server mode and a embedded mode. i need something like a embedded mode with "external" filebase.
any suggestions for me ?
HSQLDB can be embedded into a any program.
Like Jayan told me in a comment to my question
embedded mode does accept a location to save the data.

Categories

Resources