persondaoimpl.java

来自「Spring2.0宝典」· Java 代码 · 共 62 行

JAVA
62
字号
package lee;

import java.sql.Statement;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;
/**
 * @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;
	public PersonDaoImpl()
	{
		dc = DBConn.instance();
	}
	public void createPerson(PersonBean p)throws Exception
	{
		stmt = dc.openStmt();
		stmt.execute("insert into person_test(p_name,p_age) values('" + p.getName() + "'," + p.getAge()+")");
	}
	public PersonBean getPerson(int id) throws Exception
	{
		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"));
	}
	public List findPersonsByName(String name) throws Exception
	{
		stmt = dc.openStmt();
		String sql = "select * from person_test where p_name like '%" + name + "%'";
		ResultSet rs = stmt.executeQuery(sql);
		List result = new ArrayList();
		while (rs.next())
		{
			result.add(new PersonBean(rs.getString("p_name"),rs.getInt("p_age")));
		}
		return result;
	}
	public void deletePerson(int id) throws Exception
	{
		stmt = dc.openStmt();
		stmt.execute("delete from person_test where p_id = " + id);
	}
	public void deletePersonsByAge(int startAge , int EndAge) throws Exception
	{
		stmt = dc.openStmt();
		stmt.execute("delete from person_test where p_age between " + startAge + " and " + EndAge);
	}
	public void updatePerson(PersonBean pb)throws Exception
	{
		stmt = dc.openStmt();
		stmt.execute("update person_test set p_name = '" + pb.getName() +  "' , p_age=" + pb.getAge() + " where p_id =" + pb.getId());
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?