Connecting to a database on another machine through Java application - java

If a java desktop application from a client machine needs to connect to a database on an external central server (on another machine), kind of like a php script that can connect from a browser on the client machine to a database, how would that be accomplished? I know that you can use JDBC, but wouldn't the person who's running the desktop application need mysql connector/j driver installed on his/her computer?

As long as you bundle the appropriate JDBC driver (usually a jar) with the application and make sure it's on the runtime classpath everything should work. There is no "installation" of the driver separate from having the appropriate classes on the application's classpath.

Of course: if an application is using JDBC to connect to a DB they'll need the appropriate JDBC driver.
Most applications already have a set of library dependencies; it would just be another one.

Related

Connect to MonetDB via JDBC without specifying database name on Linux

Our Java application works as the following:
- it connects to the server without specifying any database name,
- checks if the given database exists and creates a new one if not.
On Windows, the application works fine but on Linux it cannot connect to the server. The error message: "monetdbd: please specify a database".
We would like to run the same Java application on Linux and Windows. Is there any method to check and create database from Java on both operating system?
We have figured it out at last. A client can connect to an existing database only. On Windows the installer creates a demo database. If other database is not specified, the client connects to it automatically.
On Linux the installer does not create any database, so it should be created
manually.

My SQL on client machine

I m deploying java desktop application on client machine.used mysql database,
how to connect to database without install any environment on client machine ?
You must ship/deliver your client (desktop) application with the JDBC driver (assuming you are NOT using ODBC) for MySQL...that's it!
UPDATE
Of course, make sure it is available in the Java classpath.

Apache Derby DB not working when I close NetBeans IDE

I am a beginner to Java and Databases.
Recently, i have been developing a Java Application where I am using derby database. When I start NetBeans and run the project it works fine. But when I close the IDE and run the .JAR file it does not not work. Can anybody tell me the reason behind it?
Netbeans is running Apache Derby for you in a networked mode, and your application is connecting to it. When netbeans is not running, then the database is not running either, so it's not available to your application.
If you want your application to run w/o a being able to reach a database over a network, your application must be changed to run Derby in embedded mode.
If you want your application to run connected to a networked version of Derby, you will need to arrange to run Derby somewhere that your application can connect to it.
Obviously it will not run as when you connect to the localhost:1527 port, it opens in Network mode (more exactly a client-server mode) which means : it will only work when the IDE is running which runs the Apache Derby DB Server and all other services.
You should try running it in Embedded Mode which will run your database even if you are not establishing a connection to the Derby server.
In a more simpler way I would say that it acts just like a file to your java executable, which manipulates data based on the schema you provide. You must try this fantastic and simple way Java, NetBeans and Derby Embedded. It will server your purpose. But before you begin Embedded you must add derby.jar file to your classpath.
Thanks!! Hope it helps

How to embed database derby in jar file

I do not want to install a database server on target machine. It is possible that we can simply ship a jar file with the database embedded in it, and not worry about installing a database server separately?
I created one application in netbeans using derby and it is working fine in my pc but when i am running on other machine it is giving me Error connecting to server localhost on port 1527 --> this error says that there is no Database running on port 1527. but i dont want that the client should take more efforts to start DB server and all technical process. It should be simply runnable when i start application Derby DB should start and when i close DB should close.
So what will be the solution for my problem?
And one more Question is Can i use derby database for large scale projects?
No, you don't have to install a database server on the target machine; Derby works just fine in embedded mode without a separate server.
However, if you want multiple client applications to be actively sharing the same data in the same database, you'll want to use the client-server mode, not the embedded mode.
And yes you can embed a jar with the database into your program. If you want to know how, read this documentation: http://db.apache.org/derby/docs/10.11/devguide/cdevdvlp24155.html
And yes you can use Derby for large scale projects. But it doesn't come with all the enterprise features (online backup, etc.) that a true enterprise scale commercial DBMS comes with.
But, since it sounds like you are just getting started with Derby, can I suggest that you please start with the Derby tutorials before you try these advanced configurations? Here is the link to the Derby tutorials: http://db.apache.org/derby/docs/10.11/getstart/
It may not be possible to give you very good advice without knowing a lot more about the application you're trying to build and the overall architecture you're trying to use.

How to connect Java desktop application to an online mysql database?

As titled, I want to make my desktop java application to connect to an online mysql db ?! How can I reach it such connection to be able to add and retrieve data?!
Download the connector from here. Add it to your classpath and in the code:
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://remoteUri/database-name?"
+ "user=user&password=userpw");
By all means, a JDBC connection is the way to go. But, you must make sure that the MySQL users have access to the database from another IP Address (i.e. your desktop application).
MySQL users are set up so that they can access the database on the local machine. Therefore, you have to allow access to the MySQL database from any host. Please see the following article which shows how to do that.
You could directly connect and let the queries run over the network (via JDBC)example: vogella.com
You build an RESTful Service, there are lots of tutorials as Example;
RESTful Web Services API using Java and MySQL

Categories

Resources