Java JDBC是Java中进行关系型数据库操作的标准方式,它提供了丰富的API让我们灵活处理数据库的连接、操作和结果集。但是,使用Java JDBC进行开发时没有封装的话会显得冗长、繁琐,因此自定义封装工具类可以提高工作效率并提高代码可读性和可维护性。
下面是Java JDBC自定义封装工具类的步骤和完整代码攻略:
1.建立数据库连接
public class DBUtil {
private static Connection conn;
private static final String URL = "jdbc:mysql://localhost:3306/demo_db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() {
try {
//加载驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
//获得数据库连接
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
2.释放数据库资源
public static void release(ResultSet rs, Statement stmt, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3.封装查询操作
public static List<Map<String, Object>> executeQuery(String sql, Object[] params) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Map<String, Object>> dataList = new ArrayList<>();
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
rs = pstmt.executeQuery();
ResultSetMetaData metaData = rs.getMetaData();
int colCount = metaData.getColumnCount();
while (rs.next()) {
Map<String, Object> data = new HashMap<>();
for (int i = 1; i <= colCount; i++) {
String colName = metaData.getColumnName(i);
Object colValue = rs.getObject(i);
data.put(colName, colValue);
}
dataList.add(data);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
release(rs, pstmt, conn);
}
return dataList;
}
4.封装更新操作
public static int executeUpdate(String sql, Object[] params) {
Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
release(null, pstmt, conn);
}
return result;
}
以上就是封装Java JDBC的核心代码部分,下面是两个简单的示例说明:
示例一:查询表中所有数据
String sql = "select * from user";
List<Map<String, Object>> result = DBUtil.executeQuery(sql, null);
for (Map<String, Object> map : result) {
for (String key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
}
示例二:更新表中数据
String sql = "update user set age = ? where id = ?";
Object[] params = {25, 1};
int result = DBUtil.executeUpdate(sql, params);
if (result > 0) {
System.out.println("操作成功!");
} else {
System.out.println("操作失败!");
}
以上是Java JDBC自定义封装工具类的完整攻略,通过这种方式可以简化开发人员的工作,提高开发效率并降低出错率。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java JDBC自定义封装工具类的步骤和完整代码 - Python技术站