private Connection getConnection() throws SQLException {
OracleDataSource ods = new OracleDataSource();
String url = "jdbc:oracle:thin:test/test@localhost:1521:orcl";
ods.setURL(url);
Connection conn = ods.getConnection();
return conn;
}
public ArrayList
ArrayList
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
int i = 0;
while (rs.next()) {
ArrayList
for (i = 1; i <= cols; ++i) {
if (rs.getString(i) == null) {
alRow.add("");
} else {
alRow.add(rs.getString(i));
}
}
alRS.add(alRow);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
throw new RuntimeException("数据库访问异常");
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException sqle2) {
sqle2.printStackTrace();
throw new RuntimeException("资源关闭异常");
}
}
return alRS;
}
将SQL语句查询出的数据装载到数据容器中,C#用的DataSet,Java我用的ArrayList
你这段代码的意思是 : 执行sql语句并且更新数据库 然后返回执行后的数据库表内容