用来接收数据库t表的类
package Bean ;
public class T {
private int id;
private String NAME;
private String sex;
private String birthday;
private String zhicheng;
private String yuanxi;
public T ( ) {
}
public T ( int id, String NAME, String sex, String birthday, String zhicheng, String yuanxi) {
this . id = id;
this . NAME = NAME;
this . sex = sex;
this . birthday = birthday;
this . zhicheng = zhicheng;
this . yuanxi = yuanxi;
}
public int getBid ( ) {
return id;
}
public void setBid ( int id) {
this . id = id;
}
public String getNAME ( ) {
return NAME;
}
public void setNAME ( String NAME) {
this . NAME = NAME;
}
public String getSex ( ) {
return sex;
}
public void setSex ( String sex) {
this . sex = sex;
}
public String getBirthday ( ) {
return birthday;
}
public void setBirthday ( String birthday) {
this . birthday = birthday;
}
public String getZhicheng ( ) {
return zhicheng;
}
public void setZhicheng ( String zhicheng) {
this . zhicheng = zhicheng;
}
public String getYuanxi ( ) {
return yuanxi;
}
public void setYuanxi ( String yuanxi) {
this . yuanxi = yuanxi;
}
@Override
public String toString ( ) {
return "t{" +
"id=" + id +
", NAME='" + NAME + '\'' +
", sex='" + sex + '\'' +
", birthday='" + birthday + '\'' +
", zhicheng='" + zhicheng + '\'' +
", yuanxi='" + yuanxi + '\'' +
'}' ;
}
}
最基础的BaseDAO封装数据基本操作
package DAO;
import java. lang. reflect. Field ;
import java. sql. * ;
import java. util. ArrayList ;
public abstract class BaseDAO {
public int update ( Connection connection, String sql, Object . . . args) {
PreparedStatement preparedStatement = null ;
try {
preparedStatement = connection. prepareStatement ( sql) ;
for ( int i = 0 ; i < args. length; i++ ) {
preparedStatement. setObject ( i+ 1 , args[ i] ) ;
}
int i = preparedStatement. executeUpdate ( ) ;
return i;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
try {
preparedStatement. close ( ) ;
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
}
}
return 0 ;
}
public < E > E select ( Connection connection, Class < E > clazz, String sql, Object . . . args) {
PreparedStatement preparedStatement = null ;
ResultSet resultSet = null ;
try {
preparedStatement = connection. prepareStatement ( sql) ;
for ( int i = 0 ; i < args. length; i++ ) {
preparedStatement. setObject ( i+ 1 , args[ i] ) ;
}
resultSet = preparedStatement. executeQuery ( ) ;
ResultSetMetaData metaData = resultSet. getMetaData ( ) ;
int columnCount = metaData. getColumnCount ( ) ;
if ( resultSet. next ( ) ) {
E e = clazz. newInstance ( ) ;
for ( int i = 0 ; i < columnCount; i++ ) {
Object value = resultSet. getObject ( i+ 1 ) ;
String columnLabel = metaData. getColumnLabel ( i + 1 ) ;
Field declaredField = clazz. getDeclaredField ( columnLabel) ;
declaredField. setAccessible ( true ) ;
declaredField. set ( e, value) ;
}
return e;
}
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
} catch ( InstantiationException e) {
e. printStackTrace ( ) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
} catch ( NoSuchFieldException e) {
e. printStackTrace ( ) ;
} finally {
try {
preparedStatement. close ( ) ;
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
}
try {
resultSet. close ( ) ;
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
}
}
return null ;
}
public < E > ArrayList < E > selectAll ( Connection connection, Class < E > clazz, String sql, Object . . . args) {
PreparedStatement preparedStatement = null ;
ResultSet resultSet = null ;
ArrayList < E > arrayList = null ;
try {
preparedStatement = connection. prepareStatement ( sql) ;
for ( int i = 0 ; i < args. length; i++ ) {
preparedStatement. setObject ( i+ 1 , args[ i] ) ;
}
resultSet = preparedStatement. executeQuery ( ) ;
connection. setTransactionIsolation ( Connection . TRANSACTION_READ_COMMITTED) ;
ResultSetMetaData metaData = resultSet. getMetaData ( ) ;
int columnCount = metaData. getColumnCount ( ) ;
arrayList = new ArrayList < > ( ) ;
while ( resultSet. next ( ) ) {
E e = clazz. newInstance ( ) ;
for ( int i = 0 ; i < columnCount; i++ ) {
Object value = resultSet. getObject ( i+ 1 ) ;
String columnLabel = metaData. getColumnLabel ( i + 1 ) ;
Field declaredField = clazz. getDeclaredField ( columnLabel) ;
declaredField. setAccessible ( true ) ;
declaredField. set ( e, value) ;
}
arrayList. add ( e) ;
}
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
} catch ( InstantiationException e) {
e. printStackTrace ( ) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
} catch ( NoSuchFieldException e) {
e. printStackTrace ( ) ;
} finally {
try {
preparedStatement. close ( ) ;
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
}
try {
resultSet. close ( ) ;
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
}
}
return arrayList;
}
public < E > E getvalue ( Connection connection, String sql, Object . . . args) {
PreparedStatement preparedStatement = null ;
ResultSet resultSet = null ;
try {
preparedStatement = connection. prepareStatement ( sql) ;
for ( int i = 0 ; i < args. length; i++ ) {
preparedStatement. setObject ( i+ 1 , args[ i] ) ;
}
resultSet = preparedStatement. executeQuery ( ) ;
if ( resultSet. next ( ) ) {
Object value = resultSet. getObject ( 1 ) ;
return ( E ) value;
}
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
} finally {
try {
resultSet. close ( ) ;
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
}
try {
preparedStatement. close ( ) ;
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
}
}
return null ;
}
}
用接口规范对于t表的常用操作
package DAO;
import Bean. T ;
import java. sql. Connection ;
import java. util. List ;
public interface TDAO {
void insert ( Connection connection, T t) ;
void delete ( Connection connection, int id) ;
void update ( Connection connection, T t) ;
T getTByid ( Connection connection, int id) ;
List < T > getAll ( Connection connection) ;
Long getCount ( Connection connection) ;
String getMAXBirthday ( Connection connection) ;
}
实现上面的接口来定制指定表的数据库操作
package DAO;
import Bean. T ;
import java. sql. Connection ;
import java. util. ArrayList ;
import java. util. List ;
public class TDAOImpl extends BaseDAO implements TDAO{
@Override
public void insert ( Connection connection, T t) {
String sql = "insert into t(NAME,sex,birthday,zhicheng,yuanxi) values(?,?,?,?,?)" ;
update ( connection, sql, t. getNAME ( ) , t. getSex ( ) , t. getBirthday ( ) , t. getZhicheng ( ) , t. getYuanxi ( ) ) ;
}
@Override
public void delete ( Connection connection, int id) {
String sql = "delete from t where id =?" ;
update ( connection, sql, id) ;
}
@Override
public void update ( Connection connection, T t) {
String sql = "update t set NAME =?,sex =?,birthday =?,zhicheng =?,yuanxi =? where id =?" ;
update ( connection, sql, t. getNAME ( ) , t. getSex ( ) , t. getBirthday ( ) , t. getZhicheng ( ) , t. getYuanxi ( ) , t. getBid ( ) ) ;
}
@Override
public T getTByid ( Connection connection, int id) {
String sql = "select id,NAME,sex,birthday,zhicheng,yuanxi from t where id =?" ;
T t = select ( connection, T . class , sql, id) ;
return t;
}
@Override
public List < T > getAll ( Connection connection) {
String sql = "select * from t" ;
ArrayList < T > ts = selectAll ( connection, T . class , sql) ;
return ts;
}
@Override
public Long getCount ( Connection connection) {
String sql = "select count(*) from t" ;
return getvalue ( connection, sql) ;
}
@Override
public String getMAXBirthday ( Connection connection) {
String sql = "select max(birthday) from t" ;
return getvalue ( connection, sql) ;
}
}