一些基础的关于java jdbc的程序代码
public static List<Country> query(String sql) {
Connection conn = DBConnection.getConnection();
List<Country> list = new ArrayList<Country>();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Country cou = new Country();
cou.setName(rs.getString(1));
cou.setRegion(rs.getString(2));
cou.setArea(rs.getString(3));
cou.setPopulation(rs.getString(4));
cou.setGdp(rs.getString(5));
list.add(cou);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
public static List<Country> upPage() {
List<Country> list = null;
if (i > 2) {
x = ((--i) - 1) * 5;
String sql = "select top "
+ x
+ " * from (select * from bbc where name not in(select top " + x + " name from bbc order by name)) as A ";
list = query(sql);