Experimental environment is as follows:
youxi1 CentOS7.5 Tomcat8.5.35 192.168.5.101
youxi2 CentOS7.5 Mysql5.7.20 192.168.5.102
(1). Mysql link to download the official package
URL: https://dev.mysql.com/downloads/connector/j/ , select Platform Independent (platform-independent, that is, regardless of the platform). I am here to download the mysql-connector-java-8.0.13.tar.gz.
After the download is complete uploaded to the Tomcat server, I have here is youix1.
(2) Create test data on the mysql server and authorized
[root@youxi2 ~]# mysql -uroot -p123456 mysql> create database test_db; Query OK, 1 row affected (0.00 sec) mysql> use test_db; Database changed mysql> create table java_tb(id int,name varchar(20)); Query OK, 0 rows affected (0.02 sec) mysql> insert into java_tb values(1,'zhangsan'),(2,'lisi'); Query OK, 2 rows affected (0.06 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from java_tb; +------+----------+ | id | name | +------+----------+ | 1 | zhangsan | | 2 | lisi | +------+----------+ 2 rows in set (0.00 sec) mysql> grant all on test_db.* to 'tomcat_user'@'192.168.5.101' identified by 'tomcatpassword'; Query OK, 0 rows affected, 1 warning (0.02 sec) mysql> flush privileges; //刷新 Query OK, 0 rows affected (0.00 sec)
(3) Unzip the file, copy the jar package to the installation directory of tomcat lib folder, and finally restart tomcat
[root@youxi1 ~]# tar zxf mysql-connector-java-8.0.13.tar.gz -C /usr/local/src/ //解压 [root@youxi1 ~]# cp /usr/local/src/mysql-connector-java-8.0.13/mysql-connector-java-8.0.13.jar /usr/local/tomcat8.5/lib/ //复制jar包 [root@youxi1 ~]# ps aux | grep /usr/local/tomcat8.5/ root 2559 0.2 10.0 2325180 99932 ? Sl 12:32 0:21 /usr/local/jdk1.8.0_191/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8.5/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
-Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat8.5/bin/bootstrap.jar:/usr/local/tomcat8.5/bin/tomcat-juli.jar
-Dcatalina.base = / usr / local / tomcat8.5 -Dcatalina.home = / usr / local / tomcat8.5 -Djava.io.tmpdir = / usr / local / tomcat8.5 / temp org.apache.catalina.startup Start .Bootstrap the root 992 2982 0.0 0.0 112.72 thousand PTS / S 0 + 14:33 0:00 grep --color = Auto /usr/local/tomcat8.5/ [youxi1 the root @ ~] -9 # 2559 // kill the kill tomcat [root @ youxi1 ~] # ps // invoke the command ps at the beginning of the last! ps the AUX | grep /usr/local/tomcat8.5/ // display the full command root 2985 0.0 0.0 112720 992 pts / 0 R + 14:33 0 : 00 = grep --color Auto /usr/local/tomcat8.5/ [root @ youxi1 ~] # /usr/local/tomcat8.5/bin/startup.sh the Using CATALINA_BASE: /usr/local/tomcat8.5 the Using CATALINA_HOME: /usr/local/tomcat8.5 the Using CATALINA_TMPDIR: /usr/local/tomcat8.5/temp the Using JRE_HOME: /usr/local/jdk1.8.0_191 Using CLASSPATH: /usr/local/tomcat8.5/bin/bootstrap.jar:/usr/local/tomcat8.5/bin/tomcat-juli.jar Tomcat started. [root@youxi1 ~]# !ps ps aux | grep /usr/local/tomcat8.5/ root 2995 109 6.1 2263640 60904 pts/0 Sl 14:33 0:03 /usr/local/jdk1.8.0_191/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8.5/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
-Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat8.5/bin/bootstrap.jar:/usr/local/tomcat8.5/bin/tomcat-juli.jar
-Dcatalina.base=/usr/local/tomcat8.5 -Dcatalina.home=/usr/local/tomcat8.5 -Djava.io.tmpdir=/usr/local/tomcat8.5/temp org.apache.catalina.startup.Bootstrap start root 3015 0.0 0.0 112720 992 pts/0 R+ 14:33 0:00 grep --color=auto /usr/local/tomcat8.5/
(4) Create a test page
[youxi1 the root @ ~] Vim /usr/local/tomcat8.5/webapps/ROOT/mysql.jsp # <% @ Page Import = "the java.sql. *"%> <HTML> <body> <% the Class.forName ( "com.mysql.cj.jdbc.Driver") newInstance ();.
// url contains Mysql IP address (192.168.5.102), port number (3306), database (test_db), connected user name and password (tomcat_user and tomcatpassword), using the character set (UTF-8) String url = "jdbc: MySQL: //192.168.5.102: 3306 / TEST_DB the User = tomcat_user & password = tomcatpassword & useUnicode = to true & characterEncoding = UTF-8?"; Connection conn = DriverManager.getConnection (URL); the Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String = SQL "SELECT * from java_tb"; the ResultSet stmt.executeQuery RS = (SQL); while(rs.next()){ %> id:<%=rs.getString(1)%> name:<%=rs.getString(2)%><br><br> <%}%> <% out.print("Congratulations!!! JSP connect MYSQL IS OK!!"); %> <% rs.close(); stmt.close(); conn.close(); %> </body> </html>
Require the same character set and database side.
View on Windows