数据库管理器类的设计模式(Design pattern for database manager class [closed])
我实现了一个类
Database Manager
来管理两个数据库引擎上的操作。 该类有一个私有变量databaseEngine
,它在使用类方法(删除数据库,创建数据库,运行脚本,比较,断开连接等)之前设置,并且基于此变量,类可以识别如何操作。但是,我知道这是错的,
Database Manager
的方法充满了像这样的交换机案例:public void CreateNewDatabase(String databaseName){ switch (databaseEngine){ case "mysql": //Executes a prepared statement for dropping mysql database (databaseName break; case "postgres": //Executes a prepared statement for dropping postgres database (databaseName break; ... } }
我需要一个很好的建议。 我想加载配置和资源文件夹中的所有内容,我的意思是,准备好的语句用于创建和删除等。如果需要支持新的数据库引擎,它将不会令人头疼,因为它只需要保存sql sripts在资源文件和配置文件中的任何其他数据。 请建议我对这种情况有用的任何设计模式。
I implemented a class
Database Manager
that manages operations on two database engines. The class has a private variabledatabaseEngine
which is set before using class methods (drop database, create database, run script, compare, disconnect, etc.) and based on this variable the class recognizes how to behave.However, and I know it's wrong,
Database Manager
's methods are full of switch cases like this one:public void CreateNewDatabase(String databaseName){ switch (databaseEngine){ case "mysql": //Executes a prepared statement for dropping mysql database (databaseName break; case "postgres": //Executes a prepared statement for dropping postgres database (databaseName break; ... } }
I require a good advice about this. I want to load everything from configuration and resources folders, I mean, the prepared statement for creating and dropping, etc. If a new database engine needs to be supported, it won't be a headache as It would just require to save sql sripts in a resources file and any other data in a configuration file. Please, suggest me any design pattern useful for this case.
原文:https://stackoverflow.com/questions/37597207
最满意答案
OK玩弄它显示了它和jQuery日期的答案完全一样。 你以0x开头的数字部分。
在0x之后取10个数字。 例如在A2:
=MID(A1, 3, 10)
将其转换为十六进制,例如A3:
= HEX2DEC(A2)
除以86400,例如A4:
=A3/86400
并将结果添加到1/1/1970日期。 例如= A5:
=A4 + Date(1970, 1, 1)
或简而言之:
=(hex2dec(mid(a1,numstart,10))/86400) + date(1970,1,1)
用
numstart
的1开始索引替换numstart
。例如3,如果你有一个12或13位数字,如0x12345678AB,你会得到12345678AB
这与在Excel中转换JSON日期/日期(1388624400000)/日期类似
除了那个:
一个。 这个问题被回答错误,并且不起作用。 (我编辑它)
湾 .sql文件是通过SQL从数据库的存储过程中检索的。 虽然在他们使用jQuery返回的Ajax数据,这似乎是不同的问题。 原来他们是不同格式的相同号码。
作为补充说明,我在我的十六进制数字的开头有一个空格标记。 在我做了MID之前,我没有看到它。
注意:使用ajax返回的格式化日期(例如/ date:0x12345678ab /)时,您将
numstart
设置为8.如果hex2dec失败,请尝试在调用hex2dec之前将十六进制字符串转换为大写。 要调试只是把每个公式放在一个单独的单元格中,所以你会看到什么有效,什么不可行。OK Playing around with it showed me that it's exactly the same as the jquery date answer. You take the numeric portion starting with 0x.
Take the 10 digits AFTER the 0x. e.g. in A2:
=MID(A1, 3, 10)
Turn it into hexadecimal e.g. in A3:
= HEX2DEC(A2)
Divide by 86400 e.g. A4:
=A3/86400
And add the result to 1/1/1970 date. e.g. = A5:
=A4 + Date(1970, 1, 1)
Or in short:
=(hex2dec(mid(a1,numstart,10))/86400) + date(1970,1,1)
Replace
numstart
with the 1-starting index of the number.e.g. 3 if you have a 12 or 13 digit number like 0x12345678AB and you'll get 12345678AB
This is similar to the Convert JSON Date /Date(1388624400000)/ to Date in Excel
Except that:
a. The question was answered wrong and wouldn't work. (I edited it)
b. The .sql file was retrieved in a stored procedure from the database via SQL. While in the question they were using jquery returned ajax data, which seemed to differ. Turns out they're the same number with a different format.
As an added remark, I had a space mark at the beginning of my hex number. Until I did the MID on it, I didn't see that.
Note: When using ajax returned formatted dates like /date:0x12345678ab/ you'll set
numstart
to 8. If hex2dec fails, try turning the hex string into uppercase before calling hex2dec. To debug just put each formula in a separate cell, so you see what works and what doesn't.
相关问答
更多-
我有一个解决方案。 首先,我将Excel格式从xsl更改为xslx。 我使用了像这样的CellStyle格式: CellStyle dateStyle = wb.createCellStyle(); DataFormat df = wb.createDataFormat(); dateStyle.setDataFormat(df.getFormat("dd.mm.yyyy")); ... case "java.sql.Timestamp": { cell.setCellStyle(dateStyle); ...
-
来自SQL的Excel中的日期(Date in Excel from SQL)[2021-12-15]
OK玩弄它显示了它和jQuery日期的答案完全一样。 你以0x开头的数字部分。 在0x之后取10个数字。 例如在A2: =MID(A1, 3, 10) 将其转换为十六进制,例如A3: = HEX2DEC(A2) 除以86400,例如A4: =A3/86400 并将结果添加到1/1/1970日期。 例如= A5: =A4 + Date(1970, 1, 1) 或简而言之: =(hex2dec(mid(a1,numstart,10))/86400) + date(1970,1,1) 用numstart的1开始 ... -
从SQL到Excel日期格式(From SQL to Excel date format)[2022-05-07]
你会踢自己... 在Excel中使日期列更宽;-) You'll kick yourself... Make the date column wider in Excel ;-) -
如何使用Excel VBA将开始日期和结束日期参数传递给SQL?(How do I pass start date and end date parameter to SQL using Excel VBA?)[2023-05-14]
我有一个类似的问题,将用户在Excel中输入的日期传递给SQL Server。 我必须做的是转换excel日期格式,这是今天2017-04-14由42839代表的地方。要将42839转换为日期格式,SQL可以采用我使用的(dateadd(d,42839,'1899-12-30 “))。 看起来你可能会传递范围(“J1”.text)为'42839'。 I had a similar issue passing dates the user inputted in excel to SQL Server. Wh ... -
一种选择可能是使DB2 IMPORT或LOAD命令执行日期格式转换。 在表中,您将文件加载到,将相应的列定义为具有DATE数据类型,然后发出如下所示的DB2命令: import from yourfile of del modified by dateformat="MM/DD/YYYY" replace into yourtable (column1, column2, ...); 之后,该值将是DATE - 无论如何应该存储日期,而不是字符串,并且您可以使用VARCHAR_FORMAT()函数 ...
-
如何在Excel中的日期周围添加单引号以便在SQL中使用?(How to add single quotation around date in Excel for use in SQL?)[2022-04-18]
评论者很接近,但不是100% ="'" & TEXT(A1,"mm/dd/yyyy") & "'" 8/13/2018是月份日年份而非日期月份年份 commenter was close but not 100% ="'" & TEXT(A1,"mm/dd/yyyy") & "'" 8/13/2018 is month day year not day month year -
使用SSIS for Excel进入SQL Server的日期格式问题(Date format problem using SSIS for Excel into SQL Server)[2022-08-01]
我想你已回答了自己的问题。 日期格式化单元格的导入被视为日期,其他格式被视为字符串。 可能你的SQL服务器日期设置是MM / dd / yyyy,因此转换。 为什么不尝试在SSIS包中添加数据转换步骤,并将列中的每个转换为单个格式 - datetime或string。 然后我确信SQL服务器将以相同的方式处理所有这些。 拉吉 I think you have answered your own question. The import of date formatted cells are treated ... -
方法#1在从田地中移除具有配方的细胞后起作用。 公式是只读的,并由Microsoft和其他帖子记录。 Method #1 worked once the cell with the formula was removed from the field. Formulas are Read-only as well documented by Microsoft and other posts.
-
您可以使用类似的东西转换为日期时间格式varchar以进行导入: =year(e4) & "-" & right("0" & month(e4), 2) & "-" & right("0" & day(e4), 2) & "T00:00:00" 这将确保每次使用两位数格式的月份和日期。 您还可以使用: =date(year(e4), month(e4), day(e4)) 这会将其转换为Excel日期格式。 You can use something like this to convert to a ...
-
您需要对日期值的字符串表达式使用明确的格式: start = Application.WorksheetFunction.Text(Cells(3, 2), "yyyy/mm/dd") ende = Application.WorksheetFunction.Text(Cells(4, 2), "yyyy/mm/dd") You need to use an unambiguous format for the string expressions of the date values: start = ...