package com.fengjie.dao;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.fengjie.entity.实体类名;
通用方法必须实体类名和表名一模一样
public class BaseDao {
//打开数据库连接
public static Connection getConn() {
Connection conn = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/数据库名”, “root”, “数据库密码”);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//关闭数据库连接
public static void closeAll(ResultSet rs,PreparedStatement pstmt,Connection conn) {
try {
if (rs!=null) {
rs.close();
}
if (pstmt!=null) {
pstmt.close();
}
if (conn!=null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//通用添加数据
public int add(Object object) {
Connection conn = BaseDao.getConn();
PreparedStatement pstmt = null;
int row = 0;
try {
Class c = object.getClass();
Field[] fields = c.getDeclaredFields();
Field.setAccessible(fields,true);
StringBuffer sBufferuffer = new StringBuffer("insert into ");
sBufferuffer.append(c.getSimpleName() +"(");
for (int i = 1; i < fields.length; i++) {
sBufferuffer.append(fields[i].getName());
if (i<fields.length-1) {
sBufferuffer.append(",");
}
}
sBufferuffer.append(") values(");
for (int i = 1; i < fields.length; i++) {
sBufferuffer.append("?");
if (i<fields.length-1) {
sBufferuffer.append(",");
}
}
sBufferuffer.append(")");
System.out.println(sBufferuffer);
pstmt = conn.prepareStatement(sBufferuffer.toString());
for (int i = 1; i < fields.length; i++) {
pstmt.setObject(i, fields[i].get(object));
}
row = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(null, pstmt, conn);
}
return row;
}
//通用查询数据
public List<Object> findAll(Object object){
Connection conn = BaseDao.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Object> list = new ArrayList<Object>();
try {
Class c = object.getClass();
Field[] fields = c.getDeclaredFields();
Field.setAccessible(fields, true);
String sql = "select * from " +c.getSimpleName();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
Object object2 = c.newInstance();
for (int i = 0; i < fields.length; i++) {
fields[i].set(object2,rs.getObject(fields[i].getName()));
}
list.add(object2);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(rs, pstmt, conn);
}
return list;
}
//通用修改数据
public int update(Object object) {
Connection conn = BaseDao.getConn();
PreparedStatement pstmt = null;
int row = 0;
try {
Class c = object.getClass();
Field[] fields = c.getDeclaredFields();
Field.setAccessible(fields, true);
StringBuffer sBuffer=new StringBuffer("update "+c.getSimpleName() +" set ");
for (int i = 1; i < fields.length; i++) {
sBuffer.append(fields[i].getName()+"=?");
if(i<fields.length-1) {
sBuffer.append(",");
}
}
sBuffer.append(" where "+fields[0].getName()+" =?");
pstmt = conn.prepareStatement(sBuffer.toString());
for (int i = 1; i < fields.length; i++) {
pstmt.setObject(i, fields[i].get(object));
}
pstmt.setObject(fields.length, fields[0].get(object));
row = pstmt.executeUpdate();
System.out.println(sBuffer);
} catch (Exception e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(null, pstmt, conn);
}
return row;
}
//通用删除数据
public int delete(Object object) {
Connection conn = BaseDao.getConn();
PreparedStatement pstmt = null;
int row = 0;
try {
Class c=object.getClass();
Field[] fields = c.getDeclaredFields();
Field.setAccessible(fields, true);
StringBuffer sBuffer = new StringBuffer("delete from "+c.getSimpleName()+" where ");
sBuffer.append(fields[0].getName() +"=?");
pstmt = conn.prepareStatement(sBuffer.toString());
pstmt.setObject(1, fields[0].get(object));
row = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(null, pstmt, conn);
}
return row;
}
//id查询数据
public 实体类名 chaByid(int id){
实体类名 a = new 实体类名();
Connection conn = BaseDao.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from 表名 where id=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
while (rs.next()) {
a.setId(rs.getInt("id"));
a.setName(rs.getString("name"));
a.setPassword(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
}
如有疑问疑问请在评论区提出,我会尽快答复,如有更好的请跟我说一下。