Skip to content

Commit 7261c50

Browse files
committed
数据库连接支持更新配置
1 parent 78154c4 commit 7261c50

File tree

5 files changed

+46
-18
lines changed

5 files changed

+46
-18
lines changed

src/main/java/com/zzg/mybatis/generator/controller/NewConnectionController.java renamed to src/main/java/com/zzg/mybatis/generator/controller/DbConnectionController.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import java.net.URL;
1515
import java.util.ResourceBundle;
1616

17-
public class NewConnectionController extends BaseFXController {
17+
public class DbConnectionController extends BaseFXController {
1818

19-
private static final Logger _LOG = LoggerFactory.getLogger(NewConnectionController.class);
19+
private static final Logger _LOG = LoggerFactory.getLogger(DbConnectionController.class);
2020

2121
@FXML
2222
private TextField nameField;
@@ -35,6 +35,7 @@ public class NewConnectionController extends BaseFXController {
3535
@FXML
3636
private ChoiceBox<String> dbTypeChoice;
3737
private MainUIController mainUIController;
38+
private boolean isUpdate = false;
3839

3940

4041
@Override
@@ -48,7 +49,7 @@ void saveConnection() {
4849
return;
4950
}
5051
try {
51-
ConfigHelper.saveDatabaseConfig(config.getName(), config);
52+
ConfigHelper.saveDatabaseConfig(this.isUpdate, config);
5253
getDialogStage().close();
5354
mainUIController.loadLeftDBTree();
5455
} catch (Exception e) {
@@ -107,4 +108,16 @@ private DatabaseConfig extractConfigForUI() {
107108
return config;
108109
}
109110

111+
public void setConfig(DatabaseConfig config) {
112+
isUpdate = true;
113+
nameField.setText(config.getName());
114+
hostField.setText(config.getHost());
115+
portField.setText(config.getPort());
116+
userNameField.setText(config.getUsername());
117+
passwordField.setText(config.getPassword());
118+
encodingChoice.setValue(config.getEncoding());
119+
dbTypeChoice.setValue(config.getDbType());
120+
schemaField.setText(config.getSchema());
121+
}
122+
110123
}

src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void initialize(URL location, ResourceBundle resources) {
9696
dbImage.setFitWidth(40);
9797
connectionLabel.setGraphic(dbImage);
9898
connectionLabel.setOnMouseClicked(event -> {
99-
NewConnectionController controller = (NewConnectionController) loadFXMLPage("新建数据库连接", FXMLPage.NEW_CONNECTION, false);
99+
DbConnectionController controller = (DbConnectionController) loadFXMLPage("新建数据库连接", FXMLPage.NEW_CONNECTION, false);
100100
controller.setMainUIController(this);
101101
controller.showDialogStage();
102102
});
@@ -122,11 +122,17 @@ public void initialize(URL location, ResourceBundle resources) {
122122
if (level == 1) {
123123
final ContextMenu contextMenu = new ContextMenu();
124124
MenuItem item1 = new MenuItem("关闭连接");
125-
item1.setOnAction(event1 -> {
126-
treeItem.getChildren().clear();
127-
});
128-
MenuItem item2 = new MenuItem("删除连接");
129-
item2.setOnAction(event1 -> {
125+
item1.setOnAction(event1 -> treeItem.getChildren().clear());
126+
MenuItem item2 = new MenuItem("编辑连接");
127+
item2.setOnAction(event1 -> {
128+
DatabaseConfig selectedConfig = (DatabaseConfig) treeItem.getGraphic().getUserData();
129+
DbConnectionController controller = (DbConnectionController) loadFXMLPage("编辑数据库连接", FXMLPage.NEW_CONNECTION, false);
130+
controller.setMainUIController(this);
131+
controller.setConfig(selectedConfig);
132+
controller.showDialogStage();
133+
});
134+
MenuItem item3 = new MenuItem("删除连接");
135+
item3.setOnAction(event1 -> {
130136
DatabaseConfig selectedConfig = (DatabaseConfig) treeItem.getGraphic().getUserData();
131137
try {
132138
ConfigHelper.deleteDatabaseConfig(selectedConfig.getName());
@@ -135,7 +141,7 @@ public void initialize(URL location, ResourceBundle resources) {
135141
AlertUtil.showErrorAlert("Delete connection failed! Reason: " + e.getMessage());
136142
}
137143
});
138-
contextMenu.getItems().addAll(item1, item2);
144+
contextMenu.getItems().addAll(item1, item2, item3);
139145
cell.setContextMenu(contextMenu);
140146
}
141147
if (event.getClickCount() == 2) {

src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,27 @@ public static List<DatabaseConfig> loadDatabaseConfig() throws Exception {
8383
}
8484
}
8585

86-
public static void saveDatabaseConfig(String name, DatabaseConfig dbConfig) throws Exception {
86+
public static void saveDatabaseConfig(boolean isUpdate, DatabaseConfig dbConfig) throws Exception {
87+
String configName = dbConfig.getName();
8788
Connection conn = null;
8889
Statement stat = null;
8990
ResultSet rs = null;
9091
try {
9192
conn = ConnectionManager.getConnection();
9293
stat = conn.createStatement();
93-
ResultSet rs1 = stat.executeQuery("SELECT * from dbs where name = '" + name + "'");
94-
if (rs1.next()) {
95-
throw new RuntimeException("配置已经存在, 请使用其它名字");
94+
if (!isUpdate) {
95+
ResultSet rs1 = stat.executeQuery("SELECT * from dbs where name = '" + configName + "'");
96+
if (rs1.next()) {
97+
throw new RuntimeException("配置已经存在, 请使用其它名字");
98+
}
9699
}
97100
String jsonStr = JSON.toJSONString(dbConfig);
98-
String sql = String.format("INSERT INTO dbs values('%s', '%s')", name, jsonStr);
101+
String sql;
102+
if (isUpdate) {
103+
sql = String.format("UPDATE dbs SET value = '%s' where name = '%s'", jsonStr, configName);
104+
} else {
105+
sql = String.format("INSERT INTO dbs values('%s', '%s')", configName, jsonStr);
106+
}
99107
stat.executeUpdate(sql);
100108
} finally {
101109
if (rs != null) rs.close();

src/main/java/com/zzg/mybatis/generator/util/DbUtil.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ public static List<String> getTableNames(DatabaseConfig config) throws Exception
6868
while (rs.next()) {
6969
tables.add(rs.getString("name"));
7070
}
71-
// rs = md.getTables(config.getSchema(), null, null, new String[] {"TABLE", "VIEW"});
72-
} else {
71+
} else if (DbType.valueOf(config.getDbType()) == DbType.Oracle){
7372
rs = md.getTables(null, config.getUsername().toUpperCase(), null, new String[] {"TABLE", "VIEW"});
73+
} else {
74+
rs = md.getTables(null, config.getUsername().toUpperCase(), null, null);
7475
}
7576
while (rs.next()) {
7677
tables.add(rs.getString(3));

src/main/resources/fxml/newConnection.fxml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<?import javafx.collections.*?>
99
<?import com.zzg.mybatis.generator.model.*?>
1010

11-
<AnchorPane prefHeight="389.0" prefWidth="569.0" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zzg.mybatis.generator.controller.NewConnectionController">
11+
<AnchorPane prefHeight="389.0" prefWidth="569.0" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zzg.mybatis.generator.controller.DbConnectionController">
1212
<children>
1313
<GridPane alignment="CENTER_RIGHT" layoutX="10.0" layoutY="30.0" vgap="5.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0">
1414
<columnConstraints>

0 commit comments

Comments
 (0)