网页右边,向下滑有目录索引,可以根据标题跳转到你想看的内容 |
---|
如果右边没有就找找左边 |
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9f9f8e41fa220a16cd5fe08767062a7b.png)
运行效果 |
---|
只需要传入一个实体类,即可完成对应功能,也就是随便找一个表,对应一个实体类,作为参数传入即可完成功能 |
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8cef5f0eae2d00748436d79d467537be.png)
一、环境搭建
创建一个maven项目并引入JDBC驱动依赖(不会maven就手动下载jar包,放在项目中然后导入环境,效果一样 ) |
---|
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/967dda33684bf07e24645b645075a7fd.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/70dbf47ee7f17a2b0e701843735ad92f.png)
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>oracle-driver-ojdbc</artifactId>
<version>12.1.0.2</version>
</dependency>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6011ef63acd1780943881800be6b2459.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a3565d71265c851d6e9048ee8a86c528.png)
package com.yzpnb.entity;
public class User {
private Integer id;
private String username;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
二、源码与效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fde37bcc34e7e212883a7fe7754b9d44.png)
package com.yzpnb.mapper;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JDBCMapper {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
public JDBCMapper() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "yzp", "root");
} catch (SQLException e) {
e.printStackTrace();
}
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
private List<Object> resultSetList(ResultSet resultSet) throws Exception {
ArrayList<Object> list = new ArrayList<Object>();
ResultSetMetaData md = resultSet.getMetaData();
int columnCount = md.getColumnCount();
while (resultSet.next()) {
Map<String,Object> rowData = new HashMap<String,Object>();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), resultSet.getObject(i));
}
list.add(rowData);
}
return list;
}
public List<Object> selectAll(Object obj){
String sql = "select * from \""+obj.getClass().getSimpleName().toLowerCase()+"\"";
List<Object> list = null;
try {
resultSet = statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
try {
list = this.resultSetList(resultSet);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sql);
return list;
}
public Integer insert(Object obj) {
Field[] fields = obj.getClass().getDeclaredFields();
String sql = "insert into \""+obj.getClass().getSimpleName().toLowerCase()+"\"(";
for (Field field:fields) {
sql += "\""+field.getName()+"\",";
}
sql += ") values(";
for (Field field:fields) {
try {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), obj.getClass());
Method readMethod = pd.getReadMethod();
Object invoke = readMethod.invoke(obj);
if(invoke != null){
if(pd.getPropertyType().getName().equals("java.lang.String")){
sql+="\'"+invoke+"\'"+",";
}else{
sql +=invoke+",";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
sql+=")";
sql = sql.replace(",)", ")");
System.out.println(sql);
try {
ResultSet resultSet = statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return 1;
}
public Integer updateById(Object obj){
Object id = null;
Field[] fields = obj.getClass().getDeclaredFields();
String sql = "update "+"\""+obj.getClass().getSimpleName().toLowerCase()+"\" set ";
for(Field field:fields){
try {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), obj.getClass());
if(field.getName().equals("id")){
id = propertyDescriptor.getReadMethod().invoke(obj);
continue;
}
Method readMethod = propertyDescriptor.getReadMethod();
Object invoke = readMethod.invoke(obj);
if(invoke != null){
sql+="\""+field.getName()+"\"=";
if(propertyDescriptor.getPropertyType().getName().equals("java.lang.String")){
sql+="\'"+invoke+"\',";
}else{
sql+=invoke+",";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
sql+="where \"id\" = "+id;
sql = sql.replace(",where"," where");
System.out.println(sql);
try {
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return 1;
}
public Integer deleteById(Object obj){
String sql = "delete from \""+obj.getClass().getSimpleName().toLowerCase()+"\" where \"id\" = ";
try {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor("id", obj.getClass());
sql+=propertyDescriptor.getReadMethod().invoke(obj);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sql);
try {
statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return 1;
}
public void closeJDBC(){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6dce879722eae91d6967ab7b31c9fe46.png)
package com.yzpnb.controler;
import com.yzpnb.entity.User;
import com.yzpnb.mapper.JDBCMapper;
import java.util.List;
public class TestJDBC {
public static void main(String[] args) {
JDBCMapper jdbcMapper = new JDBCMapper();
List<Object> objects = jdbcMapper.selectAll(new User());
System.out.println("查询结果为:"+objects.toString());
User user = new User();
user.setId(10);
user.setUsername("Test");
user.setAge(15);
jdbcMapper.insert(user);
User user1 = new User();
user.setId(2);
user.setUsername("张三");
jdbcMapper.updateById(user1);
User user2 = new User();
user2.setId(1);
jdbcMapper.deleteById(user2);
jdbcMapper.closeJDBC();
}
}