package com.cxz.util;
import java.sql.*;
public class DBTemplate {
private static String driverClass = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/ceramic";
private static String userName = "root";
private static String password = "19841230";
static {
try {// register the jdbc driver only once
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// should reading
}
public DBTemplate() {
}
private Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, userName, password);
}
private PreparedStatement getPreparedStatement(Connection conn, String sql)
throws SQLException {
return conn.prepareStatement(sql);
}
private void setParams(PreparedStatement pstmt, Object[] params) throws SQLException{
for (int i = 0; i < params.length; i++) {
if (Integer.class == params[i].getClass()) {
pstmt.setInt(i+1, (Integer)params[i]);
} else if (String.class == params[i].getClass()) {
pstmt.setString(i+1, (String)params[i]);
} else if (Date.class == params[i].getClass()) {
pstmt.setDate(i+1, (Date)params[i]);
} else if (Float.class == params[i].getClass()) {
pstmt.setFloat(i+1, (Float)params[i]);
} else if (Double.class == params[i].getClass()){
pstmt.setDouble(i+1, (Double)params[i]);
} else {
throw new IllegalArgumentException(
"The method query can only treat types: String, Date, int, float, double");
}
}
}
public boolean execute(String sql, Object[] params) {
Connection conn = null;
PreparedStatement pstmt = null;
boolean succ = false;
if (sql == null) {
// to check whether the params if null;
throw new NullPointerException(
"The parameters of DBTemplate::query(...) should not be null!");
}
try {
conn = this.getConnection();
pstmt = this.getPreparedStatement(conn, sql);
if (params != null) {// if it has ?
this.setParams(pstmt, params);
}
succ = pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return succ;
}
public void query(String sql, Object[] params, QueryManager qm) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
if (sql == null || qm == null) {
// to check whether the params if null;
throw new NullPointerException(
"The parameters of DBTemplate::query(...) should not be null!");
}
try {
conn = this.getConnection();
pstmt = this.getPreparedStatement(conn, sql);
if (params != null) {// if it has ?
this.setParams(pstmt, params);
}
rs = pstmt.executeQuery();
try {
qm.doQuery(rs);
} catch (SQLException ex) {
ex.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
分享到:
相关推荐
红领巾伴我成长二(3)班主题班会.doc
共育花朵健康成长二(7)班主任总结.pdf
消防安全伴我成长二年级作文范例400字6篇.pdf
20210210-中信证券-玻纤行业系列分析报告之二:电子纱电子布,龙头粗纱企业的第二成长曲线.pdf
20210617-国泰君安-科技成长的起点系列二:格雷厄姆的启示,成长性从未如此重要.pdf
20210722-国金证券-2021年基金二季报剖析:赛道为王,成长突进.pdf
20210210-申万宏源-三七互娱-002555-转型蓄力,开启第二成长曲线.pdf
鲁美小学二年级品德与生活下册我成长记录PPT学习教案.pptx
20210820-招商证券-江海股份-002484-深耕三大电容制造,新能源开启第二成长曲线.pdf
20210824-招商证券-卓胜微-300782-滤波器及PA模组迎量产曙光,站在第二成长曲线起点.pdf
新北师大二年级下册数学小蝌蚪的成长PPT课件.pptx
20210819-招商证券-江海股份-002484-深耕三大电容制造,新能源开启第二成长曲线.pdf
二年级成长记录.doc
20210805-招商证券-光峰科技-688007-硬科技铸就激光显示卖铲人,家用放量打开第二成长曲线.pdf
二. 市场目标概述 三. 项目优势及特点简介 四. 利润来源简析 五. 投资和预算 六. 融资方案(资金筹措及投资方式) 七. 财务分析(预算及投资报酬) 第二部分 综述 第一章 项目背景 一...
APICloud的学习成长之路(二),分享学习思路和感悟
二年级数学下册五加与减5.6小蝌蚪的成长教学反思素材北师大版20200506150
海康威视-002415-企业端需求与创新业务是公司的第二成长曲线
道德与法治《学习伴我成长》教学反思.pdf