Connect To MySQL Database in Java API and Deploy It On Tomcat Server(Local Server & Remote Server)

How To Develop And Deploy Your Web App (Angular + Tomcat Server + MySQL) — Part 5

Mia
4 min readSep 14, 2020

Part 1: Develop Frontend Page With Angular
Part 2: Install Tomcat On Server (Local & Remote)
Part 3: Develop Java API Running On Tomcat Server
Part 4: Install MySQL And Create Database (Local & Remote Server)
Part 5: Connect To MySQL Database in Java API and Deploy It On Tomcat Server (Local Server & Remote Server)

Taipei 101 in Taiwan (By Mia)

This is an extremely simple web app example for beginners and I spent only few days learning and building this web. I hope this will give a hint for those who want to learn how to build and deploy a web both frontend and backend.

  • Connect To MySQL Database in Java API and Deploy It On Tomcat Server (Local Server)
  • Connect To MySQL Database in Java API and Deploy It On Tomcat Server (Remote Server)
  • Conclution

Connect MySQL Database in Java API and Deploy It On Tomcat Server (Local Server)

Key Code In Java API Program

Create db package and a DBConnect.java class under db package.

Create db package
Create DBConnect.java under db package

Modify DBConnect.java class:

package db;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DBConnect {
public String test = "";
public void ConnectDB() {
String sql = "select * from student"; // SQL query
Connection conn = null; // connect to db
Statement st = null; // database statement
ResultSet rs = null; // database result
try {
// register jdbc Driver
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tomcat_test", "root", "");
// Create statement object
st = conn.createStatement();
rs = st.executeQuery(sql);
// Iterate the ResultSet
while (rs.next()) {
test = rs.getString("student_name") + " " + rs.getString("student_gender");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
rs.close();
} catch (Exception e2) {
// TODO: handle exception
}
try {
st.close();
} catch (Exception e3) {
// TODO: handle exception
}
try {
conn.close();
} catch (Exception e4) {
// TODO: handle exception
}
}
}
}
DBConnect.java

Modify sayHtmlHello() function and display the data from dbConnect:

Original code in Hello.java
Modified code in Hello.java

Add jdbc Driver dependency in pom.xml file. Make sure to click the maven button to reload dependency.

pom.xml

Deploy On Local Server

Open Project Structure settings and config the Tomcat file.

Project Structure settings

Select all maven libraries and Put into /WEB-INF/lib, then click OK.

Notes: If the war file doesn’t include these libraries, it may have 500 error when connecting database.

Project Structure settings

Finish the config, then Build Artifacts.

Build Artifacts
Build Artifact

When it’s done, there’s out directory.

Build Artifacts finished

Start the program by clicking Run.

Notes: Make sure you’ve stop Tomcat using shutdown.sh command mentioned above on your local server. Otherwise, the program can not start correctly.

TomcatTest

Deploy The Project On Tomcat

  • Deploy on local computer
  • Deploy on remote computer

Conclusion

These are the completed procedures how I develop and deploy my web from frontend to backend. Thank you for watching and sorry for my poor English. Anyways, hope this will help you guys!

More…

The following parts could be found in my other articles.

References

--

--