templet模式
Posted 不停的奋斗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了templet模式相关的知识,希望对你有一定的参考价值。
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模式的主要内容,如果未能解决你的问题,请参考以下文章
java JAVA Templete for Eclipse Snippets。