SqlServer.java 1.62 KB
package com.huaheng.api.mes.utils;

import java.sql.*;

public class SqlServer {

    static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    static String url = "jdbc:sqlserver://" + Const.DB_URL + ";DatabaseName=" + Const.DB_DatabaseName + "";
    static Connection con = null;
    static Statement st = null;
    static ResultSet res = null;

    public class Const {
        // TODO 完善数据库信息
        public static final String DB_URL = "192.168.100.111";
        public static final String DB_DatabaseName = "UFDATA_001_2017";
        public static final String DB_UserName = "wms";
        public static final String DB_Password = "wms8136";
    }

    public static void dataBase() {
        try {
            Class.forName(SqlServer.driver);
            con = DriverManager.getConnection(url, "" + Const.DB_UserName + "", "" + Const.DB_Password + "");
        } catch (ClassNotFoundException e) {
            System.err.println("装载 JDBC 驱动程序失败。");
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("无法连接数据库");
            e.printStackTrace();
        }
    }

    /**
     * 查询SQL方法
     * @param sql
     * @return
     * @throws SQLException
     */
    public static ResultSet find(String sql) throws SQLException{
        //获得连接
        dataBase();
        st=con.createStatement();
        try {
            //对数据库进行数据查询
            res=st.executeQuery(sql);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

}