File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 66import org .springframework .shell .standard .ShellComponent ;
77import org .springframework .shell .standard .ShellMethod ;
88import org .springframework .shell .standard .ShellOption ;
9+ import org .springframework .util .StringUtils ;
910
1011import javax .annotation .PostConstruct ;
1112import 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 ();
You can’t perform that action at this time.
0 commit comments