码迷,mamicode.com
首页 > 其他好文 > 详细

templet模式

时间:2017-10-14 23:37:10      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:try   ted   close   on()   rri   lex   int   finally   catch   

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模式

标签:try   ted   close   on()   rri   lex   int   finally   catch   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!