How to connect a database with a web application developed in Eclipse
To check when you forget
Graduated from the Faculty of Education, National University in 2011 (at that time, I had no interest in programming) After graduating from university, worked at Isetan, Recruit, etc. Around 2016-Start studying programming by self-education (You will be able to develop apps with Rails) Worked as SE from 2018
In Rails, DB settings could be processed automatically by entering a command. In Java, I have to set it myself, so I wrote this article to study it.
PC: Mac OS: MacOS Mojave Language: Java IDE: Eclipse DB: PostgreSQL DB administration tool: pgAdmin
-The database of the connection destination is the local environment (port: 5432). ・ Customer table exists in sample database -The following sample data is stored in the customer table.
SQL
SELECT * FROM customer;
Execution result
id | name | email | password
------+----------+---------------------+----------
0001 | sample1 | [email protected] | password
0002 | sample2 | [email protected] | password
0003 | sample3 | [email protected] | password
0004 | sample4 | [email protected] | password
0005 | sample5 | [email protected] | password
0006 | sample6 | [email protected] | password
0007 | sample7 | [email protected] | password
0008 | sample8 | [email protected] | password
0009 | sample9 | [email protected] | password
0010 | sample10 | [email protected] | password
(10 rows)
・ This WEB application is created as a Dynamic WEB Project. ・ This WEB application assumes to access http: // localhost: 8080 / sample / sample2
Sampleapp2.java
package jp.co.sample;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class Sampleapp2 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<style>table,th,td,tr{border: 1px solid black};</style>");
out.println("<body>");
out.println("<h1>Sampleapp by Java Servlet</h1>");
out.println("</body>");
out.println("</html>");
String url = "jdbc:postgresql://localhost:5432/sample";
String user = "USERNAME";
String password = "PASSWORD";
try{
Class.forName("org.postgresql.Driver");
System.out.println("Successful database connection");
} catch(Exception e) {
e.printStackTrace();
}
try {
Connection conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM customer";
ResultSet rset = stmt.executeQuery(sql);
out.println("<table>");
out.println("<tr><th>ID</th><th>UserName</th><th>Email</th></tr>");
while(rset.next()) {
out.println("<tr><td>" + rset.getString("id") + "</td><td>" + rset.getString("name") + "</td><td>" + rset.getString("email") + "</td></tr>");
}
out.println("</table>");
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
Place the **. Jar file ** in the WEB-INF / lib
directory
Run the web app server and access http: // localhost: 8080 / sample / sample2
Eclipse console
May 12, 2019 4:26:33 pm org.apache.catalina.startup.VersionLoggerListener log
information: Server version name: Apache Tomcat/9.0.14
(Omitted)
May 12, 2019 4:26:35 pm org.apache.catalina.startup.Catalina start
information:Start the server[1,215]ms
Successful database connection
Check the screen with a browser It was confirmed that the data stored in the database was displayed on the browser.
Save / edit / update / delete data Cooperation with DB placed on AWS
In Rails, a DB that can be set automatically by writing a few lines of commands and executing them. In Java, it is troublesome to have to set each one by yourself.
Recommended Posts