MySQL : 5.7 Java : 8.1 Mac Eclipse
-MySQL already has a table etc. ・ You can connect without difficulty at the terminal etc.
Download JDBC for MySQL https://dev.mysql.com/downloads/connector/j/
** Download "Platform Independent (Architecture Independent), Compressed TAR Archive" **
Because there is mysql-connector-java-5.1.48-bin.jar
in the unzipped file
Put in / Tomcat / lib
of tomcat
(In my case, / Users / namari / apache-tomcat-9.0.27 / lib
)
Create SqlTest.java
appropriately in the project.
SqlTest.java
package chapter14;
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
/**
* Servlet implementation class SqlTest
*/
@WebServlet("/SqlTest")
public class SqlTest extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(
HttpServletRequest request,
HttpServletResponse response
) throws ServletException, IOException {
//This time, the book database on localhost
String url = "jdbc:mysql://localhost/book";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, "username", "password");
//Processing for database
msg = "ok";
} catch (SQLException | ClassNotFoundException e){
msg = "NG";
}
response.getWriter().println(msg);
}
}
Recommended Posts