1.在后台配置好需要连接的数据源
2.代码
public Connection getConnection(String datasourceid) {
Connection conn = null;
try {
weaver.interfaces.datasource.DataSource datasource = (weaver.interfaces.datasource.DataSource) StaticObj.getServiceByFullname(datasourceid, weaver.interfaces.datasource.DataSource.class); //获取数据源的信息
conn = datasource.getConnection(); //和数据源取得连接
} catch (Exception e) {
}
return conn;
}
public void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
Connection conn = getConnection("datasource.testoa"); datasource.+数据源名
PreparedStatement s = conn.prepareStatement("select count(*) as counthrm from hrmresource");
ResultSet rs = s.executeQuery();
if (rs.next()) {
String counthrm = rs.getString("counthrm");
//输出到控制台
out.write("人数:" +counthrm);
}
rs.close();//关闭记录集
s.close();//关闭statement
//批量处理
RecordSet rs = new RecordSet();
String sql = " insert into uf_materialInfo(name) values(?)";
List<List> updateParList = new ArrayList<>();
for(int i =0;i<10;i++)
{
List<Object> list = new ArrayList<>();
list.add(i);
updateParList.add(list);
}
rs.executeBatchSql(sql,updateParList);