0

Here is a test program I wrote with Apache dbutils :

    public static void main(String[] args) {
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/bacula";
        String driver = "com.mysql.jdbc.Driver";
        String user = "bacula";
        String pwd = "root";
        try {
            DbUtils.loadDriver(driver);
            conn = DriverManager.getConnection(url, user, pwd);
            QueryRunner query = new QueryRunner();

            List<Map<String, Object>> mapList = query.query(conn, "select * from Client", new MapListHandler());

            for (Iterator<Map<String, Object>> li = mapList.iterator(); li.hasNext();) {
                Map<String, Object> m = li.next();  
                for (Iterator<Entry<String, Object>> mi = m.entrySet().iterator(); mi.hasNext();) {  
                    Entry<String, Object> e = mi.next();  
                    System.out.println(e.getKey() + "=" + e.getValue());  
                }  
            }  
        } catch (SQLException se) {
            se.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
    }
}

And the output is :

ClientId=1
Name=[B@8146158
Uname=[B@7202dc8c
AutoPrune=1
FileRetention=2592000
JobRetention=15552000

But Name and Uname should be of type "string" in MySQL, where did I make an error ? Thanks.

hawarden_
  • 1,904
  • 5
  • 28
  • 48

2 Answers2

1

Use String s = new String(bytes); . The representation is of byte[] that's returned from the select query.

In your program, it would be like,

String key = e.getKey();
String value = e.getValue();
if(key.equals("Name") || key.equals("UName")){
    System.out.println(key + "=" + new String((byte[])value));
}else{
   System.out.println(key  + "=" + value );
}

I might have typos. But try this one. Looks like you are getting byte[]

Karthik R
  • 5,523
  • 2
  • 18
  • 30
0

Check this page to know to the mapping between SQL types and java type

http://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html

mgalala
  • 106
  • 8