[Mac / Java / Eclipse / PostgreSQL] Connect Java application and database

What to write in this article

-How to connect Java application and database when creating Java application with Eclipse installed on Mac

Characteristics of the person who wrote this article

・ Experience in creating web applications with Rails ・ No experience in creating web applications from scratch with Java

When creating a Rails app, for example

DB configuration command in Rails


rails model User name:String email:String
rails:db mingrate

You can connect automatically by executing.

In contrast, Java apps require you to configure each one yourself.

This time, I wrote this article to study the setting.

Development environment

Remarks
PC MacBook Air
OS MacOS Mojave Version: 10.14.4
language Java Version: 12.0.1
IDE Eclipse Eclipse IDE for Enterprise Java Developers. Version: 2019-03 (4.11.0)
WEB server Apache Tomcat Version: 8
DB PostgreSQL Version: 11.2
DB management tool pgAdmin Version: 4.5
browser Chrome version: 73.0.3683.103

Prerequisites

Prepare "User" table in database "sample" The following data is stored in the "User" table

SQL


SELECT * FROM USER;

SQL execution result on pgAdmin スクリーンショット 2019-04-20 21.22.17.png

File placement

Place "Sampleapp6.java" to write database connection process in dynamic WEB project "sample" スクリーンショット 2019-04-20 21.54.29.png

Code example

Sampleapp6.java


package jp.co.sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Sampleapp6 {
	
	public static void main(String[] args) throws Exception {
		
        String url = "jdbc:postgresql://localhost:5432/sample";
        String user = "sample database user";
        String password = "samle database password";

		
		//Connect to database
		try(Connection conn = DriverManager.getConnection(url,user,password)) {
			
			//Prepare a SELECT statement
			String sql = "SELECT * FROM CUSTOMER";
			PreparedStatement pStmt = conn.prepareStatement(sql);
			
			ResultSet rs = pStmt.executeQuery();
			
			//View results
			while (rs.next()) {
				String id = rs.getString("ID");
				String name = rs.getString("NAME");
				String email = rs.getString("EMAIL");
				
				System.out.println(id + " " + name + " " + email);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	};

point

** Apply JDBC driver **

Download the JDBC driver and save it (this time save it to your desktop) https://jdbc.postgresql.org/download.html スクリーンショット 2019-04-20 22.10.02.png

Right-click on the "sample" project → select "Properties" スクリーンショット 2019-04-20 21.58.56.png

Select "Java Build Path" → Apply the external build path saved on the desktop to "Module path" スクリーンショット 2019-04-20 22.02.43.png

Processing execution result

The image below is the Eclipse console I was able to confirm that I was getting the data from the database. D4mCbABUYAAPC_e.png

Impressions

In Rails, connecting a database is just a few lines of command. You have to configure all Java yourself.

I researched the DB connection method on the net, but I didn't know which information was correct and felt that it was difficult for me to study by myself.

What to do in the future

How to implement data save / edit / update / delete process in Java application

References

Daigo Kunimoto "Introduction to Servlet & JSP 2nd Edition", Impress Corporation, 2019/03/21 First Edition

Recommended Posts

[Mac / Java / Eclipse / PostgreSQL] Connect Java application and database
[Java / PostgreSQL] Connect the WEB application to the database
Download and install Eclipse (Java) (Mac version)
Connect from Java to PostgreSQL
Java development environment (Mac, Eclipse)
Connecting Java application and PostgreSQL with JDBC without using eclipse (execute at command prompt)
Eclipse installation and code completion enhancements (Mac for Java development)
[OpenCV3.2.0] Eclipse (Java) settings (for Mac)
What is Java and Development Environment (MAC)
Connect from Java to MySQL using Eclipse
Install java and android-sdk on Mac using homebrew
Put Java 11 and spring tool suite on mac
Java management and STS (Eclipse) build path settings
Connect to Aurora (MySQL) from a Java application
PostgreSQL Pub / Sub functionality and Java client implementation
Java 9+ application status
Java and JavaScript
XXE and Java
[Tutorial] Download Eclipse → Run the application with Java (Pleiades)
Install java and maven using brew on new mac
Install Eclipse on Mac and translate it into Japanese
Install Java / Tomcat / PostgreSQL without polluting your Mac environment
Create a JAVA WEB application and try OMC APM