22 August 2016

Kết nối Database Mysql trong Java (Pattern Connection)

Connection to Database Mysql
Java 2016
public class DBUtil {

    private static Connection connection = null;

    public static Connection getConnection() {
        if (connection != null) {
            return connection;
        } else {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/project_name", "user", "pass");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
    }
    //Test connection
    public static void main(String[] args) {
        System.out.println(getConnection());
    }

}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang