首页 > 数据库技术 > 详细

javaweb操作数据库工具类

时间:2021-06-15 16:50:07      阅读:20      评论:0      收藏:0      [点我收藏+]
package com.wang.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
private static String driver;
private static String url;
private static String username;
private static String password;

static {
Properties properties = new Properties();
InputStream io = BaseDao.class.getClassLoader().getResourceAsStream("jdbc.properties");

try {
properties.load(io);
} catch (IOException e) {
e.printStackTrace();
}
driver=properties.getProperty("jdbc.driver");
url=properties.getProperty("jdbc.url");
username=properties.getProperty("jdbc.user");
password=properties.getProperty("jdbc.password");

}

public static Connection getConnection(){
Connection connection=null;
try {
Class.forName(driver);
connection= DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}


return connection;
}

public static ResultSet execute(Connection connection,PreparedStatement pstm,ResultSet resultSet,String sql,Object[] params) throws SQLException {
pstm = connection.prepareStatement(sql);
for (int i = 0; i <params.length ; i++) {
pstm.setObject(i+1,params[i]);

}
resultSet = pstm.executeQuery();
return resultSet;


}


public static int execute(Connection connection,PreparedStatement pstm,String sql,Object[] params) throws SQLException {
pstm = connection.prepareStatement(sql);
for (int i = 0; i <params.length ; i++) {
pstm.setObject(i+1,params[i]);

}
int update = pstm.executeUpdate();
return update;


}

public static boolean closeResource(Connection connection,PreparedStatement pstm,ResultSet resultSet)
{
boolean flag=true;


if (connection!=null)
{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
flag=false;
}

}
if (pstm!=null)
{
try {
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
flag=false;
}

}
if (resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
flag=false;
}

}
return flag;
}






}

javaweb操作数据库工具类

原文:https://www.cnblogs.com/upupup-999/p/14884975.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!