private static BasicDataSource dataSource;
private static Connection con=null;
private static PreparedStatement ps =null;
private static ResultSet rs = null;
static {
dataSource = new BasicDataSource();
initparameter();
}
private static void initparameter() {
Properties prop = new Properties();
try {
prop.load(ContactJdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
}
dataSource.setDriverClassName(prop.getProperty("driver"));
dataSource.setUrl(prop.getProperty("url"));
dataSource.setUsername(prop.getProperty("username"));
dataSource.setPassword(prop.getProperty("password"));
dataSource.setMaxActive(Integer.parseInt(prop.getProperty("maxActive")));
dataSource.setMinIdle(Integer.parseInt(prop.getProperty("min")));
dataSource.setInitialSize(Integer.parseInt(prop.getProperty("size")));
dataSource.setMaxWait(Integer.parseInt(prop.getProperty("maxwait")));
}
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
try {
con = dataSource.getConnection();
ps = con.prepareStatement(sql);
if(obj!=null){
for(int i=0;i<obj.length;i++){
ps.setObject(i+1, obj[i]);
}
}
return ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
try {
con = dataSource.getConnection();
ps = con.prepareStatement(sql);
if(obj!=null){
for(int i=0;i<obj.length;i++){
ps.setObject(i+1, obj[i]);
}
}
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
try {
if(con!=null){
con.close();
con=null;
}
if(ps!=null){
ps.close();
ps=null;
}
if(rs!=null){
rs.close();
rs=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
Date date = null;
try {
date=new Date(new SimpleDateFormat("yyyy-MM-dd").parse(haridate).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
private static Connection con=null;
private static PreparedStatement ps =null;
private static ResultSet rs = null;
static {
dataSource = new BasicDataSource();
initparameter();
}
private static void initparameter() {
Properties prop = new Properties();
try {
prop.load(ContactJdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
}
dataSource.setDriverClassName(prop.getProperty("driver"));
dataSource.setUrl(prop.getProperty("url"));
dataSource.setUsername(prop.getProperty("username"));
dataSource.setPassword(prop.getProperty("password"));
dataSource.setMaxActive(Integer.parseInt(prop.getProperty("maxActive")));
dataSource.setMinIdle(Integer.parseInt(prop.getProperty("min")));
dataSource.setInitialSize(Integer.parseInt(prop.getProperty("size")));
dataSource.setMaxWait(Integer.parseInt(prop.getProperty("maxwait")));
}
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
//调用该方法前写一SQL语句 将?参数的值通过Object数组进行赋值 没有?参数时写为(sql,null)
try {
con = dataSource.getConnection();
ps = con.prepareStatement(sql);
if(obj!=null){
for(int i=0;i<obj.length;i++){
ps.setObject(i+1, obj[i]);
}
}
return ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//增 删 改 的方法
try {
con = dataSource.getConnection();
ps = con.prepareStatement(sql);
if(obj!=null){
for(int i=0;i<obj.length;i++){
ps.setObject(i+1, obj[i]);
}
}
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
//在调用以上的方法后调用该方法来关闭
try {
if(con!=null){
con.close();
con=null;
}
if(ps!=null){
ps.close();
ps=null;
}
if(rs!=null){
rs.close();
rs=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//日期的一个转换
Date date = null;
try {
date=new Date(new SimpleDateFormat("yyyy-MM-dd").parse(haridate).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}