Connecting different user to 1 server(database derby) in netbeans using java - java

I made an inventory system in netbeans using the language java and i connect it to derby as my database it is already working as a whole system but I want to have a client-server functionality what I mean is I want my program to have a server which holds the database(different computer) and different users with different computers which they can save/view data at the same time to the database means they are connected in one network.
Working with multiple connections to a single database.
What code can I use or method or do I have to import something?
I did some research and the only thing that I found is socket which can be used to create a chat between server and a client.
But I only tried the IP 127.0.0.1 for the client since I am making running the server and the client in the same computer.
And also can I open the connection of the server in the client form and send data like SQLQuery so I can save it in the database of the server?
Where can I see examples for these? Please help thanks

Yes, Derby supports a client-server configuration of your application.
Deploying your application in the client-server configuration is straightforward:
Deploy the Derby Network Server, for example by running the startNetworkServer script that is included with the Derby distribution.
Ensure that derbyclient.jar is in your application's CLASSPATH, and that you register "org.apache.derby.jdbc.ClientDriver" with the JDBC DriverManager.
Change your JDBC Connection URL from jdbc:derby:<db details> to jdbc:derby://<host:port>/<db details>.
If this is your first time using Derby, I strongly recommend working your way through the Derby tutorial at https://db.apache.org/derby/docs/10.12/getstart/index.html
For more information about running the Derby Network Server to service database requests for your applications, read the Derby Admin Guide: https://db.apache.org/derby/docs/10.12/adminguide/index.html

Use the IP 0.0.0.0 or for all connections in the server. The the connection url should include the name of the server or the ip address of the server in the network. When you use ip 127.0.0.1 or localhost derby can only accept connections to the database in the same machine, in this case localhost. All of this can be done by your network application server

Related

How to merge local database server into a localhost service?

I've been recently learning how databases work and how to write a service that can operate the database. So the situation is following:
I have a MySQL database ran on a localhost server (XAMPP). Accessing the database is simple: localhost:3036/my_database.
In addition to that, I wrote a Java Spring service (actually just copied the sample from https://github.com/spring-guides/gs-accessing-data-mysql.git) that is supposed to be an interface accepting curl requests and based on them to operate the database. So far so good. Everything works. The problem is that this service also runs on a localhost address: localhost:8080.
I would like to implement the MySQL server into my Spring service so that I can just run this service and the database will run and be hosted on the same port. The data is supposed to be available only through this service so I don't want an additional independent MySQL server to be running on a different localhost port.
At the end of the day, I would like to generate an executable jar file that when ran provides me the service on a localhost address and deals with locally stored database seamlessly.
Edit:
I wouldn't like my database to be independent form the service. I don't want to take care of running it and connecting to the service. What I'm searching for is sommething like #jr593 mentioned in the answers below, an embedded database. But is it possible for such databases to save the data locally on the device that the service is running beetwen service runs?
There is a few solution for this.
Change port for java app. It it's Spring Boot you could check property server.port and set it to e.g 8081.
You can chcek what is working on XAMPP on port 8080 and shoud down it (it could be apache or something else)
You don't need XAMPP to have MySQL on local host. You can stop/remove xampp and install standalone MySql, so you will take only port 3036 and nat 8080 and 3036.
You could use Docker with MySql image instead of XAMPP.

Connectng SQL Server 2012 with Java using JDBC

I am using sqljdbc4.jar for connecting SQL Server 2008 with java application using Windows authentication. Everything works perfectly up to date until I tried to run the same code with SQL Server 2012.
After research, I came to know that I need to enable the TCP/IP. But my problem is, my applications will be distributed to users in the form of .EXE and users will install the application themselves. Also, all users will have the SQL SERVER 2012 pre-installed, my application is not supposed to install the sql server.
Now my question is, how can I connect to SQL server without enabling TCP/IP manually? I heard about shared memory protocol also, but not sure how its connection string will look like?
I am using below connection url till SQL Server 2008:
jdbc:sqlserver://localhost;instanceName=SQLServer12;databaseName=Test;integratedSecurity=true;SelectMethod=direct;responseBuffering=adaptive
Please give your suggestions so I can try it here. Thanks in advance.
Try adding the property Network Library=dbmslpcn; OR Net=dbmslpcn; to the connection string for Shared Memory. The default value is dbmssocn (TCP/IP).
Network Library: The network library used to establish a connection to an instance of SQL Server. The corresponding network DLL must be installed on the system to which you connect. If you do not specify a network and you use a local server (for example, "." or "(local)"), shared memory is used.
I got this from: https://www.connectionstrings.com/all-sql-server-connection-string-keywords

Connecting system with any http site's database through my desktop application

Suppose, I have a website for a school to collect students information and data.And my site has a MySQL databse.So how can I or my desktop application get those data from my site's database?Basically, when I use MySQL for my desktop application I write some line of codes to connect with my local MySQL database which is given below.
public settingdatabase(){
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/rgacd","root", "");
stat = con.createStatement();
}catch(Exception ex){
System.out.println("Error: "+ex);
}
}
But when I try to connect with any HTTP site's database throough my desktop application what should I do.
Thanks In Advance. :D
Typically there are firewalls involved. Many networks running web servers will only accept incoming http/https connections (ports 80 and 443). Other ports (such as 3306 to connect to a mysql database) are closed to the outside world. The web server for the site often connects to an application server (e.g. Tomcat) that has network privileges to access the internal database.
So the simple answer is usually you can't directly connect to the database backing any arbitrary website because you don't have access to it.
Here is a somewhat typical architecture courtesy of Tivoli and an IBM slide deck:
By design, in most cases, direct access to the RDBMS is simply inaccessible to the users outside the firewall.
Now, in the unusual case that the firewall allowed an open port to connect to the database from any external machine, then you could connect to the machine via a jdbc network connection (the same as you are doing by accessing the database over localhost:3306). To establish such a connection you would need to use the publicly available address of the database machine (which may or may not be the same as the address used for the http connection, depending upon the network configuration of the system).

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

do i need a tomcat server to make java jdbc swing application to run on lan

I am attempting to create a MySQL and Java client app for my home network.On the server machine I successfully connected to the MySQL as root
Now I want to connect to MySQL from my client PC using the Java client program,
How to do this??
Do i need to install Tomcat Server to run on server for this.
I am using Windows 7 on all my clients and server machines.
Tomcat server is used for web applications.You just need to create a JDBC code and in the URL string just give the ip address of the system where mysql is installed.For example
connection = DriverManager
.getConnection("jdbc:mysql://192.168.1.205:3306/database_name","root", "password");
Also if you have not granted the permission in mysql then grant it .See this for granting permission for access to mysql on remote system
Depending on what you want to achieve, you need to establish a JDBC connection to the server. Take a look at the JDBC trail for more details.
This will allow you to connect directly from you client machine to your database server. This would be done using the required JDBC connection URL for the MySQL connection, for example jdbc:mysql://[host][,failoverhost...][:port]/[database] ยป
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]..., check out Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J for more details.
You may also need to configure your MySQL server to allow remote access before you can connect
You just need to do two things:
Make sure the server hosting MySQL allows incoming connections
Write JDBC code and use your MySQL server hostname/ipaddress in the jdbc string to connect and use it.

Categories

Resources