Skip to content

Instantly share code, notes, and snippets.

@lamngockhuong
Last active December 29, 2017 09:46
Show Gist options
  • Select an option

  • Save lamngockhuong/190726d563c6218d29d7c2aac9bdb297 to your computer and use it in GitHub Desktop.

Select an option

Save lamngockhuong/190726d563c6218d29d7c2aac9bdb297 to your computer and use it in GitHub Desktop.
[Database Connection Util] #java #connection
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* https://ngockhuong.com
*/
public class DatabaseConnection {
private static String db = "bnews";
private static String url = "jdbc:mysql://localhost:3306/" + db + "?useUnicode=true&characterEncoding=UTF-8";
private static String user = "root";
private static String password = "";
private static Connection connection = null;
public static Connection getConnection() {
try {
// tìm và nạp driver: (java8 không cần)
Class.forName("com.mysql.jdbc.Driver");
// tạo kết nối giữa java vs hqt csdl
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment