Java JDBC by mySQL,mariaDB
General Steps to Creating, Compiling and Running Java Programs
Applets in Unix & Linux server
- Open a Unix window.
Now, install mariaDB or mySQL and install connectot/J
For example:
Setting class path
The JDBC connector will be located in the path
/usr/share/java/
. We can export the classpath as,export CLASSPATH=/usr/share/java/mysql-connector-java.jar:$CLASSPATH
Grant privileges to user
For the user connecting to the database, the privileges should be granted. This can be achieved using the below
mysql
query.GRANT ALL PRIVILEGES ON *.* TO ‘user’@’%’ IDENTIFIED BY ‘<user-password>’ WITH GRANT OPTION;
Now, we can establish a connection to this machine's
mysql
server using the java program but only as alocalhost
. If we need to use this server's name or connect from some other machine, we will get several exceptions as discussed here and here.Edit
/etc/hosts
fileIt is to be noted that in the above step if I just used the IP address of the machine rather than the hostname of the machine, it worked. So, I suspected the issue was something related to
dns
name resolution.I modified my
/etc/hosts
file to have the below entry.127.0.0.1 servername.edu localhost
I was still not able to connect to the database using the hostname which I thought was pretty weird given that I have changed the
/etc/hosts
file as well. I even stoppedip6tables
to check if that is the issue. However, none of the options worked and it was giving me the exception as,ERROR: java.net.UnknownHostException:connection refused
Edit /etc/my.cnf file
The last exception was promising. It says connection refused rather than saying something like not able to resolve the hostname. So, I thought something should be changed in the
/etc/my.cnf
file and I added the below lines to the file.port=3306 bind-address=0.0.0.0
When I made the above changes, I could connect to this machine from anywhere using the hostname
Using the editor of your choice, create some Java source code.
For example, suppose we want a class named "JDBCTest",
then we would start vi, emacs, pico, or whatever editor with the name of the Java file:
or
orThe source code, JDBCTest.java, might look like this:
Don't forget to save the file!- Double check the name of the Java program file you just saved
by listing out the files in the current directory: - Assuming we saved the Java source code (as in step 4) from within your editor,
we can now go back to the command prompt and compile it.
This is done with the javac compiler command:
'my_lesson > _JAVA' 카테고리의 다른 글
Java Applet (1) | 2016.02.16 |
---|---|
Java Applications (0) | 2016.02.16 |
Java Platform Standard Edition 8 Documentation (0) | 2016.02.16 |
댓글