📄 persondaoimpl.java
字号:
package lee;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.SQLException;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class PersonDaoImpl implements PersonDao
{
private Statement stmt;
private DBConn dc;
private Log log = LogFactory.getLog(this.getClass());
public PersonDaoImpl()
{
dc = DBConn.instance();
}
public void createPerson(PersonBean p)throws DbAccessException
{
try
{
stmt = dc.openStmt();
stmt.execute("insert into person_test(p_name,p_age) values('" + p.getName() + "'," + p.getAge()+")");
}
catch (SQLException sqle)
{
log.info(sqle.getMessage());
throw new DbAccessException("数据库访问异常");
}
}
public PersonBean getPerson(int id) throws DbAccessException
{
try
{
stmt = dc.openStmt();
ResultSet rs = stmt.executeQuery("select * from person_test where p_id = " + id);
return new PersonBean(rs.getString("p_name"),rs.getInt("p_age"));
}
catch (SQLException sqle)
{
log.info(sqle.getMessage());
throw new DbAccessException("数据库访问异常");
}
}
public List findPersonsByName(String name) throws DbAccessException
{
ResultSet rs = null;
List result = new ArrayList();
try
{
stmt = dc.openStmt();
String sql = "select * from person_test where p_name like '%" + name + "%'";
rs = stmt.executeQuery(sql);
while (rs.next())
{
result.add(new PersonBean(rs.getString("p_name"),rs.getInt("p_age")));
}
}
catch (SQLException sqle)
{
log.info(sqle.getMessage());
throw new DbAccessException("数据库访问异常");
}
return result;
}
public void deletePerson(int id) throws DbAccessException
{
try
{
stmt = dc.openStmt();
stmt.execute("delete from person_test where p_id = " + id);
}
catch (SQLException sqle)
{
log.info(sqle.getMessage());
throw new DbAccessException("数据库访问异常");
}
}
public void deletePersonsByAge(int startAge , int EndAge) throws DbAccessException
{
try
{
stmt = dc.openStmt();
stmt.execute("delete from person_test where p_age between " + startAge + " and " + EndAge);
}
catch (SQLException sqle)
{
log.info(sqle.getMessage());
throw new DbAccessException("数据库访问异常");
}
}
public void updatePerson(PersonBean pb)throws DbAccessException
{
try
{
stmt = dc.openStmt();
stmt.execute("update person_test set p_name = '" + pb.getName() + "' , p_age=" + pb.getAge() + " where p_id =" + pb.getId());
}
catch (SQLException sqle)
{
log.info(sqle.getMessage());
throw new DbAccessException("数据库访问异常");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -