SqlServer.java
1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
}
}
}