用jTDS通过JDBC连:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.4</version>
</dependency>
public static List<String> executeQuery(String sql,String columns) {
try {
Connection conn = getConnection();
Statement st = conn.createStatement();
ResultSet set = st.executeQuery(sql);
List<String> result = new ArrayList<String>();
while (set.next()) {
String[] columnList = columns.split(",");
for(String str:columnList){
result.add(set.getString(str));
}
}
set.close();
st.close();
conn.close();
return result;
} catch (SQLException e) {
throw new IllegalArgumentException(e);
}
}
public static Connection getConnection() {
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String url = "jdbc:jtds:sqlserver://localhost:1433;DatabaseName=jdl";
String username = "sa";
String password = "";
Connection conn = DriverManager.getConnection(url, username,
password);
return conn;
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}