标签:root 备忘录模式 eee 实体 桥接 inux override on() asi
2020.01.08
博客期:121
星期三
今天对过去整个大二和大三用到的数据库的方法进行汇总,可以有效的使用、套用,每一个部分都有《软件设计模式》知识,上述代码满足了开闭原则,如果要使用此代码,只需添加数据类继承已经写好的类就可以了,无需修改。
开发项目中,项目整体结构:
【小编网不好,图传不上去,到时候,补上】
类图:
【小编网不好,图传不上去,到时候,补上】
源码:
com.dblink.basic.sqlkind 包:
1 package com.dblink.basic.sqlKind; 2 3 /*标准信息连接类*/ 4 public abstract class Sql { 5 //---[属性成员] 6 /*端口*/ 7 protected String port = ""; 8 /*端口*/ 9 protected String database = ""; 10 /*主机地址*/ 11 protected String host = ""; 12 //---[方法成员] 13 /*获取连接 URL*/ 14 public abstract String buildURL(); 15 /*获得相应数据库给定的驱动*/ 16 public abstract String getDriver(); 17 /*get、set方法*/ 18 public String getPort() { 19 return port; 20 } 21 public void setPort(String port) { 22 this.port = port; 23 } 24 public String getDatabase() { 25 return database; 26 } 27 public void setDatabase(String database) { 28 this.database = database; 29 } 30 /*构造方法*/ 31 public Sql(String database,String port) { 32 this.port = port; 33 this.database = database; 34 this.host = "localhost"; 35 } 36 public Sql(String database,String port,String host) { 37 this.port = port; 38 this.database = database; 39 this.host = host; 40 } 41 }
1 package com.dblink.basic.sqlKind; 2 3 public class MySql extends Sql{ 4 //---[方法成员] 5 /*构造方法*/ 6 public MySql(String database) { 7 super(database, "3306"); 8 } 9 public MySql(String database, String port) { 10 super(database, port); 11 } 12 public MySql(String database, String port,boolean is) { 13 super(database, port); 14 } 15 public MySql(String database, boolean is,String host) { 16 super(database, "3306", host); 17 } 18 public MySql(String database, String port, String host) { 19 super(database, port, host); 20 } 21 /*获取连接 URL*/ 22 @Override 23 public String buildURL() { 24 return "jdbc:mysql://"+host+":"+port+"/"+database+"?useSSL=false&serverTimezone=UTC"; 25 } 26 /*获得相应数据库给定的驱动*/ 27 @Override 28 public String getDriver() { 29 return "com.mysql.jdbc.Driver"; 30 } 31 }
1 package com.dblink.basic.sqlKind; 2 3 public class MySql_s extends MySql{ 4 //---[方法成员] 5 /*构造方法*/ 6 public MySql_s(String database) { 7 super(database); 8 } 9 public MySql_s(String database, String port) { 10 super(database, port); 11 } 12 public MySql_s(String database, String port, boolean is) { 13 super(database, port, is); 14 } 15 public MySql_s(String database, boolean is, String host) { 16 super(database, is, host); 17 } 18 public MySql_s(String database, String port, String host) { 19 super(database, port, host); 20 } 21 /*获得相应数据库给定的驱动*/ 22 @Override 23 public String getDriver() { 24 return "com.mysql.cj.jdbc.Driver"; 25 } 26 }
1 package com.dblink.basic.sqlKind; 2 3 public class SqlServer extends Sql{ 4 //---[方法成员] 5 /*构造方法*/ 6 public SqlServer(String database) { 7 super(database, "1433"); 8 } 9 public SqlServer(String database, String port) { 10 super(database, port); 11 } 12 public SqlServer(String database, String port,boolean is) { 13 super(database, port); 14 } 15 public SqlServer(String database, boolean is,String host) { 16 super(database, "1433", host); 17 } 18 public SqlServer(String database, String port, String host) { 19 super(database, port, host); 20 } 21 /*获取连接 URL*/ 22 @Override 23 public String buildURL() { 24 return "jdbc:sqlserver://"+host+":"+port+";DatabaseName="+database+";"; 25 } 26 /*获得相应数据库给定的驱动*/ 27 @Override 28 public String getDriver() { 29 return "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 30 } 31 }
1 package com.dblink.basic.sqlKind; 2 3 public class WindowsSqlServer extends SqlServer { 4 //---[方法成员] 5 /*构造方法*/ 6 public WindowsSqlServer(String database) { 7 super(database); 8 } 9 public WindowsSqlServer(String database, String port) { 10 super(database, port); 11 } 12 public WindowsSqlServer(String database, String port, boolean is) { 13 super(database, port, is); 14 } 15 public WindowsSqlServer(String database, boolean is, String host) { 16 super(database, is, host); 17 } 18 public WindowsSqlServer(String database, String port, String host) { 19 super(database, port, host); 20 } 21 /*获取连接 URL*/ 22 @Override 23 public String buildURL() { 24 return "jdbc:sqlserver://"+host+":"+port+";integratedSecurity=true;DatabaseName="+database+";"; 25 } 26 }
1 package com.dblink.basic.sqlKind; 2 3 public class SqlServer extends Sql{ 4 //---[方法成员] 5 /*构造方法*/ 6 public SqlServer(String database) { 7 super(database, "1433"); 8 } 9 public SqlServer(String database, String port) { 10 super(database, port); 11 } 12 public SqlServer(String database, String port,boolean is) { 13 super(database, port); 14 } 15 public SqlServer(String database, boolean is,String host) { 16 super(database, "1433", host); 17 } 18 public SqlServer(String database, String port, String host) { 19 super(database, port, host); 20 } 21 /*获取连接 URL*/ 22 @Override 23 public String buildURL() { 24 return "jdbc:sqlserver://"+host+":"+port+";DatabaseName="+database+";"; 25 } 26 /*获得相应数据库给定的驱动*/ 27 @Override 28 public String getDriver() { 29 return "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 30 } 31 }
com.dblink.basic.utils 包:
1 package com.dblink.basic.utils; 2 3 import java.sql.Connection; 4 5 public interface DBUtils { 6 public Connection getConnection(); 7 }
1 package com.dblink.basic.utils; 2 3 //登录成员类 4 public class UserInfo { 5 //---[属性成员] 6 private String username; 7 private String password; 8 //---[方法成员] 9 /*set、get方法*/ 10 //set 11 public void setUsername(String username) { 12 this.username = username; 13 } 14 public void setPassword(String password) { 15 this.password = password; 16 } 17 //get 18 public String getUsername() { 19 return username; 20 } 21 public String getPassword() { 22 return password; 23 } 24 /*构造方法*/ 25 public UserInfo(String username,String password){ 26 this.username = username; 27 this.password = password; 28 } 29 /*检验*/ 30 @Override 31 public String toString(){ 32 return username+","+password; 33 } 34 }
com.dblink.basic 包:
1 package com.dblink.basic; 2 3 import com.dblink.basic.sqlKind.Sql; 4 import com.dblink.basic.utils.UserInfo; 5 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 import java.sql.SQLException; 9 10 public class SqlUtils { 11 //---[属性成员] 12 private Sql sqlInfo; 13 private UserInfo userInfo; 14 //---[方法成员] 15 private Connection getConnection() throws ClassNotFoundException, SQLException { 16 Class.forName(sqlInfo.getDriver()); 17 Connection conn = null; 18 if(userInfo==null) 19 conn = DriverManager.getConnection(sqlInfo.buildURL()); 20 else 21 conn = DriverManager.getConnection(sqlInfo.buildURL(),userInfo.getUsername(),userInfo.getPassword()); 22 23 return conn; 24 } 25 public SqlConnection getSQLConnection() throws SQLException, ClassNotFoundException { 26 return new SqlConnection(getConnection()); 27 } 28 /*构造方法*/ 29 public SqlUtils(Sql sqlInfo,UserInfo userInfo){ 30 this.sqlInfo = sqlInfo; 31 this.userInfo = userInfo; 32 } 33 }
1 package com.dblink.basic; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class SqlConnection { 9 //---[基础数据] 10 public Connection conn; 11 public Statement stmt; 12 public ResultSet rs; 13 //---[方法区] 14 //构造方法 15 public SqlConnection() { 16 this.conn = null; 17 this.stmt = null; 18 this.rs = null; 19 } 20 public SqlConnection(Connection conn) { 21 this.conn = conn; 22 this.stmt = null; 23 this.rs = null; 24 } 25 public void close() { 26 try { 27 if(this.rs!=null) { 28 this.rs.close(); 29 } 30 if(this.stmt!=null) 31 this.stmt.close(); 32 if(this.conn!=null) 33 this.conn.close(); 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 } 37 } 38 }
com.dblink.bean 包:
1 package com.dblink.bean; 2 3 import org.json.JSONArray; 4 5 import java.util.ArrayList; 6 import java.util.Collection; 7 8 public class BeanGroup extends ArrayList <JavaBean> { 9 //---[方法区] 10 //构造方法 11 public BeanGroup(int initialCapacity) { 12 super(initialCapacity); 13 } 14 public BeanGroup() { 15 super(); 16 } 17 public BeanGroup(Collection<JavaBean> c) { 18 super(c); 19 } 20 //转化方法 21 public String toString(){ 22 return super.toString(); 23 } 24 public JSONArray toJSONArray(){ 25 JSONArray jsonArray = new JSONArray(); 26 int leng = super.size(); 27 for(int i=0;i<leng;++i){ 28 JavaBean jb = super.get(i); 29 jsonArray.put(jb.toJSONArray()); 30 } 31 return jsonArray; 32 } 33 }
1 package com.dblink.bean; 2 3 import org.json.JSONArray; 4 import org.json.JSONObject; 5 6 import java.util.ArrayList; 7 import java.util.Collection; 8 9 public class JavaBean extends ArrayList<Object> { 10 //---[方法区] 11 //构造方法 12 public JavaBean() { 13 super(); 14 } 15 public JavaBean(Collection<Object> c) { 16 super(c); 17 } 18 public JavaBean(int initialCapacity) { 19 super(initialCapacity); 20 } 21 //转化方法 22 public String toString(){ 23 return super.toString(); 24 } 25 public JSONArray toJSONArray(){ 26 JSONArray jsonArray = new JSONArray(); 27 int leng = super.size(); 28 for(int i=0;i<leng;++i){ 29 Object q = super.get(i); 30 jsonArray.put(q); 31 } 32 return jsonArray; 33 } 34 }
1 package com.dblink.bean; 2 3 import org.json.JSONArray; 4 import org.json.JSONObject; 5 6 public class ReadableTable { 7 //---[属性成员] 8 //列名称集合 9 public JavaBean cloName; 10 //表格详细信息 11 public BeanGroup beans; 12 //---[方法成员] 13 /*构造方法*/ 14 public ReadableTable(){ 15 this.cloName = null; 16 this.beans = null; 17 } 18 public ReadableTable(JavaBean cloName,BeanGroup beans){ 19 this.cloName = cloName; 20 this.beans = beans; 21 } 22 /*格式转化*/ 23 public JSONObject toJSONObject(){ 24 JSONObject jsonObject = new JSONObject(); 25 jsonObject.put("Length",this.beans.size()); 26 jsonObject.put("Column",this.cloName.size()); 27 jsonObject.put("cloName",this.cloName.toJSONArray()); 28 jsonObject.put("beans",this.beans.toJSONArray()); 29 return jsonObject; 30 } 31 public JSONArray toJSONArray(){ 32 JSONArray jsonArray = new JSONArray(); 33 JSONObject jsonObject = new JSONObject(); 34 int leng = this.beans.size(); 35 int cloNum = this.cloName.size(); 36 jsonObject.put("Length",leng); 37 jsonObject.put("Column",cloNum); 38 jsonObject.put("ColNames",this.cloName.toJSONArray()); 39 jsonArray.put(jsonObject); 40 for (int i=0;i<leng;++i) 41 { 42 JSONObject jso = new JSONObject(); 43 JavaBean jb = this.beans.get(i); 44 for(int j=0;j<cloNum;++j) 45 { 46 Object obj = jb.get(j); 47 String name = this.cloName.get(j).toString(); 48 jso.put(name,obj); 49 } 50 jsonArray.put(jso); 51 } 52 return jsonArray; 53 } 54 }
com.dblink.sql 包:
1 package com.dblink.sql; 2 3 import com.dblink.basic.SqlConnection; 4 import com.dblink.basic.SqlUtils; 5 import com.dblink.bean.BeanGroup; 6 import com.dblink.bean.JavaBean; 7 import com.dblink.bean.ReadableTable; 8 9 import java.sql.PreparedStatement; 10 import java.sql.ResultSetMetaData; 11 import java.sql.SQLException; 12 13 /*数据库连接中介者*/ 14 public class DBLink { 15 //---[基础数据] 16 //连接信息 17 protected SqlConnection sqlConnection = null; 18 //---[方法区] 19 //构造方法 20 public DBLink(SqlUtils sqlUtils){ 21 buildConnection(sqlUtils); 22 } 23 //建立连接 24 protected void buildConnection(SqlUtils sqlUtils) { 25 try { 26 this.sqlConnection = sqlUtils.getSQLConnection(); 27 } catch (ClassNotFoundException | SQLException e) { 28 e.printStackTrace(); 29 } 30 } 31 //重新设置连接 32 public void reset(){ 33 try { 34 if(this.sqlConnection.rs!=null) 35 this.sqlConnection.rs.close(); 36 if(this.sqlConnection.stmt!=null) 37 this.sqlConnection.stmt.close(); 38 } catch (SQLException e) { 39 e.printStackTrace(); 40 } 41 } 42 //获取查询结果 43 public ReadableTable getSelect(String sql) throws SQLException{ 44 ReadableTable rt = new ReadableTable(); 45 BeanGroup bg = new BeanGroup(); 46 JavaBean jb = new JavaBean(); 47 48 reset(); 49 this.sqlConnection.stmt = this.sqlConnection.conn.createStatement(); 50 this.sqlConnection.rs = this.sqlConnection.stmt.executeQuery(sql); 51 52 ResultSetMetaData resultSetMetaData = this.sqlConnection.rs.getMetaData(); 53 54 int leng = resultSetMetaData.getColumnCount(); 55 56 for(int i=1;i<=leng;++i) 57 { 58 String name = resultSetMetaData.getColumnName(i); 59 jb.add(name); 60 } 61 62 while(this.sqlConnection.rs.next()) 63 { 64 JavaBean jbs = new JavaBean(); 65 for(int j=1;j<=leng;++j) 66 { 67 String cn = resultSetMetaData.getColumnName(j); 68 jbs.add(this.sqlConnection.rs.getObject(cn)); 69 } 70 bg.add(jbs); 71 } 72 73 rt.beans = bg; 74 rt.cloName = jb; 75 76 return rt; 77 } 78 //执行操作语句 79 public void executeSql(String sql){ 80 try { 81 reset(); 82 this.sqlConnection.stmt = this.sqlConnection.conn.createStatement(); 83 this.sqlConnection.stmt = (PreparedStatement) this.sqlConnection.conn.prepareStatement(sql); 84 ((PreparedStatement)sqlConnection.stmt).executeUpdate(); 85 this.sqlConnection.stmt.close(); 86 } catch (SQLException e) { 87 System.out.println("爷爷!你的数据库连接出现问题啦!"); 88 } 89 } 90 //释放连接 91 public void free(){ 92 this.sqlConnection.close(); 93 } 94 }
1 package com.dblink.sql; 2 3 import com.dblink.basic.SqlUtils; 4 5 /*可储存连接信息的数据库连接中介者*/ 6 public class RoyalDBLink extends DBLink { 7 protected SqlUtils sqlUtils = null; 8 public RoyalDBLink(SqlUtils sqlUtils) { 9 super(sqlUtils); 10 this.sqlUtils = sqlUtils; 11 } 12 public SqlUtils getSqlUtils(){ 13 return this.sqlUtils; 14 } 15 }
然后是测试类:com.dblink.sql 包内
1 package com.dblink.sql; 2 3 import com.dblink.basic.SqlUtils; 4 import com.dblink.basic.sqlKind.MySql_s; 5 import com.dblink.basic.sqlKind.Oracle; 6 import com.dblink.basic.sqlKind.WindowsSqlServer; 7 import com.dblink.basic.utils.UserInfo; 8 9 import java.sql.SQLException; 10 11 public class DBTest { 12 public static void main(String[] args) throws SQLException { 13 14 //测试 1 15 //使用MySql数据库,使用用户登录模式 16 DBLink dbLink1 = new DBLink(new SqlUtils(new MySql_s("hebei"),new UserInfo("root","cdhbhbgbjzdz"))); 17 System.out.println(dbLink1.getSelect("Select * From tree").toJSONObject()); 18 dbLink1.free(); 19 20 //测试 2 21 //使用SqlServer数据库(Windows验证模式) 22 DBLink dbLink2 = new DBLink(new SqlUtils(new WindowsSqlServer("bookMis"),null)); 23 System.out.println(dbLink2.getSelect("Select * From book").beans.toJSONArray()); 24 dbLink2.free(); 25 26 //测试 3 27 //使用Oracle数据库,使用用户登录模式 28 DBLink dbLink3 = new DBLink(new SqlUtils(new Oracle("ORACLE"),new UserInfo("SCOTT","cdbhbgbjzdz"))); 29 System.out.println(dbLink3.getSelect("Select * From T_USER").beans.toJSONArray()); 30 dbLink3.free(); 31 32 } 33 }
PS:
这里补充一下:连接每一种数据库需要的驱动都不一样
小编使用IDEA编写的Java程序(JDK版本为1.8,也就是8),SqlServer安装了2012版本,对应的Jar包如下:(外部依赖项)
【小编网不好,图传不上去,到时候,补上】
对应下载地址:(请根据自己下载的数据库版本选择相应的驱动Jar包)
Mysql:https://dev.mysql.com/downloads/connector/j/
SqlServer:https://www.microsoft.com/zh-CN/download/details.aspx?displaylang=en&id=11774
Oracle:https://www.oracle.com/database/technologies/appdev/jdbc.html
如果上述链接失效,可以采用百度搜索 “【数据库名称】 驱动 官网 下载 ”的检索式进行检索
注:如果你要选择SqlServer的Windows验证模式,请你一定要安装驱动并从官网解压下来的压缩包内找到 sqljdbc_auth.dll 文件,把它复制到 C:\Windows\System32 目录下(Linux系统的无法使用windows验证,算是废话吧)
分析用到的设计模式:
1、建造者模式:对Connection的创建是由外类的方法创建的,创建过程不对外部实体进行公开。
2、桥接模式:将原本的连接信息拆分成UserInfo和Sql两部分,是对“数据库“部分的抽象化和”登录“部分的抽象化的分离,满足接口隔离原则。
3、策略模式:将数据库具体构建的方法封装起来,程序员调用时可以根据不同数据库的需要选择不同的策略。
4、模板方法模式:这个在未拆分的原数据库连接类中就已经实现了,具体实现在DBLink类当中。
5、备忘录模式:RoyalDBLink类是对DBLink类的备忘记录类,不像DBLink只要连接建立之后,就已经丢失了连接信息。
6、中介者模式:ReadableTable类是数据和DBLink的中介者,起到了协调使用的左右(ReadableTable是索取了本次索引的包括列名和全体数据的集合)。
至于外观模式,我平常一直都在用就觉得没什么说的必要了。
设计细节:
最后我没有让 SqlUtils 类 和 DBUtils 接口 划泛化关系,这是因为SqlUtils作为建造者(Builder),不应该对外展示自己的建造方法只需要返回需要的成员就好(接口的方法都是公用的,不符合本程序设计思想的需要)。
Java 使用 JDBC 连接数据库的代码整合[MySql、SqlServer、Oracle]-[经过设计模式改造](2020年寒假小目标01)
标签:root 备忘录模式 eee 实体 桥接 inux override on() asi
原文地址:https://www.cnblogs.com/onepersonwholive/p/12168785.html