[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java

Related article

-[Java] I installed JDBC and tried to connect with servlet + MySQL. (There is a version using DAO / Bean)

Reference site

JDBC driver introduction

  1. [Download] mysql-connector-java-8.0.18.zip from the official website (https://dev.mysql.com/downloads/connector/j/)
  2. Unzip and extract mysql-connector-java-5.1.48-bin.jar
  3. Put it in / WebContent / WEB-INF / lib / mysql-connector-java-5.1.48-bin.jar

Create context.xml

Create in / WebContent / META-INF / context.xml

context.xml


<?xml version="1.0" encoding="UTF-8" ?>
<Context>
	<Resource name = "jdbc/book"
              auth = "Container"
              type = "javax.sql.DataSource"
              driverClassName = "com.mysql.jdbc.Driver"
              url      = "jdbc:mysql://localhost/book"
              username = "namari"
              password = "password">
	</Resource>
</Context>

Servlet

Create in /Sample/webapps/book/WEB-INF/src/chapter14/All.java

All.java


package chapter14;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;

/**
 * Servlet implementation class All
 */
@WebServlet("/All")
public class All extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public All() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		try {
			//Get connection
			InitialContext ic = new InitialContext();
			DataSource ds = (DataSource) ic.lookup("java:/comp/env/jdbc/book");
			Connection con = ds.getConnection();

			//SQL statement transmission
			PreparedStatement st = con.prepareStatement("select * from product");
			//Execution & result reception
			ResultSet rs = st.executeQuery();

			//Data display
			while (rs.next()) {
				out.println(rs.getInt("id") + ":" + rs.getString("name") + ":" + rs.getInt("price"));
			}

			//Database disconnection
			st.close();
			con.close();

		} catch (Exception e) {
			//Connection / SQL statement error
			e.printStackTrace(out);

		} // try
	}

}

Recommended Posts

[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
[Java] I installed JDBC and tried to connect with servlet + MySQL. (There is a version using DAO / Bean)
I tried using OpenCV with Java + Tomcat
I tried to break a block with java (1)
Connect to MySQL 8 with Java
I started MySQL 5.7 with docker-compose and tried to connect
I tried to interact with Java
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
Create a memo app with Tomcat + JSP + Servlet + MySQL using Eclipse
Connect from Java to MySQL using Eclipse
I tried to make a talk application in Java using AI "A3RT"
Connect to Aurora (MySQL) from a Java application
java I tried to break a simple block
I tried to set tomcat to run the Servlet.
I tried to implement a server using Netty
[Java] Connect to MySQL
I tried using a database connection in Android development
I tried to implement TCP / IP + BIO with JAVA
[Java 11] I tried to execute Java without compiling with javac
I tried to operate SQS using AWS Java SDK
I tried to create a Clova skill in Java
I tried to make a login function in Java
I tried using Log4j2 on a Java EE server
I tried OCR processing a PDF file with Java
I tried to implement Stalin sort with Java Collector
I tried scraping a stock chart using Java (Jsoup)
I want to connect to Heroku MySQL from a client
[Template] MySQL connection with Java
Connect to DB with Java
I tried using Java REPL
I tried to make an introduction to PHP + MySQL with Docker
[Rails] I tried to create a mini app with FullCalendar
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried using Hotwire to make Rails 6.1 scaffold a SPA
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I tried using Dapr in Java to facilitate microservice development
I tried to make a client of RESAS-API in Java
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
I tried to create a padrino development environment with Docker
I tried OCR processing a PDF file with Java part2
I tried to get started with Swagger using Spring Boot
I tried using the CameraX library with Android Java Fragment
How to register as a customer with Square using Tomcat
I made a Dockerfile to start Glassfish 5 using Oracle Java
Easy to make LINE BOT with Java Servlet Part 2: I tried image messages and templates
I tried using JOOQ with Gradle
I tried UDP communication with Java
I tried using Java8 Stream API
I tried using JWT in Java
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried using Java memo LocalDate
I tried using GoogleHttpClient of Java
A story that I struggled to challenge a competition professional with Java
I tried to build an http2 development environment with Eclipse + Tomcat
[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor
I tried to build a simple application using Dockder + Rails Scaffold