Skip to content

Commit 61e7731

Browse files
committed
- Updated the JSON write() methods in the HTTP Request class to encode the JSON to UTF-8
- Added new getTableNames() method to the Database class - Removed debug/print statement in the Connection class git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@1560 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent da5b9bc commit 61e7731

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

src/javaxt/http/Request.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,19 +427,31 @@ public void write(byte[] payload) {
427427
//** write
428428
//**************************************************************************
429429
/** Used to open an HTTP connection and sent a JSON object to the server.
430+
* Note that the JSON object is encoded using UTF-8. In some applications,
431+
* the encoding may need to be explicitly defined in the "Content-Type"
432+
* request header. Example:
433+
<pre>
434+
request.setHeader("Content-Type", "application/json;charset=UTF-8");
435+
</pre>
430436
*/
431437
public void write(javaxt.json.JSONObject json){
432-
write(json.toString().getBytes());
438+
write(json.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8));
433439
}
434440

435441

436442
//**************************************************************************
437443
//** write
438444
//**************************************************************************
439445
/** Used to open an HTTP connection and sent a JSON array to the server.
446+
* Note that the JSON array is encoded using UTF-8. In some applications,
447+
* the encoding may need to be explicitly defined in the "Content-Type"
448+
* request header. Example:
449+
<pre>
450+
request.setHeader("Content-Type", "application/json;charset=UTF-8");
451+
</pre>
440452
*/
441453
public void write(javaxt.json.JSONArray arr){
442-
write(arr.toString().getBytes());
454+
write(arr.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8));
443455
}
444456

445457

src/javaxt/sql/Connection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public boolean open(java.sql.Connection conn){
205205
</pre>
206206
*/
207207
public void close(){
208-
System.out.println("Closing connection...");
208+
//System.out.println("Closing connection...");
209209
try{Conn.close();}
210210
catch(Exception e){
211211
//e.printStackTrace();

src/javaxt/sql/Database.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,10 @@ public void run() throws InterruptedException {
763763
//**************************************************************************
764764
//** getTables
765765
//**************************************************************************
766-
/** Used to retrieve an array of tables found in this database.
766+
/** Used to retrieve an array of tables and columns found in this database.
767767
*/
768768
public Table[] getTables() throws SQLException {
769+
if (tables!=null) return tables;
769770
try (Connection conn = getConnection()){
770771
return getTables(conn);
771772
}
@@ -775,7 +776,7 @@ public Table[] getTables() throws SQLException {
775776
//**************************************************************************
776777
//** getTables
777778
//**************************************************************************
778-
/** Used to retrieve an array of tables found in a database.
779+
/** Used to retrieve an array of tables and columns found in a database.
779780
*/
780781
public static Table[] getTables(Connection conn){
781782
Database database = conn.getDatabase();
@@ -802,6 +803,56 @@ public static Table[] getTables(Connection conn){
802803
}
803804

804805

806+
//**************************************************************************
807+
//** getTableNames
808+
//**************************************************************************
809+
/** Used to retrieve an array of table names found in the database. If a
810+
* table is part of a schema, the schema name is prepended to the table
811+
* name. This method is significantly faster than the getTables() method
812+
* which returns the full metadata for each table.
813+
*/
814+
public String[] getTableNames() throws SQLException {
815+
java.util.ArrayList<javaxt.utils.Record> tableNames = new java.util.ArrayList<>();
816+
817+
818+
if (tables!=null){
819+
for (Table table : tables){
820+
javaxt.utils.Record record = new javaxt.utils.Record();
821+
record.set("schema", table.getSchema());
822+
record.set("table", table.getName());
823+
tableNames.add(record);
824+
}
825+
}
826+
else{
827+
try (Connection conn = getConnection()){
828+
829+
DatabaseMetaData dbmd = conn.getConnection().getMetaData();
830+
try (ResultSet rs = dbmd.getTables(null,null,null,new String[]{"TABLE"})){
831+
while (rs.next()) {
832+
javaxt.utils.Record record = new javaxt.utils.Record();
833+
record.set("schema", rs.getString("TABLE_SCHEM"));
834+
record.set("table", rs.getString("TABLE_NAME"));
835+
tableNames.add(record);
836+
}
837+
}
838+
}
839+
}
840+
841+
String[] arr = new String[tableNames.size()];
842+
for (int i=0; i<arr.length; i++){
843+
javaxt.utils.Record record = tableNames.get(i);
844+
String tableName = record.get("table").toString();
845+
String schemaName = record.get("schema").toString();
846+
if (schemaName!=null && !schemaName.isEmpty()){
847+
tableName = schemaName + "." + tableName;
848+
}
849+
arr[i] = tableName;
850+
}
851+
java.util.Arrays.sort(arr);
852+
return arr;
853+
}
854+
855+
805856
//**************************************************************************
806857
//** getCatalogs
807858
//**************************************************************************

0 commit comments

Comments
 (0)