package com.lgg.utils;
import java.io.InputStream;
import java.sql.*;
import java.util.*;
public class SqlHelper {
//定义需要的变量
private Connection ct=null;
//大多数情况下,我们使用的是PreparedStatement来替代Statement可以防止sql注入
private PreparedStatement ps=null;
private ResultSet rs=null;
private CallableStatement cs=null;
//连接数据库的参数
private static String url="";
private static String username="";
private static String driver="";
private static String password="";
//读配置文件
private static Properties pp=null;
private static InputStream fis=null;
//加载驱动,只需要一次
static{
try{
//从dbinfo.properties文件中读取配置信息
pp=new Properties();
//当我们使用java web的时候,读取文件要使用类加载器[因为类加载器去读取资源的时候,默认的主目录是src目录
fis=SqlHelper.class.getClassLoader().getResourceAsStream("/com/lgg/utils/dbinfo.properties");
pp.load(fis);
url=pp.getProperty("url");
username=pp.getProperty("username");
driver=pp.getProperty("driver");
password=pp.getProperty("password");
Class.forName(driver);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
fis.close();
}catch(Exception e){
e.printStackTrace();
}
fis=null; //垃圾回收站上收拾
}
}
/*传入表名返回该表的记录数*/
public int getRowCount(String sql){
int rowCount = 0;
ct = getConnection();
try {
ps = ct.prepareStatement(sql);
ResultSet rs= ps.executeQuery();
if (rs.next()){
rowCount = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
close(rs,ps,ct);
}
return rowCount;
}
public Connection getConnection(){
try{
ct=DriverManager.getConnection(url,username,password);
}catch(Exception e){
e.printStackTrace();
}
return ct;
}
/* 传入sql语句 返回查询结果封装成对象(每一行表示一个对象)集合*/
public ArrayList executeQuery(String sql,String[] paras){
ct = getConnection();
ArrayList al = new ArrayList();
try {
ps = ct.prepareStatement(sql);
if (paras != null){
for(int i=0;i<paras.length;i++){
ps.setString(i+1, paras[i]);
}
rs=ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int column = rsmd.getColumnCount();
while(rs.next()){
Object[] ob=new Object[column];
for(int i=1;i<=column;i++){
ob[i-1] = rs.getObject(i);
}
al.add(ob);
}
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
close(rs,ps,ct);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException("executesqlResultSet方法出错:"+e.getMessage());
}
}
return al;
}
public void executeUpdate(String sql,String[] parameters){
//1.创建一个ps
ct=getConnection();
try{
ps=ct.prepareStatement(sql);
//给?赋值
if(parameters!=null){
for(int i=0;i<parameters.length;i++){
ps.setString(i+1, parameters[i]);
}
}
//执行
ps.execute();
}catch(Exception e){
e.printStackTrace();//开发阶段
//抛出异常,抛出运行异常,可以给调用该函数的函数一个选择
//可以处理,也可以放弃处理
throw new RuntimeException(e.getMessage());
}finally{
//关闭资源
close(rs,ps,ct);
}
}
//关闭资源的函数
public void close(ResultSet rs,Statement ps,Connection ct){
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
rs=null;
}
if(ps!=null){
try{
ps.close();
}catch(Exception e){
e.printStackTrace();
}
ps=null;
}
if(ct!=null){
try{
ct.close();
}catch(Exception e){
e.printStackTrace();
}
ct=null;
}
}
public Connection getct(){
return ct;
}
public PreparedStatement getPs(){
return ps;
}
public ResultSet getRs(){
return rs;
}
public CallableStatement getCs(){
return cs;
}
public int getRowCount(String sql,String[] parameters){
int rowCount = 0;
ct = getConnection();
try {
ps = ct.prepareStatement(sql);
if(parameters!=null){
for(int i=0;i<parameters.length;i++){
ps.setString(i+1, parameters[i]);
}
}
ResultSet rs= ps.executeQuery();
if (rs.next()){
rowCount = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
close(rs,ps,ct);
}
return rowCount;
}
}