---
# System prepended metadata

title: CountDB完整檔案

---

# CountDB完整檔案
```java=
public class CountDB {
// 常數命名規則為大寫
private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/guiproject?serverTimezone=UTC&useSSL=false&password=Password123";
private static final String JDBC_NAME = "root";
private static final String JDBC_PASSWORD = "Password123";
private static Connection conn = null;


static {
    try {
        Class.forName(JDBC_DRIVER);
        System.out.println("連接成功"); //如果裝有按鈕的java檔案做完了彈出提示框，可以將這個檔案中的這行刪除
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static Connection getConnection() {
    try {
        conn = DriverManager.getConnection(JDBC_URL, JDBC_NAME, JDBC_PASSWORD);
        System.out.println("連接成功");//如果裝有按鈕的java檔案做完了彈出提示框，可以將這個檔案中的這行刪除
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return conn;
}



public static void close(Connection comm) {
    if (comm != null) {
        try {
            comm.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public static void close(PreparedStatement ps) {
    if(ps !=null) {
        try {
            ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
    }
public static void close(Statement st) {
    if(st !=null) {
        try {
            st.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }	
}
}
public static void close(ResultSet rs) {
    if(rs !=null) {
        try {
            rs.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }	
}
}
}
```