首页 > 其他 > 详细

templet模式

时间:2017-10-14 22:58:33      阅读:265      评论:0      收藏:0      [点我收藏+]
package template;

import java.sql.Connection;
import java.sql.ResultSet;

/**
* Created by marcopan on 17/9/26.
*/
public abstract class QueryRunner {
public abstract Connection createConnection();

public abstract String createSQL();

public abstract ResultSet runSQL(Connection conn, String sql);

public ResultSet run() throws Exception {
Connection conn = null;
String sql = "";
try {
conn = this.createConnection();
sql = this.createSQL();
return runSQL(conn, sql);
} finally {
if (conn != null) {
conn.close();
}
}
}
}

package template;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* Created by marcopan on 17/9/26.
*/
public class OracleQueryRunner extends QueryRunner {

@Override
public Connection createConnection() {
System.out.println("createConnection from OracleQueryRunner");
return null;
}

@Override
public String createSQL() {
return "createSQL from OracleQueryRunner";
}

@Override
public ResultSet runSQL(Connection conn, String sql) {
System.out.println("runSQL from OracleQueryRunner");
Statement st = null;
try {
st = conn.createStatement();
return st.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return null;
}
}

templet模式

原文:http://www.cnblogs.com/panning/p/7668903.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!