14 June 2016

Kết nối databse đến mysql workbench trong Java Connection with mysql workbench using jdbc

Database Connection
Java 2016
package com.sample.soap.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
 private static Connection connection = null;
 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
 static final String DB_URL = "jdbc:mysql://localhost:3306/tutorial";
 static final String DB_USER = "root";
 static final String DB_PASS = "123456";

 public static Connection getConnection() {
  if (connection != null) {
   return connection;
  } else {
   try {
    Class.forName(JDBC_DRIVER);
    connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
   } catch (ClassNotFoundException e) {
    // TODO: handle exception
    e.printStackTrace();
   } catch (SQLException e) {
    // TODO: handle exception
    e.printStackTrace();
   }
   return connection;
  }
 }
}
Nếu thấy 1 dòng thông báo như trên hình thì kết nối thành công
Ảnh minh họa trên có 3 lần kết nối nên hiển thị 3 dòng



0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang