CounterWebAppV6 based on H2 database

Preface

Hello! This is a cloud application system development technology. Previously we wrote a Java program for CounterWebApp, which is based on memory storage and will be reset (from scratch) after the web application is restarted. This new version of the task will use the H2 file database as the storage of the counter, so that the counter can continue to count after the web application is restarted. The technology stack for web application development is limited to Spring Boot + MyBatis + Thymeleaf.

Software and technology used

Software: Eclipse, Xshell, Filezilla
Others: tomcat, jdk, H2 database
Platform: Alibaba Cloud

accomplish

Environment configuration

tomcat

Eclipse: After importing the project, click window->preference->server->Runtime Environments->add to add your own locally configured tomcat (tomcat is already configured by default), and click OK.

Insert image description hereAdd tomcat8 to the server and try to start it
Insert image description hereIf the tomcat8 is started successfully, the following words "started" will appear, which means it is successful.

Insert image description here

H2 database

Website download H2 database
Website:link:http://www.h2database.com/ html/main.html
Insert image description here
After downloading, open your document and click h2.bat
Insert image description hereIt will open the browser and open the H2 database operation background Configure H2DRIVERS variables.
Insert image description here
Note: When the background is connected to the h2 database, the web program cannot be connected to the h2 database at the same time. If you need to run the program, please exit the background link.
Insert image description here

Insert image description here

Code

We use eclipse on the java side.
The project structure is as follows:
Project structure
There are three main documents: counter.jps, User.java, JDBCUtils.java
The following are Narration: counter.jps

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1" import="JDBC.copy.JDBCUtils,com.cqust.User"%>

<html><body>

<%@ page language="java" %>

        <!-- 每次加载这个页面就调用一次updata方法 -->
<% JDBCUtils.updata(Integer.parseInt(JDBCUtils.selet().getCOUNTER_VALUE()));
System.out.println("++"+JDBCUtils.selet());
%>        

<h1>Maven + Spring MVC Web Project Example</h1>
<h2>Message : <%=JDBCUtils.selet().getCOUNTER_NAME()%></h2>       
<h2>COUNTER : <%= JDBCUtils.selet().getCOUNTER_VALUE()%></h2>  

</body></html>

User.java

package com.cqust;

public class User {
    
           //存放数据
private String COUNTER_ID;
private String COUNTER_NAME;
private String COUNTER_VALUE;
@Override
public String toString() {
    
    
	return "User [COUNTER_ID=" + COUNTER_ID + ", COUNTER_NAME=" + COUNTER_NAME + ", COUNTER_VALUE=" + COUNTER_VALUE
			+ "]";
}
public String getCOUNTER_ID() {
    
    
	return COUNTER_ID;
}
public void setCOUNTER_ID(String cOUNTER_ID) {
    
    
	COUNTER_ID = cOUNTER_ID;
}
public String getCOUNTER_NAME() {
    
    
	return COUNTER_NAME;
}
public void setCOUNTER_NAME(String cOUNTER_NAME) {
    
    
	COUNTER_NAME = cOUNTER_NAME;
}
public String getCOUNTER_VALUE() {
    
    
	return COUNTER_VALUE;
}
public void setCOUNTER_VALUE(String cOUNTER_VALUE) {
    
    
	COUNTER_VALUE = cOUNTER_VALUE;
}

}

JDBCUtils.java

package JDBC.copy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.cqust.User;

public class JDBCUtils {
    
    

	
	//建立程序与h2数据库的连接
	public static Connection getconnection() throws SQLException, ClassNotFoundException {
    
    

		// String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
		// String username="root";
		// String password="";
		// Class.forName("com.mysql.jdbc.Driver");  
		
		String url = "jdbc:h2:~/H2/test";
		String username = "sa";
		String password = "";
		Class.forName("org.h2.Driver");
//连接H2数据库
		Connection con = DriverManager.getConnection(url, username, password); //获取了连接
		System.out.println("hear+++++");
		return con;
	}
//updata是更新数据库数据方法,
	public static void updata(int s) {
    
    
		Statement stmt = null;     //
		Connection conn = null;

		try {
    
    

			conn = getconnection();
			System.out.println("hello");
			int p = s + 1;         //value数据加一
			String UpdateSql = "UPDATE COUNTERS SET COUNTER_VALUE=" + p + " WHERE COUNTER_ID=2"; //数据库语句
			int l = conn.createStatement().executeUpdate(UpdateSql);   //执行语句
		} catch (Exception e) {
    
    
			// TODO: handle exception
		} finally {
    
    
			try {
    
    
				if (stmt != null) {
    
    
					stmt.close();
					stmt = null;
				}
				if (conn != null) {
    
    
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
//查找数据
	public static User selet() {
    
    
		Statement stmt = null;
		Connection conn = null;

		try {
    
    
			// System.out.println("selet?");
			conn = getconnection();
			
			System.out.println(conn.toString());
			
			ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM COUNTERS where COUNTER_ID=2;");//执行的结果给rs
			
			ArrayList<User> us = new ArrayList<>();
			
			while (rs.next()) {
    
         //
				User user = new User();
				user.setCOUNTER_ID(rs.getString("COUNTER_ID"));
				user.setCOUNTER_NAME(rs.getString("COUNTER_NAME"));
				user.setCOUNTER_VALUE(rs.getString("COUNTER_VALUE"));
				us.add(user);
				
				System.out.println(us.toString());
			}
			
			return us.get(0);
			
		} catch (Exception e) {
    
    
			return null;
			// TODO: handle exception
		} finally {
    
    
			try {
    
    
				if (stmt != null) {
    
    
					stmt.close();
					stmt = null;
				}
				if (conn != null) {
    
    
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}
	//释放连接
	public static void release(ResultSet re, Statement sta, Connection con) {
    
    
		try {
    
    
			if (re != null) {
    
    
				re.close();
			}
			if (sta != null) {
    
    
				sta.close();
			}
			if (con != null) {
    
    
				con.close();
			}
		} catch (Exception e) {
    
    
			// TODO: handle exception
		}
	}
}

Windows side test

The browser backend exits the H2 database connection
and then runs as a server to get the result.
Insert image description here
Now we connect to H2 in the background to view the data, and we can see that the data is consistent.
Insert image description here

2. Web application cloud deployment

2.1 Server environment configuration

2.1.1 Java environment configuration

Use X shell to remotely connect to the Alibaba Cloud server. The command is as follows:

[C:\~]$ ssh [email protected]

The first step is to use FileZilla to upload JDK1.8 to the virtual machine, decompress it to the /usr/lib/jvm/ directory, and then add the Java PATH path under the global environment variable, as follows:

export export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64export
export JRE_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
export PATH=$JAVA HOME/bin :$PATH

The second step is to verify whether Java is configured successfully, as shown in the figure
Insert image description here

Since then, the virtual machine Java environment has been successfully configured.

2.1.2 Tomcat server configuration

The first step is to use FileZilla to upload the Tomcat installation package to the virtual machine, and then extract it to the user directory.

The second step is to configure Tomcat’s Java environment and go to the following directory

Paige@Norton:~$ cd apache-tomcat-9.0.44/bin

Then edit the startup.sh file and add the following lines:

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:%{JAVA_HOME}/lib:%{JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
export TOMCAT_HOME=/home/Paige/apache-tomcat-9.0.44

Open the browser and enter: 47.106.80.46:8080 as shown below:

Insert image description here
Since then, the Tomcat server has been deployed successfully.

2.2 Deploy applications

The first step is to package the project file into a .War package, and then upload it to the following directory of the Alibaba Cloud server:

/opt/tomcat/apache-tomcat-8.5.68/webapps

Then open the Tomcat server:
Insert image description here
Open the browser and enter 47.106.80.46:8080
Insert image description here

Guess you like

Origin blog.csdn.net/Norton_Paige/article/details/118026680