-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
1,228 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 | ||
http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>github.donsez.bd</groupId> | ||
<artifactId>jdbc-postgresql</artifactId> | ||
<version>0.1.0-SNAPSHOT</version> | ||
<name>postgresql</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<postgresql.version>42.7.1</postgresql.version> | ||
<!-- Remark : This Postgres JDBC Driver had vulnerabilities --> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<version>${postgresql.version}</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.0</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
</project> |
39 changes: 39 additions & 0 deletions
39
jdbc-postgresql/src/main/java/com/mkyong/jdbc/ConnectOnlyExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.mkyong.jdbc; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.Properties; | ||
|
||
public class ConnectOnlyExample { | ||
|
||
public static void main(String[] args) { | ||
|
||
// https://docs.oracle.com/javase/8/docs/api/java/sql/package-summary.html#package.description | ||
// auto java.sql.Driver discovery -- no longer need to load a java.sql.Driver class via Class.forName | ||
|
||
// register JDBC driver, optional since java 1.6 | ||
/*try { | ||
Class.forName("org.postgresql.Driver"); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
}*/ | ||
|
||
// auto close connection | ||
try (Connection conn = DriverManager.getConnection( | ||
"jdbc:postgresql://127.0.0.1:5432/test", "postgres", "changeme")) { | ||
|
||
if (conn != null) { | ||
System.out.println("Connected to the database!"); | ||
} else { | ||
System.out.println("Failed to make connection!"); | ||
} | ||
|
||
} catch (SQLException e) { | ||
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
jdbc-postgresql/src/main/java/com/mkyong/jdbc/ListEmployeeExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.mkyong.jdbc; | ||
|
||
import com.mkyong.jdbc.model.Employee; | ||
|
||
import java.math.BigDecimal; | ||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ListEmployeeExample { | ||
|
||
public static void main(String[] args) { | ||
|
||
System.out.println("PostgreSQL JDBC Connection Testing ~"); | ||
|
||
// https://docs.oracle.com/javase/8/docs/api/java/sql/package-summary.html#package.description | ||
// auto java.sql.Driver discovery -- no longer need to load a java.sql.Driver class via Class.forName | ||
|
||
// register JDBC driver, optional since java 1.6 | ||
// Class.forName("org.postgresql.Driver"); | ||
|
||
List<Employee> result = new ArrayList<>(); | ||
|
||
String SQL_SELECT = "Select * from EMPLOYEE"; | ||
|
||
// auto close connection and preparedStatement | ||
try (Connection conn = DriverManager.getConnection( | ||
"jdbc:postgresql://127.0.0.1:5432/test", "postgres", "changeme"); | ||
PreparedStatement preparedStatement = conn.prepareStatement(SQL_SELECT)) { | ||
|
||
ResultSet resultSet = preparedStatement.executeQuery(); | ||
|
||
while (resultSet.next()) { | ||
|
||
long id = resultSet.getLong("ID"); | ||
String name = resultSet.getString("NAME"); | ||
BigDecimal salary = resultSet.getBigDecimal("SALARY"); | ||
Timestamp createdDate = resultSet.getTimestamp("CREATED_DATE"); | ||
|
||
Employee obj = new Employee(); | ||
obj.setId(id); | ||
obj.setName(name); | ||
obj.setSalary(salary); | ||
// Timestamp -> LocalDateTime | ||
obj.setCreatedDate(createdDate.toLocalDateTime()); | ||
|
||
result.add(obj); | ||
|
||
} | ||
result.forEach(x -> System.out.println(x)); | ||
|
||
} catch (SQLException e) { | ||
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.