package com.mjm.solarInsertDB;

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

/**
 * Created by user on 2016/12/30.
 */
public class ConnectInformation {
    String ip;
    String database;
    String table;
    String user;
    String password;
    Connection connection;
    StringBuffer url = new StringBuffer();
//    String url;

    public ConnectInformation(String ip, String database, String user, String password,String table) {


        this.ip = ip;
        this.database = database;
        this.user = user;
        this.password = password;
        this.table = table;

        url.append("jdbc:sqlserver://").append(ip).append(":1433/").append(database).append("?useSSL=false&serverTimezone=UTC");
//        url.append("jdbc:mysql://").append(ip).append(":3306/").append(database).append("?useSSL=false&serverTimezone=UTC");

//        url.append("jdbc:mysql://").append(ip).append('/').append(database).append("?useUnicode=true&characterEncoding=utf-8");
        jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
        try {
            connection = DriverManager.getConnection(url.toString(), user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    public ConnectInformation() {
    }

    public Connection getSQLServerConnection(String ip, String database, String user, String password, String table){
        this.ip = ip;
        this.database = database;
        this.user = user;
        this.password = password;
        this.table = table;


        url.append("jdbc:sqlserver://").append(ip).append(":1433;databaseName=").append(database);
        System.out.println("url: "+url.toString());
        System.out.println("user: "+user);
        System.out.println("password: "+password);
        try {
            connection = DriverManager.getConnection(url.toString(), user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Connection getConnection(){
        return connection;

    }
    public void checkConnection() {
        try {
            if (connection.isClosed() || connection == null) {
//                Class.forName("com.mysql.cj.jdbc.Driver");
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                connection = DriverManager.getConnection(url.toString(), this.user, this.password);
            }
        } catch (SQLException e) {
            System.out.println("checkConnection: "+e.toString());
//            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        //定义加载驱动
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        //定义数据库用户
        String userName = "sa";
        //定义数据库密码
        String userPwd = "mjmadmin@99";
        //定义数据库连接对象
        Connection dbConn = null;
        //定义命令对象
        Statement stmt = null;
        try {
            //1.加载及注册驱动
            Class.forName(driverName);
            //2.定义数据库连接字符串
            String dbURL = "jdbc:sqlserver://192.168.0.132:1433;DatabaseName=solar_itri";
            //3.数据库连接
            dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
            System.out.println("asdfsfsdf====");
            System.out.println("连接数据库成功");
            //4.定义SQL语句
            String sql = "insert into data (c0) values(2318);";
            //5.创建一个执行sql对象
//            compile 'com.microsoft.sqlserver:mssql-jdbc:8.4.1.jre8'
            stmt = dbConn.createStatement();
            //6.执行SQL语句命令
//            uberjar files('FICLibs/mssql-jdbc-8.4.1.jre8.jar')
//            ResultSet resultSet = stmt.executeQuery(sql);
            boolean execute = stmt.execute(sql);
            //7.处理结果，并获取数据
//            while (resultSet.next()) {
//                String userID = resultSet.getString("userID");
//                String password = resultSet.getString("password");
//                String name = resultSet.getString("userName");
//                System.out.println(userID + "	" + password + "	" + name);
//            }
            //8.关闭连接
//            resultSet.close();
            dbConn.close();
            stmt.close();
        } catch (SQLException e) {      //数据库的处理异常的方法
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (dbConn != null) {
                try {
                    dbConn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
