-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
139 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package examples.azure.mssql.cli; | ||
|
||
import java.io.InputStreamReader; | ||
import java.io.LineNumberReader; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
public class Cli { | ||
|
||
private Connection connection; | ||
private String[] arguments; | ||
|
||
public Cli(String[] arguments) throws SQLException { | ||
try { | ||
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); | ||
} catch (ClassNotFoundException e) { | ||
throw new SQLException("SQLServerDriver not found", e); | ||
} | ||
String url = arguments[0]; | ||
if (!url.startsWith("jdbc:")) { | ||
url = "jdbc:sqlserver://" + url; | ||
} | ||
this.connection = DriverManager.getConnection(url, arguments[1], arguments[2]); | ||
this.arguments = arguments; | ||
} | ||
|
||
public static void main(String[] arguments) throws Exception { | ||
Cli cli = new Cli(arguments); | ||
cli.run(); | ||
} | ||
|
||
public void run() throws Exception { | ||
if (arguments.length > 3) { | ||
executeSql(arguments[3]); | ||
} else { | ||
boolean interactive = true; | ||
if (interactive) { | ||
LineNumberReader reader = new LineNumberReader(new InputStreamReader(System.in)); | ||
boolean exit = false; | ||
do { | ||
System.out.print(">> "); | ||
String sql = reader.readLine(); | ||
if ("/exit".equalsIgnoreCase(sql.trim())) { | ||
exit = true; | ||
continue; | ||
} | ||
executeSql(sql); | ||
} while (!exit); | ||
} | ||
} | ||
} | ||
|
||
private void executeSql(String sql) { | ||
try { | ||
Statement statement = connection.createStatement(); | ||
boolean hasResultSet = statement.execute(sql); | ||
if (hasResultSet) { | ||
ResultSet resultSet = statement.getResultSet(); | ||
if (resultSet != null) { | ||
int columnCount = resultSet.getMetaData().getColumnCount(); | ||
for (int i = 1; i <= columnCount; i++) { | ||
System.out.print(resultSet.getMetaData().getColumnName(i) + "\t"); | ||
} | ||
System.out.println(); | ||
|
||
boolean hasResults = false; | ||
while(resultSet.next()) { | ||
hasResults = true; | ||
for (int i = 1; i <= columnCount; i++) { | ||
System.out.print(resultSet.getString(i) + "\t"); | ||
} | ||
System.out.println(); | ||
} | ||
if (!hasResults) { | ||
System.out.println("no results"); | ||
} | ||
} else { | ||
System.out.println("no results"); | ||
} | ||
} else { | ||
int updateCount = statement.getUpdateCount(); | ||
if (updateCount == -1) { | ||
System.out.println("Statement executed successfully."); | ||
} else { | ||
System.out.println("Statement executed successfully, " + updateCount + " rows affected."); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
System.out.println("Error executing statement: " + e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
sql/get-country/src/main/java/examples/azure/mssql/getcountry/GetCountry.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters