目录
前言
代码与实例
最近数据库要用达梦,个人对达梦又有了新的认识。使用的是达梦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 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 getCmdMap() {
return cmdMap;
}
public void setCmdMap(Map 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 {
@Data
class SQLTemplateStruct{
public String index;
public String data;
}
//存sqlCMD文件
private static Map> m_sqlTemplateMap = new HashMap>();
private static Map m_sqlMap = new HashMap();
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 getSingleMap(String str){
Map fileMap = new HashMap();
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 map = new HashMap();
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_sqlMap
Set stringSet = m_sqlTemplateMap.keySet();
Integer index = 0;
for(String key : stringSet){
String fStr = map.get(Integer.parseInt(m_sqlTemplateMap.get(key).get("索引")));
Set 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 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