package com.hsp.utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.sql.*;
public class SqlHelper
{
//定义变量
private
static Connection ct = null;
//大多数情况下用preparedstatement替代statement
private
static PreparedStatement ps = null;
private
static ResultSet rs = null;
//连接数据库的参数
private
static String url = "";
private
static String username = "";
private
static String driver = "";
private
static String passwd = "";
private
static CallableStatement cs = null;
public
static CallableStatement getCs()
{
return cs;
}
private
static Properties pp = null;
private
static InputStream fis = null;
//加载驱动,只需要一次,用静态代码块
static
{
try
{
//从dbinfo.properties
pp = new Properties();
fis=SqlHelper.class.getClassLoader().getResourceAsStream("mysql.properties");
//fis = new FileInputStream();
pp.load(fis);
url = pp.getProperty("url");
username = pp.getProperty("username");
driver = pp.getProperty("driver");
passwd = pp.getProperty("passwd");
Class.forName(driver);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{ fis.close();}
catch(IOException e) {e.printStackTrace();}
|