Java: Connect to SQL on Apache locally - java

I have a site running a Linux/Apache/MySQL/PHP stack. I would like to connect to the database from a locally run Java program. Is this possible? (Deploying the Java program to the server would be a great effort.)

Yes, of course; as long as the Java program has the correct credentials, that the user it logs in with is given proper access from the database, etc.
From the perspective of the database, it has no idea who is connecting to it or what technology they are connecting from - all it sees is just another connection.
See Establishing a Connection in the same JDBC tutorial I linked to earlier.

Related

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

Working with two databases from different domains within a same server

I have two domains.
In domain A, I have a PHP web site and in domain B, I have Java Website.
Now I want to use both database from both site.
How can I do that? I am using MySql 5.7.17
You could set up a third server dedicated to mySQL and share that database with the 2 web servers, or you can just run a mySQL database on each of those computers and use the IP address or domain name of them to connect to each other's server
Explanation:
You currently have a java website running and a php website running on one machine if i understand correctly.
What you need to do is download mySQL, and go through the tutorial to get the server up and running https://dev.mysql.com/doc/mysql-getting-started/en/
Then you need to download the mysql Java driver, and the mysql PHP driver
Then you need to use the drivers to connect to the same mySQL server from the code, which would be on localhost if you are doing it all on one machine.
MySQL is a server. It is its own thing, not a database like SQLite that you create in the java code or php code. You create and set it up separately as it's own server, and then any other server can connect to it
You can open and close a mysql connection any time.
mysql_connect(server, username, password);
Do some stuff with one database, then you can use:
mysql_close();
mysql_connect(server2, username2, password2);
as many times as you like.
Or you can use resource links:
$db1 = mysql_connect(server1, user1, passwd1);
$db2 = mysql_connect(server2, user2, passwd2);
But in this case, each mysql command needs to be referenced with the resource link:
mysql_query(query, $db1);

Can a java program connect to a DB when MySql Workbench is NOT running?

I am currently making a piece of software as a project in college. I have my program hooked up to a database to store and retrieve information about students after they enroll. I was bored and clicked on the JAR file while i did not have workbench opened, but i could still log in to the system and retrieve information.
My lecturer told us, during the lecture about implementing a database, that we needed workbench up and running to achieve the maximum functionality. Was she wrong?
When a MySQL (or any other SQL) server is running, you can always connect to it (technically, of course you may need username/password and you may have to care about firewall restrictions). The MySQL Workbench is just another client that connects to the database, so it is not needed to be running to let a Java program establish a connection to the database.
Thoughts: I think your lecturer meant that you will not be able to check whether all your queries where executed correctly when not having another view onto the database to check the data.
The MySQL server is running regardless of whether or not MySQL Workbench (the database administration tool) is active/running or not. Those are two different things.
Moreover, you are doing the same that the tools is doing: connecting to the running MySQL server instance in order to do some actions.

jdbc driver does not work in java

In java, I use jdbc connect to sqlite. the following sentence works,
Connection conn =DriverManager.getConnection("jdbc:sqlite:C://Users//13149//Desktop//SqliteDB0.sqlite");
When I replace this with the following one.
Connection conn =DriverManager.getConnection("jdbc:sqlite://localhost:3306//Database//SqliteDB0.sqlite")
The first time, it does not give any error message, only stop there. However, as I run it again, it always gives error message. Anyone can help me? Thanks.
Maybe you have gotten the wrong idea. A SQLite database is simply a file...so, the first setup you provided works fine.
The setup you provided would have worked fine on MySQL, for example, since there's a database server running on 3306 port. Since SQLite isn't a client/server database, but a local file, the only viable option is pointing to its physical file.
In case this second setup is supposed to be running on your application server, you just have to create the SQLite physical file and point the connection URL to it, just like you did in first place.
If you really need, for example, to have the database running on another server (different from your application server) you should consider migrating to MySQL/PostgreSQL. Both of them will allow you client/server connection.

Testing MySQL database on LAN client

I am attempting to create a MySQL DB and Java client app for my home network. I haven't really had any experience with MySQL other than PHPMyAdmin for a website backend (also have used SQLite). I have downloaded the full MySQL installation and a test DB from the MySQL website. On the server machine I successfully connected to the DB as root. Not a difficult task.
Now I want to connect to the DB from my client PC, just to check I can. Eventually I will use the JDBC driver to connect from my Java client app, but before that I just want to check I can connect.
How should I do this? SHould I just install the MySQL command line program onto the client PC?
EXTRA INFO: forgot to add, I'm using Windows 7 on all my machines.
Yes, you could install the MySQL to get MySQL command line program and connect with it, but also you can use another MySQL client tool like PHPMyAdmin, dbForge Studio for MySQL or another one.
To connect from remote host you should create special accout for it, e.g. - 'user_name'#'your_host_name'. Find more information here -
Specifying Account Names
Account Names and Passwords

Categories

Resources