This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
593869f
commit fca51be
Showing
8 changed files
with
159 additions
and
0 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
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
10 changes: 10 additions & 0 deletions
10
src/main/java/io/hardingadonis/saledock/utils/IDBContext.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,10 @@ | ||
package io.hardingadonis.saledock.utils; | ||
|
||
import java.sql.*; | ||
|
||
public interface IDBContext { | ||
|
||
public Connection getConnection(); | ||
|
||
public void closeConnection(Connection connection); | ||
} |
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
64 changes: 64 additions & 0 deletions
64
src/main/java/io/hardingadonis/saledock/utils/impl/DBContextMySQLImpl.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,64 @@ | ||
package io.hardingadonis.saledock.utils.impl; | ||
|
||
import java.io.*; | ||
import java.sql.*; | ||
import java.util.*; | ||
|
||
import io.hardingadonis.saledock.utils.*; | ||
|
||
public class DBContextMySQLImpl implements IDBContext { | ||
|
||
private String dbURL; | ||
private String user; | ||
private String password; | ||
|
||
public DBContextMySQLImpl() { | ||
try (InputStream input = DBContextMySQLImpl.class.getClassLoader().getResourceAsStream("config.mysql.properties")) { | ||
Properties prop = new Properties(); | ||
|
||
if (input == null) { | ||
this.dbURL = "jdbc:mysql://localhost:3306/saledock"; | ||
this.user = "root"; | ||
this.password = ""; | ||
} else { | ||
prop.load(input); | ||
|
||
this.dbURL = "jdbc:mysql://" + prop.getProperty("config.jdbc.url") + ":3306/" + prop.getProperty("config.jdbc.database_name"); | ||
this.user = prop.getProperty("config.jdbc.user"); | ||
this.password = prop.getProperty("config.jdbc.password"); | ||
} | ||
} catch (IOException ex) { | ||
System.err.println(ex.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public Connection getConnection() { | ||
Connection connection = null; | ||
|
||
try { | ||
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver()); | ||
|
||
connection = DriverManager.getConnection(this.dbURL, this.user, this.password); | ||
|
||
System.out.println("Connect MySQL successfully!"); | ||
} catch (SQLException ex) { | ||
System.err.println(ex.getMessage()); | ||
} | ||
|
||
return connection; | ||
} | ||
|
||
@Override | ||
public void closeConnection(Connection connection) { | ||
try { | ||
if (connection != null) { | ||
connection.close(); | ||
|
||
System.out.println("Close MySQL successfully!"); | ||
} | ||
} catch (SQLException ex) { | ||
System.err.println(ex.getMessage()); | ||
} | ||
} | ||
} |
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,4 @@ | ||
config.jdbc.url=localhost | ||
config.jdbc.database_name=saledock | ||
config.jdbc.user=root | ||
config.jdbc.password= |