Java|达梦工作笔记-达梦数据库同步工具(JDBC)
生活随笔
收集整理的這篇文章主要介紹了
Java|达梦工作笔记-达梦数据库同步工具(JDBC)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
?
前言
代碼與實例
?
前言
最近數據庫要用達夢,個人對達夢又有了新的認識。使用的是達夢7,這個數據庫有很多BUG,官方提供的客戶端,不僅卡,而且登錄了后,使用Java JDBC操作更新時,是會被阻塞的,所以,在使用Java開發達夢應用時候,盡量關閉他客戶端。
另外達夢還有個模式的概念,這玩意個人感覺就和Mysql中的數據庫一樣。還有就是SQL語句,大部分情況下是一樣的。但是復雜語句就要查他的文檔了,因為直接使用Mysql的不靠譜。
這個實例是使用Maven進行項目管理!配置SQL語句模板,對數據庫從而達到更加靈活的操作。如下模板
1 @insert into %1(%2) select %3.%4 from %3 where %2 not in (select %2 from %1) #往%1表中添加新數據,此數據只有%2這一列,這個ID從%3表中找,找%4列中存在%1表中%2列中不存在的 2 @update %1 set %2 = (select %3.%2 from %3, %2 where %1.%2 = %4) #往%1更新%2數據,這個%2數據是%3中的%2數據,這里%1表的%2列要為一個頂值 3 @select %1.%2 from %1 #查詢%1表的%2列數據 4 @update %1 set %1.%2=%3.%4 from %1,%3 where %1.%5=%3.%6 #更新表%1的%2屬性,這個%2屬性是%3表里面的,判斷條件是%1表中的%5等于%3中的%6模板匹配如下:
開頭的#表示注釋掉此行。
通過這種匹配方式,可以使得程序更加的靈活
?
代碼與實例
程序運行截圖如下:
查詢下被拷貝的表:
原始數據表:
程序結構如下:
源碼如下:
DMPtr.java
package main.dmDB;import lombok.Data;import java.sql.*; import java.util.Map;public class DMPtr {private Connection connection = null;Map<Integer, String> cmdMap;public DMPtr() throws SQLException, ClassNotFoundException {String jdbcString = "dm.jdbc.driver.DmDriver";String urlString = "jdbc:dm://127.0.0.1:5236";String userName = "SYSDBA";String passWord = "SYSDBA";Class.forName(jdbcString);//連接connection = DriverManager.getConnection(urlString, userName, passWord);}/*** 打印數據* @param rs* @throws SQLException*/private static void displayResultSet(ResultSet rs) throws SQLException {// 取得結果集元數據ResultSetMetaData rsmd = rs.getMetaData();// 取得結果集所包含的列數int numCols = rsmd.getColumnCount();//列頭for (int i = 1; i <= numCols; i++) {if (i > 1) {System.out.print(",");}System.out.print(rsmd.getColumnLabel(i));}System.out.println("");//所有數據while (rs.next()) {for (int i = 1; i <= numCols; i++) {if (i > 1) {System.out.print(",");}// 普通字段System.out.print(rs.getString(i));}System.out.println("");}}public Map<Integer, String> getCmdMap() {return cmdMap;}public void setCmdMap(Map<Integer, String> cmdMap) {this.cmdMap = cmdMap;}/**** 運行SQL命令*/public void runSQLCMD() throws SQLException {for(int i = 0; i < cmdMap.size(); i++){String queryStr = cmdMap.get(i);System.out.println("開始執行:" + queryStr);Statement statement = connection.createStatement();System.out.println("影響的行數:" + statement.executeUpdate(queryStr));statement.close();}}/**** 關閉連接*/public void closeConnection() throws SQLException {connection.close();} }ReadFileEm.java
package main.errorEm;import lombok.Getter;@Getter public enum ReadFileEm {SQL_TEMPLATE_INDEX_ERROR(1,"SQL模板索引格式不對"),SQL_TEMPLATE_STRING_ERROR(2, "SQL模板數據格式不對"),SQL_LACK_STRING_ERROR(3, "SQL模板格式不對,缺少>");private Integer code;private String msg;ReadFileEm(Integer code, String msg){this.code = code;this.msg = msg;} }ReadFileException.java
package main.Exception;import main.errorEm.ReadFileEm;public class ReadFileException extends RuntimeException{public ReadFileException(ReadFileEm readFileEm){super(readFileEm.getMsg());this.code = readFileEm.getCode();}private Integer code; }ReadFile.java
package main.file;import lombok.Data; import main.Exception.ReadFileException; import main.errorEm.ReadFileEm;import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set;public class ReadFile {@Dataclass SQLTemplateStruct{public String index;public String data;}//存sqlCMD文件private static Map<String, Map<String, String>> m_sqlTemplateMap = new HashMap<String, Map<String, String>>();private static Map<Integer, String> m_sqlMap = new HashMap<Integer, String>();public void createCMD() throws IOException {/**** 讀取命令文件*/FileReader reader = new FileReader("E:\\IdeaProject2019\\Synchronization\\src\\main\\resources\\command\\sqlCmd.txt");BufferedReader bufferedReader = new BufferedReader(reader);String str = null;Integer index = 0;while ((str = bufferedReader.readLine()) != null){if(str.contains("#"))continue;;m_sqlTemplateMap.put(index.toString(), getSingleMap(str));index++;}bufferedReader.close();reader.close();System.out.println("sqlCmd文件讀取解析結束:" + m_sqlTemplateMap);/**** 讀取模板文件*/createTemplateSqlFile();return;}/*** 獲取單條數據* @param str* @return*/protected Map<String, String> getSingleMap(String str){Map<String, String> fileMap = new HashMap<String, String>();String[] split = str.split(" ");fileMap.put("索引", getTemplateIndex(split[0]));for(int i = 1; i < split.length; i++){SQLTemplateStruct templateString = getTemplateString(split[i]);fileMap.put(templateString.getIndex(), templateString.getData());}return fileMap;}/**** 獲取Template前面的索引號* @param str* @return*/protected String getTemplateIndex(String str){String ret = null;if(!str.contains("$")){throw new ReadFileException(ReadFileEm.SQL_TEMPLATE_INDEX_ERROR);}ret = str.replace("$","");return ret;}/**** 獲取里面具體的數據* @param str* @return*/protected SQLTemplateStruct getTemplateString(String str){SQLTemplateStruct struct = new SQLTemplateStruct();if(!str.contains("%") && !str.contains(">")){throw new ReadFileException(ReadFileEm.SQL_TEMPLATE_STRING_ERROR);}String[] split = str.split(">");if(split.length < 2){throw new ReadFileException(ReadFileEm.SQL_LACK_STRING_ERROR);}struct.setIndex("%" + split[0].substring(1));struct.setData(split[1]);return struct;}/**** 填充此文件* @throws IOException*/protected void createTemplateSqlFile() throws IOException {FileReader reader = new FileReader("E:\\IdeaProject2019\\Synchronization\\src\\main\\resources\\command\\sqlTemplate.txt");BufferedReader bufferedReader = new BufferedReader(reader);String str = null;Map<Integer, String> map = new HashMap<Integer, String>();while ((str = bufferedReader.readLine()) != null){String index = str.substring(0, str.indexOf('@')).replace(" ", "");str = str.substring(0, str.indexOf('#')).substring(str.indexOf('@') + 1);//System.out.println(index + " " + str);map.put(Integer.parseInt(index), str);}bufferedReader.close();reader.close();//進行拼接,填充 m_sqlMapSet<String> stringSet = m_sqlTemplateMap.keySet();Integer index = 0;for(String key : stringSet){String fStr = map.get(Integer.parseInt(m_sqlTemplateMap.get(key).get("索引")));Set<String> stringSet1 = m_sqlTemplateMap.get(key).keySet();for(String value : stringSet1){fStr = fStr.replace(value, m_sqlTemplateMap.get(key).get(value));}m_sqlMap.put(index++, fStr);}System.out.println("sql指令匹配完成,依次如下:" + m_sqlMap);}public static Map<Integer, String> getSqlMap() {return m_sqlMap;} }Main.java
package main;import main.dmDB.DMPtr; import main.file.ReadFile;import java.io.IOException; import java.sql.*;public class Main {public static void main(String[] args) throws SQLException, ClassNotFoundException {ReadFile readFile = new ReadFile();try {readFile.createCMD();} catch (IOException e) {e.printStackTrace();}System.out.println("開始操作數據庫");DMPtr dmPtr = new DMPtr();dmPtr.setCmdMap(readFile.getSqlMap());System.out.println("內容為:" + dmPtr.getCmdMap());dmPtr.runSQLCMD();System.out.println("over!");dmPtr.closeConnection();} }這里只帖出了部分源碼,具體配置直接從Github上下載吧!
項目打包下載地址:
https://github.com/fengfanchen/Java/tree/master/Synchronization
總結
以上是生活随笔為你收集整理的Java|达梦工作笔记-达梦数据库同步工具(JDBC)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java笔记-Java通过JNI调用Li
- 下一篇: Qt调用face++的http接口,实现