什么是JDBC 他的本质是什么?
jdbc是java连接数据库的一种连接器他是一套接口规范让不同厂商数据库来实现他数据库厂商为我们提供jar包
JDBC的原生操作步骤:
1:注册驱动
Classforname("com.xxx");
2:创建数据库的连接对象
Connection con=DriverManager.getConnection(数据库 ip地址 端口号 ,root,数据库密码);
3:准备sql语句:
String sql="xxx";
4:创建执行对象:
Statement st= con.createstatement();
5:执行sql语句
st.executupdate(sql);
6:释放资源
close();
ResultSet:获取查询结果的遍历
String sql ="xxx";
ResultSet set=st.executQuery();
while(set.next){
set.getint("xx");
set.getString("xx")
}
JDBC 工具类的封装:
1:定义一个封装类:user
2:构造方法私有化
3:成员位置声明静态私有属性字符串值为null
4:静态代码块 {
(1)读取properties下的文件;
(2)通过键值获得值properties.getpropert() 吧值赋给成员属性
(3)注册驱动 Class.getnaem();
}
5:提供静态功能 返回值为:Connection 获取数据库连接对象
6:封装关闭功能:(ReultSet, Statement,Connection)
数据库连接池:
1 :什么是连接池 ,作用是什么?
连接池是一个容器,能够创建固定可重用的连接对象,连接对象用完毕,节约内存消耗归还到连接池中等待下次利用
作用:启动连接池,用户发送请求,直接通过连接池操作里面已经创建好的对象,获取线程中的某个Connection,然后直接去操作数据库,使用完毕后从当前线程中解绑
封装
public <T>T select1(String sql,Object...obj){
connection= Jdbc.con();
try {
preparedStatement= connection.prepareStatement(sql);
for (int i=0;i<obj.length;i++){
preparedStatement.setObject(i+1,obj[i]);
}
resultset = preparedStatement.executeQuery();
if (resultset.next()){
return (T) resultset.getObject(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
// 获取一行数据
public <T>T selectOne(String sql,Class clazz,Object... obj){
connection = Jdbc.con();
try {
preparedStatement= connection.prepareStatement(sql);
for (int i=0 ;i<obj.length;i++){
preparedStatement.setObject(i+1,obj[i]);
}
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
if (resultSet.next()){
Object o = clazz.newInstance();
for (int i=1;i<=columnCount;i++){
String columnLabel = metaData.getColumnLabel(i);
Class type = clazz.getDeclaredField(columnLabel).getType();
Method declaredMethod = clazz.getDeclaredMethod("set" + columnLabel.substring(0, 1).toUpperCase() + columnLabel.substring(1), type);
declaredMethod.invoke(o,resultSet.getObject(i));
}
return (T)o;
}
} catch (SQLException e) {
System.out.println("获取预编译对象失败");
} catch (IllegalAccessException e) {
System.out.println("实例对象失败");
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
System.out.println("反射获取方法失败");
} catch (InvocationTargetException e) {
System.out.println("最后一部错误");
}
return null;
}
//获取所有数据
public <T>List<T> list (String sql,Class clazz, Object...obj) throws Exception {
List <T>list =new ArrayList<>();
connection = Jdbc.con();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
preparedStatement.setObject(i + 1, obj[i]);
}
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
while (resultSet.next()) {
Object o = clazz.newInstance();
for (int i = 1; i <= columnCount; i++) {
String columnLabel = metaData.getColumnLabel(i);
Class type = clazz.getDeclaredField(columnLabel).getType();
Method declaredMethod = clazz.getDeclaredMethod("set" + columnLabel.substring(0, 1).toUpperCase() + columnLabel.substring(1), type);
declaredMethod.invoke(o, resultSet.getObject(i));
}
list.add((T)o);
}
return list;
}
public int modefy(String sql,Object...obj){
connection= Jdbc.con();
try {
preparedStatement= connection.prepareStatement(sql);
for (int i=0;i<obj.length;i++){
preparedStatement.setObject(i+1,obj[i]);
}
return preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
}