下午的时候说要用抽象工厂模式封装DB,但是由于晚上电脑出了点问题,所以当时没有贴出代码,现在电脑已经恢复正常,我就把抽象工厂模式封装DB的代码贴出来,供大家参考和点评。
DB.java
package com.gogofun.util; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 抽象工厂模式的抽象类 * @author GogoFun * */ public abstract class DB { public static PreparedStatement pstmt = null; //外部获取驱动字符串 public abstract String getDriverString(); //该方法连接本地服务器,默认端口 public abstract Connection getConnection(String dbName, String dbUser, String dbPassword); //该方法连接指定IP服务器,默认端口 public abstract Connection getConnection(String dbIp, String dbName, String dbUser, String dbPassword); //该方法连接本地服务器,指定端口 /*public abstract Connection getConnection(String dbPort,String dbName, String dbUser, String dbPassword);*/ //该方法连接指定IP服务器,指定端口 public abstract Connection getConnection(String dbIp,String dbPort,String dbName, String dbUser, String dbPassword); //执行一条SQL Query语句 public static ResultSet executeQuery(Connection conn,String sqlQ) { if((conn == null)|| (sqlQ == null)) { return null; } try { pstmt = conn.prepareStatement(sqlQ); return pstmt.executeQuery(); } catch (SQLException e) { e.printStackTrace(); close(pstmt); return null; } } //执行一条ParepredStatement For Query public static ResultSet executeQuery(Connection conn,PreparedStatement pstmt) { if((conn == null)|| (pstmt == null)) { return null; } try { return pstmt.executeQuery(); } catch (SQLException e) { e.printStackTrace(); return null; } } //执行一条SQL Update语句 public static boolean executeUpdate(Connection conn,String sqlU) { if((conn == null)|| (sqlU == null)) { return false; } try { pstmt = conn.prepareStatement(sqlU); pstmt.executeUpdate(); return true; } catch (SQLException e) { e.printStackTrace(); close(pstmt); return false; } } //执行一条ParepredStatement For Update public static boolean executeUpdate(Connection conn,PreparedStatement pstmt) { if((conn == null)|| (pstmt == null)) { return false; } try { pstmt.executeUpdate(); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } //关闭连接 public static boolean close (Connection conn) { try { if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } //关闭语句 public static boolean close (Statement stmt) { try { if(stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } //关闭结果集 public static boolean close (ResultSet rs) { try { if(rs != null) { rs.close(); rs = null; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } }
MysqlDB.java
package com.gogofun.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MysqlDB extends DB { private static String driverString = "com.mysql.jdbc.Driver"; private static String connString = "jdbc:mysql://"; private static boolean isInit = false; private static void init() { try { Class.forName(driverString); isInit = true; } catch (ClassNotFoundException e) { e.printStackTrace(); isInit = false; } } @Override public String getDriverString() { return driverString; } @Override public Connection getConnection(String dbName, String dbUser, String dbPassword) { if (!isInit) { init(); } String connStr = connString + "localhost:3306/" + dbName + "?user=" + dbUser + "&password=" + dbPassword; try { return DriverManager.getConnection(connStr); } catch (SQLException e) { e.printStackTrace(); return null; } } @Override public Connection getConnection(String dbIp, String dbName, String dbUser, String dbPassword) { if (!isInit) { init(); } String connStr = connString + dbIp + ":3306/" + dbName + "?user=" + dbUser + "&password=" + dbPassword; try { return DriverManager.getConnection(connStr); } catch (SQLException e) { e.printStackTrace(); return null; } } @Override public Connection getConnection(String dbIp, String dbPort, String dbName, String dbUser, String dbPassword) { if (!isInit) { init(); } String connStr = connString + dbIp + ":" + dbPort + "/" + dbName + "?user=" + dbUser + "&password=" + dbPassword; try { return DriverManager.getConnection(connStr); } catch (SQLException e) { e.printStackTrace(); return null; } } }
OracleDB.java
package com.gogofun.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class OracleDB extends DB { private static String driverString = "oracle.jdbc.driver.OracleDriver"; private static String connString = "jdbc:oracle:this:@"; private static boolean isInit = false; private static void init() { try { Class.forName(driverString); isInit = true; } catch (ClassNotFoundException e) { e.printStackTrace(); isInit = false; } } @Override public String getDriverString() { return driverString; } @Override public Connection getConnection(String dbName, String dbUser, String dbPassword) { if (!isInit) { init(); } try { String connStr = connString + "localhost:1521:" + dbName; return DriverManager.getConnection(connStr,dbUser,dbPassword); } catch (SQLException e) { e.printStackTrace(); return null; } } @Override public Connection getConnection(String dbIp, String dbName, String dbUser, String dbPassword) { if (!isInit) { init(); } try { String connStr = connString + dbIp + ":1521:" + dbName; return DriverManager.getConnection(connStr,dbUser,dbPassword); } catch (SQLException e) { e.printStackTrace(); return null; } } @Override public Connection getConnection(String dbIp, String dbPort, String dbName, String dbUser, String dbPassword) { if (!isInit) { init(); } try { String connStr = connString + dbIp + ":" + dbPort +":" + dbName; return DriverManager.getConnection(connStr,dbUser,dbPassword); } catch (SQLException e) { e.printStackTrace(); return null; } } }