如图,我们创建了一个标准的javabean
public class Account {
private String Account;
private int balance;
public String getAccount() {
return Account;
}
public void setAccount(String account) {
Account = account;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}
同时,构造了一个Dao的select方法
public Account select(String account) throws SQLException {
ResultSet resultSet=null;
PreparedStatement preparedStatement=null;
Account account1=null;
Connection connection=null;
if (account==null){
return null;
}else{
try {
connection=jdbcUtils.getConnection();
String sql="select * from acount where account=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setObject(1,account);
resultSet=preparedStatement.executeQuery();
if (resultSet.next()){
account1=new Account();
account1.setAccount(resultSet.getString("Account"));
account1.setBalance(resultSet.getInt("balance"));
}
return account1;
} catch (SQLException e) {
return null;
}finally {
jdbcUtils.close(connection,preparedStatement, resultSet);
}
}
}
我们于代码起始段以 Account account=null将其初始化
后续想调用javabean中的代码,只需要再次设置account=new Account即可.