?

public ? class ?DBHelper? {????
????
public ? static ?Connection?getConnection()? {????
????????Connection?conn?
= ? null ;????
????????
try ? {????
????????????Class.forName(
" com.mysql.jdbc.Driver " );????
????????????conn?
= ?DriverManager.getConnection( " jdbc:mysql://localhost/ad?useUnicode=true&characterEncoding=GBK&jdbcCompliantTruncation=false " ,????
????????????????????
" root " ,? " root " );????
????????}
? catch ?(Exception?e)? {????
????????????e.printStackTrace();????
????????}
????
????????
return ?conn;????
????}
????
}
????
???
????????
/* dao中的方法 */ ???
????
public ?List < Adv > ?getAllAdvs()? {????
????????????
????????Connection?conn?
= ? null ;????
????????ResultSet?rs?
= ? null ;????
????????PreparedStatement?stmt?
= ? null ;????
????????String?sql?
= ? " select?*?from?adv?where?id?=?? " ;????
????????List
< Adv > ?advs? = ? new ?ArrayList < Adv > ();????
???
????????conn?
= ?DBHelper.getConnection();????
????????
if ?(conn? != ? null )? {????
????????????
try ? {????
????????????????stmt?
= ?conn.prepareStatement(sql);????
????????????????????????????????stmt.setInt(
1 ,? new ?Integer( 1 ));????
????????????????rs?
= ?stmt.executeQuery();????
???
????????????????
if ?(rs? != ? null )? {????
????????????????????
while ?(rs.next())? {????
????????????????????????Adv?adv?
= ? new ?Adv();????
????????????????????????adv.setId(rs.getLong(
1 ));????
????????????????????????adv.setName(rs.getString(
2 ));????
????????????????????????adv.setDesc(rs.getString(
3 ));????
????????????????????????adv.setPicUrl(rs.getString(
4 ));????
???
????????????????????????advs.add(adv);????
????????????????????}
????
????????????????}
????
????????????}
? catch ?(SQLException?e)? {????
????????????????e.printStackTrace();????
????????????}
? finally ? {????
????????????????
try ? {????
????????????????????stmt.close();????
????????????????????conn.close();????
????????????????}
? catch ?(SQLException?e)? {????
????????????????????e.printStackTrace();????
????????????????}
????
????????????}
????
????????}
????
????????
return ?advs;????
????}
?