JDBC 工具类
描述:使用JDBC封装了底层的增删改查用SQL来操作,支持事务操作,支持预处理SQL,支持properties配置数据库
配置文件存放目录和DatabaseUtil类存放目录相同
package util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DatabaseUtil {
private String jdbc = null;
private String url = null;
private String username = null;
private String password = null;
private Connection conn = null;
private Statement st = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private InputStream is = null;
private Properties property = null;
//手动写入数据库配置
public DatabaseUtil()
{
jdbc = "";
url = "";
username = "";
password = "";
try
{
Class.forName(jdbc);
} catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
//通过properties文件写入数据库配置
public DatabaseUtil(String properties_name)
{
try
{
is = DatabaseUtil.class.getResourceAsStream(properties_name);
property = new Properties();
property.load(is);
} catch (IOException ioe) {
ioe.printStackTrace();
}finally
{
try
{
is.close();
is = null;
} catch (IOException e) {
e.printStackTrace();
}
}
jdbc = property.getProperty("jdbc");
url = property.getProperty("url");
username = property.getProperty("username");
password = property.getProperty("password");
try
{
Class.forName(jdbc);
} catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
//实例化类时写入数据库配置
public DatabaseUtil(String jdbc,String url,String username,String password)
{
this.jdbc = jdbc;
this.url = url;
this.username = username;
this.password = password;
try
{
Class.forName(jdbc);
} catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
//获得连接
public Connection getConnection()
{
try
{
conn = DriverManager.getConnection(url,username,password);
}catch (SQLException sqle)
{
this.closeConnection();
sqle.printStackTrace();
}
return conn;
}
//执行一条SQL语句
public void executeUpdate(String sql)
{
try {
st = conn.createStatement();
if(sql!=null && !sql.equals(""))
{
st.executeUpdate(sql);
}
} catch (SQLException sqle)
{
sqle.printStackTrace();
}finally
{
this.closeStatement();
}
}
//执行多条SQL语句,支持事务操作
public void executeUpdate(String[] sqls)
{
try {
conn.setAutoCommit(false);
st = conn.createStatement();
for(String sql:sqls)
{
if(sql != null && !sql.equals(""))
{
st.execute(sql);
}
}
conn.commit();
} catch (SQLException e) {
try
{
conn.rollback();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
e.printStackTrace();
}finally
{
this.closeStatement();
}
}
//以预处理的形式执行一条SQL语句
public void executeUpdatePrepared(String sql,String[] params)
{
try {
ps = conn.prepareStatement(sql);
if(ps != null)
{
for(int i=0;i<params.length;i++)
{
ps.setString(i+1, params[i]);
}
ps.executeUpdate();
}
} catch (SQLException sqle)
{
sqle.printStackTrace();
}finally
{
this.closePreparedStatement();
}
}
//以预处理的形式执行多条SQL语句,支持事务操作
public void executeUpdatePrepared(String[] sqls,String[][] params)
{
try
{
conn.setAutoCommit(false);
for(int i=0;i<sqls.length;i++)
{
ps = conn.prepareStatement(sqls[i]);
if(ps != null)
{
for(int j=0;j<params[i].length;j++)
{
ps.setString(j+1, params[i][j]);
}
ps.executeUpdate();
}
}
conn.commit();
} catch (SQLException sqle)
{
try
{
conn.rollback();
} catch (SQLException e)
{
e.printStackTrace();
}
sqle.printStackTrace();
}finally
{
this.closePreparedStatement();
}
}
//查询一条SQL
public ResultSet executeQuery(String sql)
{
try
{
st = conn.createStatement();
if(sql!=null && !sql.equals(""))
{
rs = st.executeQuery(sql);
}
}catch(SQLException sqle)
{
sqle.printStackTrace();
}finally
{
//rs遍历后需要手动关闭的
}
return rs;
}
//预处理查询一条SQL
public ResultSet executeQueryPrepared(String sql,String[] params)
{
try
{
ps = conn.prepareStatement(sql);
if(ps != null)
{
for(int i=0;i<params.length;i++)
{
ps.setString(i+1, params[i]);
}
rs = ps.executeQuery();
}
}catch(SQLException sqle)
{
sqle.printStackTrace();
}finally
{
//rs遍历后需要手动关闭的
}
return rs;
}
//关闭会话
public void closeStatement()
{
try
{
if(st != null)
{
st.close();
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
//关闭结果集
public void closeResultSet()
{
try
{
rs.close();
}catch(SQLException sqle)
{
sqle.printStackTrace();
}
}
//关闭预处理会话
public void closePreparedStatement()
{
try
{
if(ps != null)
{
ps.close();
}
}catch(SQLException sqle)
{
sqle.printStackTrace();
}
}
//关闭连接
public void closeConnection()
{
if(conn != null)
{
try
{
conn.close();
} catch (SQLException sqle)
{
sqle.printStackTrace();
}
}
}
}
mysql.properties配置文件:
#数据库配置
jdbc=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123456
JDBC 工具类
最新推荐文章于 2024-10-09 18:56:11 发布