Blame view

src/main/java/com/huaheng/api/mes/utils/SqlServer.java 1.58 KB
易文鹏 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
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 {
14
15
16
17
        public static final String DB_URL = "10.10.0.17";
        public static final String DB_DatabaseName = "M10Cloud";
        public static final String DB_UserName = "sa";
        public static final String DB_Password = "567890@com";
易文鹏 authored
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    }

    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方法
35
     *
易文鹏 authored
36
37
38
39
     * @param sql
     * @return
     * @throws SQLException
     */
40
    public static ResultSet find(String sql) throws SQLException {
易文鹏 authored
41
42
        //获得连接
        dataBase();
43
        st = con.createStatement();
易文鹏 authored
44
45
        try {
            //对数据库进行数据查询
46
            res = st.executeQuery(sql);
易文鹏 authored
47
48
49
50
51
52
53
54
55
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

}