Skip to content

Commit 587ff71

Browse files
author
pengyu
committed
update version to 1.1, support update and delete sql
1 parent 66df8ba commit 587ff71

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>py</groupId>
1212
<artifactId>java-sql-cli</artifactId>
13-
<version>1.0</version>
13+
<version>1.1</version>
1414
<name>sql client written in java</name>
1515
<description>sql helper</description>
1616

src/main/java/py/sqlcli/SqlCliCommands.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.shell.standard.ShellComponent;
77
import org.springframework.shell.standard.ShellMethod;
88
import org.springframework.shell.standard.ShellOption;
9+
import org.springframework.util.StringUtils;
910

1011
import javax.annotation.PostConstruct;
1112
import javax.annotation.PreDestroy;
@@ -72,6 +73,27 @@ public void init() {
7273
public String exec(
7374
@ShellOption() String sql,
7475
@ShellOption() boolean json) {
76+
77+
sql = sql.trim();
78+
if (StringUtils.startsWithIgnoreCase(sql, "select")) {
79+
return execQuery(sql, json);
80+
} else {
81+
return execUpdate(sql);
82+
}
83+
84+
}
85+
86+
private String execUpdate(String sql) {
87+
try {
88+
int rows = stmt.executeUpdate(sql);
89+
return "rows: "+rows;
90+
} catch (SQLException e) {
91+
e.printStackTrace();
92+
return "error: " + e.getMessage();
93+
}
94+
}
95+
96+
private String execQuery(String sql, boolean json) {
7597
try (ResultSet rs = stmt.executeQuery(sql)) {
7698
final ResultSetMetaData metaData = rs.getMetaData();
7799
final int columnCount = metaData.getColumnCount();

0 commit comments

Comments
 (0)