将一段C#写的代码改成java的

2024-12-18 22:36:41
推荐回答(2个)
回答1:

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 query(String sql) {
ArrayList alRS = new 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 alRow = new 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

回答2:

你这段代码的意思是 : 执行sql语句并且更新数据库 然后返回执行后的数据库表内容