Last active
December 29, 2017 09:46
-
-
Save lamngockhuong/190726d563c6218d29d7c2aac9bdb297 to your computer and use it in GitHub Desktop.
[Database Connection Util] #java #connection
This file contains hidden or 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
| 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