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