Hello! We have successfully used your driver in the past for project and we have a new project where we need to get a new license and take a GUID (replication id) from an access database and print it out to another text file the same way it looks in access. Before purchasing, I was trying to do this with the demo download and I was able to get a 16 element byte array, but the values do not seem to match with the GUID in access.
I have looked online at this post and I also get a byte array of length 16, but I can not get the same GUID to print out in java as is displayed in the access database. (Example print outs below)
http://www.hxtt.com/support/access1365714000.html
http://www.hxtt.com/support_view_issue.jsp?product=access&id=1176808563
The GUID in the access database looks like this...
{2E850009-6FA5-4B6B-BEAE-3C405B51B1F5}
When I do a simple query from the access database with the HXTT driver - I get these bytes...
9,0,-123,46,-91,111,107,75,-66,-82,60,64,91,81,-79,-11
How should I translate/convert those bytes to print out the human-readable GUID like 2E850009-6FA5-4B6B-BEAE-3C405B51B1F5
========================================
EXAMPLE CODE
========================================
I created a sample database for testing with one table.
Database2.mdb
Table name = Table1
Column1=ID, Replication ID, Indexed Yes(No Duplicates)
Column2=displayCode, ShortText
I modified the "example1" code from the demo download to try out several ways of reading the GUID, but none are working currently...
import java.io.File;
import java.sql.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.UUID;
/* A demo show how to use Statement.executeQuery(sql). */
public class Example1 {
public static void main(String argv[]) {
try {
Class.forName("com.hxtt.sql.access.AccessDriver").newInstance();
//Please see Connecting to the Database section of Chapter 2. Installation in Development Document
//Please change "demodata" to your database directory
File mdbFile=new File("Database2.mdb");
String url = "jdbc:Access:///"+mdbFile.getAbsolutePath();
//Please replace with your query statement.
//You should read SQL syntax in HXTT Access Development Document
// String sql = "select * from test where number1>0";
String sql="select * from table1 order by displayCode limit 1 ";
Connection con = DriverManager.getConnection(url, "", "");
Statement stmt = con.createStatement();
stmt.setFetchSize(10);
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int iNumCols = resultSetMetaData.getColumnCount();
for (int i = 1; i <= iNumCols; i++) {
System.out.println(resultSetMetaData.getColumnLabel(i)
+ " " +
resultSetMetaData.getColumnTypeName(i));
}
Object colval;
while (rs.next()) {
for (int i = 1; i <= iNumCols; i++) {
if(i==1)
{
colval=rs.getBytes(i);
byte[] bytes=(byte[]) colval;
for(byte b:bytes)
{
System.out.print(b+",");
}
System.out.println();
System.out.println("Byte Array Length="+bytes.length);
System.out.print("Byte to String="+new String(bytes));
byte[] newByte={bytes[3],bytes[2],bytes[1],bytes[0],bytes[5],bytes[4],bytes[7],bytes[6],bytes[9],bytes[8],bytes[15],bytes[14],bytes[13],bytes[12],bytes[11],bytes[10]};
UUID uuid=UUID.nameUUIDFromBytes(bytes);
System.out.print(" Java UUID="+uuid.toString());
UUID uuid2=UUID.nameUUIDFromBytes(newByte);
System.out.print(" Java UUID2="+uuid2.toString());
}
else
{
colval = rs.getObject(i);
}
System.out.print(" Object Value="+colval);
}
System.out.println();
}
rs.close();
stmt.close();
con.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
========================================
OUTPUT
========================================
ID GUID AUTO_INCREMENT
displaycode VARCHAR
9,0,-123,46,-91,111,107,75,-66,-82,60,64,91,81,-79,-11,
Byte Array Length=16
Byte to String=
|
Please use the following code:)
byte[] newByte = {bytes[3], bytes[2], bytes[1], bytes[0], bytes[5], bytes[4], bytes[7], bytes[6], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]};
StringBuffer strbuf=new StringBuffer();
for (int j = 0; j < newByte.length; j++) {
byte b = newByte[j];
if (j == 4 || j == 6 || j == 8 || j == 10) {
strbuf.append("-");
}
strbuf.append(Integer.toHexString(b & 0xFF).toUpperCase());
}
System.out.println(strbuf);
// UUID uuid2 = UUID.nameUUIDFromBytes(newByte);
UUID uuid2 = UUID.fromString(strbuf.toString());
System.out.println(" Java UUID2=" + uuid2.toString().toUpperCase());
|
2016-09-06 provides java.uti.UUID value for GUID type.
Now you should use rs.getObject(i) which will return java.util.UUID object.
rs.getString(i) will return a string value too.
|