Skip to content

Instantly share code, notes, and snippets.

@Jach
Created August 4, 2011 19:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jach/1125957 to your computer and use it in GitHub Desktop.
Save Jach/1125957 to your computer and use it in GitHub Desktop.
Coding style comparison...
// ....
public RepositoryHolder getRepositories() throws AppException {
final String query = "select id, repo_url from localdb.sys_network.repositories";
List<Repository> repo_list = new ArrayList<Repository>();
Repository master = new Repository();
DB.execute(query, master, repo_list);
RepositoryHolder ret = new RepositoryHolder();
ret.repositories = repo_list;
return ret;
}
import java.sql.*;
// ....
public RepositoryHolder getRepositories() throws AppException {
final String query = "select id, repo_url from localdb.sys_network.repositories";
List<Repository> repo_list = new ArrayList<Repository>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Connection conn = DB.getConnection();
ps = conn.prepareStatement();
ps.execute();
rs = ps.getResultSet();
while (rs.next()) {
Repository repo = new Repository();
repo.id = rs.getInt(1);
repo.repo_url = rs.getString(2);
rep_list.add(repo);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
DB.releaseConnection();
}
try {
if (ps != null) {
ps.close();
}
} catch (SQLException ex1) {
ex1.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ex2) {
ex2.printStackTrace();
}
}
RepositoryHolder ret = new RepositorHolder();
ret.repositories = repo_list;
return ret;
}
// pretend file is Repository.java
import java.sql.SQLException;
import java.sql.ResultSet;
// ....
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement(name="repository")
public class Repository extends DBLoader<Repository> {
@XmlAttribute public int id;
@XmlAttribute public String repo_url;
private Repository copy;
public void loadRow(ResultSet rs) throws SQLException {
int c = 0;
Repository rep = new Repository();
rep.id = rs.getInt(++c);
rep.repo_url = rs.getString(++c);
copy = rep;
}
public void finalize() {
}
public Repository copy() { return copy; }
}
// pretend file is Repository.java
// ....
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement(name="repository")
public class Repository {
@XmlAttribute public int id;
@XmlAttribute public String repo_url;
}
@Jach
Copy link
Author

Jach commented Aug 4, 2011

+2 for Style 1.

@Jach
Copy link
Author

Jach commented Aug 4, 2011

+1 for Style 2. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment