package cn.zcw.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 DBUtil
{
private static String jdbcDriver = ""; // 定义连接信息
private static String jdbcUrl = "";
private static String jdbcUser = "";
private static String jdbcPasswd = "";
public static void main(String[] args)
{
}
static
{
InputStream is = null;
try
{
is = DBUtil.class.getClassLoader().getResourceAsStream("conf/database.properties"); // 加载database.properties文件
Properties p = new Properties();
p.load(is);
jdbcDriver = p.getProperty("driver"); // 赋值
jdbcUrl = p.getProperty("url");
jdbcUser = p.getProperty("user");
jdbcPasswd = p.getProperty("password");
//System.out.println(jdbcPasswd);
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (is != null)
{
try
{
is.close(); // 关闭is
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public static Connection getConn()
{ // 建立连接方法
Connection conn = null;
try
{
Class.forName(jdbcDriver);
conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPasswd);
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
public static void closeAll(ResultSet rs, Statement st, Connection conn)
{ // 关闭连接(用于增删改)
if (rs != null)
{
try
{
rs.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
if (st != null)
{
try
{
st.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
if (conn != null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
public static void closeAll(ResultSet rs, PreparedStatement ps, Connection conn)
{ // 关闭连接(用于查)
if (rs != null)
{
try
{
rs.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
if (ps != null)
{
try
{
ps.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
if (conn != null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
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 DBUtil
{
private static String jdbcDriver = ""; // 定义连接信息
private static String jdbcUrl = "";
private static String jdbcUser = "";
private static String jdbcPasswd = "";
public static void main(String[] args)
{
}
static
{
InputStream is = null;
try
{
is = DBUtil.class.getClassLoader().getResourceAsStream("conf/database.properties"); // 加载database.properties文件
Properties p = new Properties();
p.load(is);
jdbcDriver = p.getProperty("driver"); // 赋值
jdbcUrl = p.getProperty("url");
jdbcUser = p.getProperty("user");
jdbcPasswd = p.getProperty("password");
//System.out.println(jdbcPasswd);
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (is != null)
{
try
{
is.close(); // 关闭is
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public static Connection getConn()
{ // 建立连接方法
Connection conn = null;
try
{
Class.forName(jdbcDriver);
conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPasswd);
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
public static void closeAll(ResultSet rs, Statement st, Connection conn)
{ // 关闭连接(用于增删改)
if (rs != null)
{
try
{
rs.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
if (st != null)
{
try
{
st.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
if (conn != null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
public static void closeAll(ResultSet rs, PreparedStatement ps, Connection conn)
{ // 关闭连接(用于查)
if (rs != null)
{
try
{
rs.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
if (ps != null)
{
try
{
ps.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
if (conn != null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}