天天看點

封裝basedao

package com.huawei.common;

import java.sql.ResultSet;

import java.sql.SQLException;

public interface CallBack {

void execute(ResultSet rs) throws SQLException;

}

package com.huawei.utils;

import java.lang.reflect.Field;

import com.huawei.domain.Address;

import com.huawei.domain.Users;

/**

* bean 工具

* @author Administrator

*

*/

public class BeanUtil {

/**

* 擷取set方法名

* 擷取标名

* @param clazz

*/

public static String getTableName(Class<?> clazz){

//擷取類名

String name = clazz.getSimpleName();

name = name.substring(0, 1).toLowerCase()+name.substring(1);

return name;

}

* @param field

* @return

public static String setter(Field field){

String name = field.getName();

return "set"+name.substring(0,1).toUpperCase()+name.substring(1);

public static String getter(Field field){

return "get"+name.substring(0,1).toUpperCase()+name.substring(1);

public static void main(String[] args) {

System.out.println(getTableName(Address.class));

public static String getColumnName(Field field){

return name.substring(0,1).toLowerCase()+name.substring(1);

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement;

public class DBUtil {

* 得到資料庫連接配接

public static Connection getConnection(){

//顯示加載驅動

try {

Class.forName("com.mysql.jdbc.Driver");

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");

return connection;

} catch (ClassNotFoundException e) {

e.printStackTrace();

}catch (SQLException e) {

}

return null;

*

* 釋放資源

* @param objects

public static void close(Object ...objects){

if(objects!=null && objects.length>0){

try {

for(Object o:objects){

if(o instanceof ResultSet){

((ResultSet)o).close();

}else if(o instanceof Statement){

((Statement)o).close();

}else if(o instanceof Connection){

((Connection)o).close();

}

}

} catch (SQLException e) {

e.printStackTrace();

}

System.out.println(getConnection());

package com.huawei.base;

import java.io.Serializable;

import java.lang.reflect.Array;

import java.lang.reflect.Method;

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;

import java.sql.PreparedStatement;

import java.sql.ResultSetMetaData;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import com.huawei.common.CallBack;

import com.huawei.utils.BeanUtil;

import com.huawei.utils.DBUtil;