Map found = (Map) queryRunner.query("select id, name, age from person", h);
Map jane = (Map) found.get(new Long(1)); // jane's id is 1
String janesName = (String) jane.get("name");
Integer janesAge = (Integer) jane.get("age");
int a1 = (Integer) null;
boolean x1 = (Boolean)null;
//正确
Integer a2 = (Integer) null;
Boolean x2 = (Boolean)null;
id bigint(20) NOT NULL AUTO_INCREMENT,
name varchar(24) DEFAULT NULL,
age int(11) DEFAULT NULL,
address varchar(120) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=gbk
private Long id;
private String sdf;
private String address2;
private Integer age;
public Person() {
}
public Person(String sdf) {
this.sdf = sdf;
}
public Person(String sdf, Integer age, String address) {
this.sdf = sdf;
this.age = age;
this.address2 = address;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSdf() {
return sdf;
}
public void setSdf(String sdf) {
this.sdf = sdf;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address2;
}
public void setAddress(String address2) {
this.address2 = address2;
}
}
import com.lavasoft.common.DBToolkit;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2010-1-25 21:00:29
*/
public class PersonDAOImpl implements PersonDAO {
private static PersonDAOImpl instance = new PersonDAOImpl();
public static PersonDAOImpl getInstance() {
return instance;
}
public static void main(String[] args) {
//错误
int a1 = (Integer) null;
boolean x1 = (Boolean) null;
//正确
Integer a2 = (Integer) null;
Boolean x2 = (Boolean) null;
getInstance().save(null);
// getInstance().save(null);
// getInstance().save(null);
// getInstance().save(null);
// getInstance().save(null);
getInstance().update(null);
getInstance().load(null);
getInstance().load4Map(null);
}
@Override
public Long save(String sql) {
Long id = null;
String ins_sql = "INSERT INTO person (NAME, age, address) VALUES ('aaa', 21, 'address001')";
Connection conn = DBToolkit.getConnection();
QueryRunner qr = new QueryRunner();
try {
qr.update(conn, ins_sql);
//获取新增记录的自增主键
id = (Long) qr.query(conn, "SELECT LAST_INSERT_ID()", new ScalarHandler(1));
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
return id;
}
@Override
public int delete(Long id) {
int x = 0;
Connection conn = DBToolkit.getConnection();
QueryRunner qr = new QueryRunner();
try {
x = qr.update(conn, "DELETE FROM person WHERE id = ?", id);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
return x;
}
@Override
public int update(Person person) {
int x = 0;
Connection conn = DBToolkit.getConnection();
QueryRunner qr = new QueryRunner();
try {
x = qr.update(conn, "UPDATE person SET NAME = ?, age = ?, address = ? WHERE id = ?", "xxx", 23, "ttt", 5);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
return x;
}
@Override
public Person load(Long id) {
Connection conn = DBToolkit.getConnection();
QueryRunner qr = new QueryRunner();
try {
Person person = (Person) qr.query(conn, "SELECT * FROM person where id = ?", new BeanHandler(Person.class), 3L);
System.out.println(person.getId() + "\t" + person.getSdf() + "\t" + person.getAge() + "\t" + person.getAddress());
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public List<Person> findPerson(String sql) {
Connection conn = DBToolkit.getConnection();
QueryRunner qr = new QueryRunner();
try {
List<Person> pset = (List) qr.query(conn, "SELECT * FROM person", new BeanListHandler(Person.class));
for (Person person : pset) {
System.out.println(person.getId() + "\t" + person.getSdf() + "\t" + person.getAge() + "\t" + person.getAddress());
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public Person load4Map(Long id) {
Connection conn = DBToolkit.getConnection();
QueryRunner qr = new QueryRunner();
try {
//先将两个字段置为null
qr.update(conn, "update person set age = null,address =null where id =1");
Map<String, Object> map = qr.query(conn, "SELECT * FROM person where id = ?", new MapHandler(), 1L);
Person person = new Person();
person.setId((Long) map.get("id"));
person.setSdf((String) map.get("name"));
person.setAge((Integer) map.get("age"));
person.setAddress((String) map.get("address"));
System.out.println(person.getId() + "\t" + person.getSdf() + "\t" + person.getAge() + "\t" + person.getAddress());
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}