1.驱动注册,资源关闭。
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class myjdbcutils {
public static Connection getConnection() {
//1.读取配置文件的信息
InputStream inputStream=ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties properties= new Properties();
properties.load(inputStream);
String user=properties.getProperty("user");
String password=properties.getProperty("password");
String url= properties.getProperty("url");
String diverClass= properties.getProperty("diverClass");
//2.加在驱动
Class.forName(diverClass);
//3.获取连接
Connection connection= DriverManager.getConnection(url, user, password);
return connection;
}
//资源关闭的操作
public static void CloseResource(Connection connection,PreparedStatement ps) {
try {
if (ps!=null) {
ps.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (connection!=null) {
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void CloseResource(Connection connection,PreparedStatement ps,ResultSet rs) {
try {
if (ps!=null) {
ps.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (connection!=null) {
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.数据增删改
package JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class AddDeleteUpdatUtils {
public static void AddDeleteUpdat(String sql,Object ...args) {
//sql中占位符应该和形参一致
Connection conn=null;
//预编译sql
PreparedStatement ps=null;
try {
conn = myjdbcutils.getConnection();
ps = conn.prepareStatement(sql);
//填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1, args[i]);
}
//执行
ps.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
myjdbcutils.CloseResource(conn, ps);
}
}
}
3.查询一条数据,全部数据
package JdbcUtils;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
public class SelectOneOrAllUtils {
//得到一条
public static <T> Object getInstance(Class<T>clazz ,String sql,Object...args) {
Connection connection=null;
PreparedStatement pStatement=null;
ResultSet resultSet=null;
try {
connection = myjdbcutils.getConnection();
pStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pStatement.setObject(i+1, args[i]);
}
resultSet = pStatement.executeQuery();
//获取结果集
ResultSetMetaData rsmd = resultSet.getMetaData();
int columnCount = rsmd.getColumnCount();
if (resultSet.next()) {
//封装到类中
T t = clazz.newInstance();
for (int i = 0; i < columnCount; i++) {
//获取列值
Object columnvalue = resultSet.getObject(i+1);
//获取列的列名
String columnName = rsmd.getColumnLabel(i+1);
//给test_jdbc赋值为value,通过反射
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t, columnvalue);
}
return t;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
myjdbcutils.CloseResource(connection, pStatement, resultSet);
}
return null;
}
//得到全部
//---------------查询多个对象-----------
public static <T> List<T> Selectalldata(Class<T>clazz , String sql, Object...args){
Connection connection=null;
PreparedStatement pStatement=null;
ResultSet resultSet=null;
try {
connection = myjdbcutils.getConnection();
pStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pStatement.setObject(i+1, args[i]);
}
resultSet = pStatement.executeQuery();
//获取结果集
ResultSetMetaData rsmd = resultSet.getMetaData();
int columnCount = rsmd.getColumnCount();
//创建集合接数据
ArrayList<T> list = new ArrayList<T>();
while (resultSet.next()) {
//封装到类中
T t = clazz.newInstance();
//处理结果集一行数据中的每一个列,给t对象指定属性赋值
for (int i = 0; i < columnCount; i++) {
//获取列值
Object columnvalue = resultSet.getObject(i+1);
//获取列的列名
String columnName = rsmd.getColumnLabel(i+1);
//给test_jdbc赋值为value,通过反射
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t, columnvalue);
}
list.add(t);
}
return list;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
myjdbcutils.CloseResource(connection, pStatement, resultSet);
}
return null;
}
}
版权声明:本文为weixin_42497022原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。