I'm currently in the process of learning Java and swing and in doing so am trying to create a desktop app.
As part of this app I have set up a mysql database which the app is connected to, however I'm uncertain as to how this would work if I was to distribute the app for other users, how would I create a database that they are able to use on their system if they don't have mySQL installed or the database initiated.
Help with this would be much appreciated.
You should use embedded database. I would not recommend MySQL for commercial applications, because it is very expensive. Try to use HSQLDB - very fast and do not has memory leaks (at least I didn't noticed).
Here is an implementation example:
private static Connection conn;
/**
* Gets database connection.
*
* #return the database connection.
* #throws SQLException if database error occurs.
*/
public static Connection getConnection() throws SQLException {
if (conn == null || conn.isClosed()) {
conn = DriverManager.getConnection("jdbc:hsqldb:file:data", "somedb", "");
// conn.setAutoCommit(false);
}
return conn;
}
Make sure you have added hsqldb.jar file to your libraries.
I would suggest looking at SQLite instead of mySQL. It sounds like you need a standalone database that does not require a server to run. I've done both Java (and Android), Swing, as well as C#. I found that SQLite was the best solution to have a database on the different platforms and not install a server.
You have three choices:
Each desktop will have it's own, private database.
Each desktop talks to one, remote database server.
Each desktop talks to a web front-end to the remote database server.
"Option 3" is the most common scenario. One option is a "web app" (in which case you don't need the "Swing desktop" app at all). Another option is a "thick client Java applet". You can use Swing, most of the processing will be local ... but your Swing applet will communicate with the remote database for database access. You might consider a REST interface.
If you really want option 1 (app and database completely local to the desktop), then I'd definitely go with an embedded database. Sqlite is an excellent choice. In fact, Android uses Sqlite. HSQLDB or even MS-Access can also be good choices.
Regarding your problem, I'd ask if you're sharing the information of the database among the users of your application. I mean if you need the data centralized in one database the users can access through the desktop Swing application.
If so, then you'd need another application that would provide the information that is stored in the database (you can use Web Services at this point), then you'd just need to implement Web Service client code to interact with your database through the Swing application.
Now, if you want independent application with a database per user. Then you can go for many options such:
SQL Lite
H2 Database Engine
Apache Derby
HSQLDB
Hope this can give you orientation in what you need to implement.
Happy coding.
Related
I have a MySQL database installed in my Amazon AWS instance (not RDS). The same database is installed in a personal computer, offline.
You have to connect to the amazon database via SSH, it is in a Ubuntu instance.
The online database contains information of 1000 users. The offline versions contain information only for that particular user.
Users use a Java desktop application to feed data into the local database. When they click on the Sync button, the 2 databases should be synced. Remember here that the desktop offline database should "upload" the newly inserted things to the online database while it should "download" new data (if any) related only to the particular user .
The system cannot be a manual way where someone manually turn on a 3rd party application, use putty or connect SSH, configure the databases etc and sync. The system should be embedded to the desktop java application.
I looked into things like SymmetricDS and it is too much complicated, not sure about the SSH access too.
Any idea about how to do this in an easy way? I am also creating a REST API thinking I can handle this manually, but if there is already built system/API I am onto it.
This is very simple and doable. Just use MySQL replication.
MySQL replication
Let me know if you want any further details. I can give you working model of my.cnf as well if required.
Br//
I am working on a project where the raspberry pi (model B) runs JAVA application to connect to and retrieve data from a Router.
What i want to know is the best approach or the best database that i should use to store that data retrieved from the router. Knowing that i will need to build another android application that should connect to that database (on the raspberry pi) and display that data.
So, what database that is compatible with the java application and the android application?
Also, later on i wish i could run the RPI as a server that enables me to connect to that database from anywhere.
Keeping in mind that:
I am new to Raspberry pi
New to Database
I suggest you to take a look at OrientDb is a NoSql Java graph-document db (you can chose) it support a lot of queries paradigm and an Sql dialect as well (you dont have the join), is fast, light, and support native rest query, i have tested it on raspberry pi (even in distributed multi-master mode on 2/3 raspberry node) and it perform well, the nice thing is that allow you to think in a different way, your data is presented like a java object an exposed you by rest paradigm in Json over http so you can think your client application on client side, using html 5 and jscript framework like angularJs for example (and you can 'deploy' your static resource direcly on the db that work like an extremly light application server)
This answer follows the previous comments. Let's say :
You have your router. Connected to it you have PC (with IP address called IP1) and your RPI with IP2.
On your RPI, I guess you have RaspBian. So with the package manager, you install mysql. You create your desired database and tables.
When using eclipse to access your database for testing purpose, or explore all data, I think you need to provide the jdbc driver, and configure the url of the db. Because you are running eclipse on your pc and you want to access the db hosted by the RPI, you should provide the IP2 address in the url.
Because your Java app will run on RPI, you can set the url to "localhost". See the following link : http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
So, to sum up, eclipse and java app both need a mysql jdbc driver, configured the right way.
Later, you'll want to access your db through your android app. You'll need to configure your router with port forwarding, to access your db from outside. But this is out of topic. I let you search.
Almost All the Databases are Compatible with JAVA.And Hardly I belive that you will not have such privilege to choose Database while develop real time Application .
Java and Android Both are not different , both are same on a Pillar.There will be no problem because of using Database.
i suggest SQLite as Android uses it natively and it does not need that much of resources as other DBMS like *SQL
How can I connect my local database in my application? I have a database which contains a book. I have to display it in my application in J2ME
Basically, in JME, an app can create a database which is a file stored in the RMS. Actually, there are several APIs that can help you in creating a Java ME database: Is there any option for local database like Sqlite for j2me - CLDC devices?
Anyway, I think the data in a sqlite database is not easily accessible to a JME app. It is not impossible but it, maybe, is complicated. Honestly, I don't know anyway to do it.
You have to create a server application where you access to database. Also your J2ME has to communicate with your server application for getting Data you want to display (Use HttpConnection Class).
In Mobile Application, you can not directly make connection with local or remote database. To Make Connection with Database , you need a middle tier called a web service. You can create web service in any platform like Java-Servlet, .net or in PHP. See this is a very simple example to work with MySql Database.
Our customer has a client/server application based on mdb database (microsoft access) and he'd like to expose part of data via his web site (deployed in java).
What is the best way to achieve it?
I need a place to start (for example web service, spring integration, mule, service mix).
If the web server is currently located on your office LAN, and the access front end applications can use/see the folder where the back end mdb file resides (we're talking windows networking here), then in theory you could expose some of this data to the web server if web server loads and usage rates are not going to be too high.
In the case that the server is not going to be on the same Office Network, then an often used an reasonable solution is to simply move the back end data out of access and move it into some database server such as SQL server, or mySQL. When you do this, then both the access application (front end part) thus continue to run and function as before. And now also the web site can also share that data. This so called upsizing process of access data is not hard to do, but you want a competent access developer that knows both SQL server and access, and has done upsize many times. If the developer done this many times then it not a lot of work in most cases to move the data out to a server and keep the existing code investment.
So your choices are to keep the access code and database as is, but simply move the data out of access to a server based system. As noted the other alternative is in the case that if your web server is attached to the same network where access data resides, then in theory would be a simple matter to place the access backend data on the same server as the web server. This setup would allow both the locally users on the Office Network, and the web server to share and utilize the data in the access backend file.
Another alternative is of course is to have access to connect to a database server your utilizing on the web system, such as MySQL of SQL server. Since access can connect to the database server in this fashion, then again it is theory possible to shuffle data at predetermined times, or even during use to pull data down that's been gathered from the web site into the access application. So you keep Access as is, but connect it to the web part that gathers needed data.
Which of the alternative above choices above makes sense will depend on your particular set of circumstances.
To really throw a wrench into this mix, access for 2010 can build scalable cloud computing systems where the data is based either on Azure SQL or even 100% web based if you have SharePoint. In fact when you publish an access database to sharepoint now the result is .net XAML (zammel) forms and a scalable system in terms of users. In the following video you'll say that the halfway point I switch to running the access application entirely in a browser:
http://www.youtube.com/watch?v=AU4mH0jPntI
To do the above access web development, you'll be using SharePoint. However, if your organization does have SharePoint now then this could be a reasonable possibility for you.
You can connect to your ACCESS database with Java through JDBC-ODBC Bridge.
The steps to do this would be:
In your server (the one hosting your Java Web Application) create an ODBC entry pointing to your ACCESS file. Name it mdbodbcaccess.
Then connect to that ODBC entry from Java using JDBC.
Something like this:
//
// points to the entry you've just created
//
Connection conn = DriverManager.getConnection("jdbc:odbc:mdbodbcaccess");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select f1, f2 from table");
while (rs.next()) {
// iterate your resultset and do something with it
System.out.println(rs.getString("f1"));
}
rs.close();
st.close();
conn.close();
Alternatively, you might want to use pooled connections from your Application Server.
I believe that you are a little bit disappointed.
MDB is Message Driven Bean. It is not database. It is a way to listen to JMS destination and perform some action driven by JMS messages. The data already must be saved somewhere in DB. If they use MDB it is a Java EE application.
Now about the site. Which technologies are used for site? If it is java based technologies your life is easier because the site is a Java EE application too. In this case you actually have 2 Java EE applications. If all these correct you can
merge them. In this case you can just call the "mdb based application" from the back end of the web site.
Alternatively you can call one application from another. Perform remote EJB call or expose some of the API as a web service and call it. Or even connect to the queue of "mdb application" from site and send messages.
If site is not written in Java use web services or connect to queue.
Generally you have a lot of possibilities. But it is hard to recommend something specific without additional details. I hope that this answer helped you a little bit. If you need more help please provided details.
I am making an Android application. Since it is so simple, I first thought I could simply eliminate the need for Java application on the server which acts as a middleware. I tried directly connecting to the database using the JDBC driver for MySQL but my program is crashing so I'm not sure if Android "supports" the JDBC driver for MySQL.
So I am thinking of how to implement the application. Basically the application writes some data from a remote MySQL database and retrieves some data from a remote MySQL database.
Do I connect to a Java server program using sockets (or some other method of communication)? Or could I implement a direct connection to the MySQL database from the client application?
I tried directly connecting to the
database using the JDBC driver for
MySQL but my program is crashing so
I'm not sure if Android "supports" the
JDBC driver for MySQL.
Never never never use a database driver across an Internet connection, for any database, for any platform, for any client, anywhere. That goes double for mobile. Database drivers are designed for LAN operations and are not designed for flaky/intermittent connections or high latency.
Do I connect to a Java server program
using sockets (or some other method of
communication)?
It doesn't have to be Java. It just has to be something designed for use over the Internet. As Mr. King's comment suggests, Web services have been used for this for much of the past decade. For Android, REST Web services are probably the easiest to consume, since there is no built-in support for SOAP or XML-RPC. But whether the Web service is implemented in Java, or PHP, or Perl, or SNOBOL, is up to you.
Well, OK, perhaps SNOBOL won't be a viable option. :-)
I know this might be a little late but as I ran into the same problem with a project at school I wanted to share my solution with you as you might profit out of my experiences.
Android is bad for Database-Operations so creating a normal Database-Controller wasn't a thing. Instead I created a Server in Java which handles all Database-related stuff and can also be extended (in my case I used a Feedback-function, too).
The Github-REPO is: https://github.com/Cedced-Bro/Public-Server You can check it out and this is open-source so you can use and contribute to it if you have more ideas to it.
To answer your question more properly: I would strongly suggest to NOT grant all users direct access to your DB as you can run into security issues with malicious users. This was the reason why I created this controller in the first place instead of just a PHP "forwarding"-server.