0
点赞
收藏
分享

微信扫一扫

JDBC封装数据库的工具类【多测师】

自由情感小屋 2022-10-26 阅读 101

一、先写一个数据库的工具函数

要求:

1、可以通过IP地址账号密码等进行连接数据库

2、封装一

封装一个查询的方法 ==》多测师_王sir

package com.xiaoshu;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Properties;



public class DBUtils {



//硬编码,写死的代码--》解耦-》配置文件

/**

* 连接字符串

*/

private static String url;

/**

* 用户名

*/

private static String user;

/**

* 密码

*/

private static String password;



/**

* 上面的这些信息会变吗??

* 数据库名

* 用户名密码变了

* 数据库服务器ip换了

* 测试环境切换到生产服务器

* 数据库:mysql--》oracle

* 测试环境

*/



//注册驱动:只需要一次,当你用DBUtils类的时候,就注册好 把注册驱动写进静态代码块当中

//静态代码块:只在类加载到jvm中执行一遍

static {

try {

Properties properties = new Properties();

properties.load(Tester.class.getResourceAsStream("/jdbc.properties"));

url = properties.getProperty("jdbc.url");

user = properties.getProperty("jdbc.user");

password = properties.getProperty("jdbc.password");



String driver = properties.getProperty("jdbc.driver");

Class.forName(driver);

} catch (Exception e) {

e.printStackTrace();

}

}



//获得连接的方法

private static Connection getConnection() {

try {

return DriverManager.getConnection(url, user, password);

} catch (SQLException e) {

e.printStackTrace();

}

return null;

}



/**

* 增删改

* @param sql

* @param parameters

* @throws ClassNotFoundException

* @throws SQLException

*/

public static void excuteSQL(String sql, String... parameters) {

Connection conn = null; //先进行初始化 为了后面的资源可以在finally当中进行关闭

PreparedStatement pstmt = null;

try {

conn = getConnection();

pstmt = conn.prepareStatement(sql);

for (int i = 0; i < parameters.length; i++) {

pstmt.setString(i + 1, parameters);

}

pstmt.execute();

} catch (SQLException e) {

e.printStackTrace();

} finally {

close(conn, pstmt);

}

}







/**

* 查询

* @param sql

* @param parameters

* @return

* @throws ClassNotFoundException

* @throws SQLException

*/

public static List<HashMap<String, String>> select(String sql, String... parameters) {

//是可能有多条记录记录--》保存到一个什么数据容器

List<HashMap<String, String>> allRecordList = null;

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet resultSet = null;

try {

//获得连接

conn = getConnection();

pstmt = conn.prepareStatement(sql);

//参数设值

for (int i = 0; i < parameters.length; i++) {

pstmt.setString(i + 1, parameters);

}

//执行查询获取结果集

resultSet = pstmt.executeQuery();

//获取结果集的元数据: 元数据(描述数据的数据)

ResultSetMetaData metaData = resultSet.getMetaData();

//获得每个字段,需要知道字段数,获得列数

int columnCount = metaData.getColumnCount();//5

allRecordList = new ArrayList<HashMap<String, String>>();

//遍历结果集

while (resultSet.next()) {

//一条记录是包装成一个map

HashMap<String, String> recordMap = new HashMap<String, String>();

//遍历所有的列

for (int i = 1; i <= columnCount; i++) {

String columnName = metaData.getColumnName(i);

String value = resultSet.getString(i);

// System.out.print(columnName+":"+value +" ");

//把字段名作为key,对应数据作为值: key -value:

recordMap.put(columnName, value);

}

//添加到List容器中

allRecordList.add(recordMap);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

close(conn, pstmt, resultSet);

}



return allRecordList;

}



/**

* 关闭资源

* @param conn

* @param pstmt

* @param resultSet

*/

private static void close(Connection conn, PreparedStatement pstmt, ResultSet resultSet) {

//关闭资源

if (resultSet != null) {

try {

resultSet.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

close(conn, pstmt);

}



/**

* 关闭资源

* @param conn

* @param pstmt

*/

private static void close(Connection conn, PreparedStatement pstmt) {

if (pstmt != null) {

try {

pstmt.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}


二、调用工具类当中的select查询方法

package com.xiaoshu;

import java.io.IOException;

import java.sql.SQLException;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Properties;



public class Tester {

public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {

String sql = "select * from member;";

List<HashMap<String, String>> recordList = DBUtils.select(sql); //查询的结果以键值对的形式返回保存在列表容器当中

for (HashMap<String, String> record : recordList) { //对recordList对象进行循环遍历 取到单个的键和值

System.out.println(record);



}

 



举报

相关推荐

封装JDBC工具类

0 条评论