Package club.data

Source Code of club.data.ConnectDB

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package club.data;

import club.beans.ConfigBean;
import club.ui.ConnectDBDialog;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
*
* @author tuan2
*/
public class ConnectDB {

    public static Connection conn = null;

    public static Connection connectMSDB() throws ClassNotFoundException {
        try {
            DataConfig data = new DataConfig();
            ConfigBean bean = new ConfigBean();
            try{
                bean = data.readConfigFile();
            } catch(Exception e){
                (new ConnectDBDialog()).setVisible(true);
                return null;
            }
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://" + bean.getDBServer()  + bean.getDBPort() + ";"
                    + "databaseName=" + bean.getDBName() + ";user=" + bean.getDBUser() + ";password=" + bean.getDBPass() + ";";
            conn = DriverManager.getConnection(connectionUrl);
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Connect Database is failed");
        } catch (NullPointerException e){
            JOptionPane.showMessageDialog(null, "Error reading file");
            (new ConnectDBDialog()).setVisible(true);
            return null;
        }
        return conn;
    }

    public static Connection connectMSDB1() throws ClassNotFoundException {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://localhost:1433;"
                    + "databaseName=club;user=test;password=test;";
            conn = DriverManager.getConnection(connectionUrl);
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Connect Database is failed");
        }
        return conn;
    }
    public static Connection connectMSDB2() throws ClassNotFoundException {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=ShowRoomManagementSystem", "sa" , "");
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Connect Database is failed");
        }
        return conn;
    }
    public static boolean testConnect(String host, String port, String user, String pass, String name){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://" + host  + port + ";"
                    + "databaseName=" + name + ";user=" + user + ";password=" + pass + ";";
            conn = DriverManager.getConnection(connectionUrl);
//            JOptionPane.showMessageDialog(null, "Connect Database is success");
            return true;
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
  //          JOptionPane.showMessageDialog(null, "Connect Database is failed");
            return false;
        }
    }
}
TOP

Related Classes of club.data.ConnectDB

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.