├── db.properties ├── src ├── db │ ├── DbException.java │ └── DB.java └── application │ └── Program.java ├── .gitignore ├── .project └── .classpath /db.properties: -------------------------------------------------------------------------------- 1 | user=root 2 | password=1234567 3 | dburl=jdbc:mysql://localhost:3306/coursejdbc 4 | useSSL=false 5 | -------------------------------------------------------------------------------- /src/db/DbException.java: -------------------------------------------------------------------------------- 1 | package db; 2 | 3 | public class DbException extends RuntimeException { 4 | private static final long serialVersionUID = 1L; 5 | 6 | public DbException(String msg) { 7 | super(msg); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/application/Program.java: -------------------------------------------------------------------------------- 1 | package application; 2 | 3 | import java.sql.Connection; 4 | 5 | import db.DB; 6 | 7 | public class Program { 8 | 9 | public static void main(String[] args) { 10 | 11 | Connection conn = DB.getConnection(); 12 | DB.closeConnection(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | # Eclipse 26 | .settings/ 27 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | jdbc1 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/db/DB.java: -------------------------------------------------------------------------------- 1 | package db; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.IOException; 5 | import java.sql.Connection; 6 | import java.sql.DriverManager; 7 | import java.sql.SQLException; 8 | import java.util.Properties; 9 | 10 | public class DB { 11 | 12 | private static Connection conn = null; 13 | 14 | public static Connection getConnection() { 15 | if (conn == null) { 16 | try { 17 | Properties props = loadProperties(); 18 | String url = props.getProperty("dburl"); 19 | conn = DriverManager.getConnection(url, props); 20 | } 21 | catch (SQLException e) { 22 | throw new DbException(e.getMessage()); 23 | } 24 | } 25 | return conn; 26 | } 27 | 28 | public static void closeConnection() { 29 | if (conn != null) { 30 | try { 31 | conn.close(); 32 | } catch (SQLException e) { 33 | throw new DbException(e.getMessage()); 34 | } 35 | } 36 | } 37 | 38 | private static Properties loadProperties() { 39 | try (FileInputStream fs = new FileInputStream("db.properties")) { 40 | Properties props = new Properties(); 41 | props.load(fs); 42 | return props; 43 | } 44 | catch (IOException e) { 45 | throw new DbException(e.getMessage()); 46 | } 47 | } 48 | } 49 | --------------------------------------------------------------------------------