这是我用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);
}
}
}
输出是:
ClientId=1
Name=[B@8146158
Uname=[B@7202dc8c
AutoPrune=1
FileRetention=2592000
JobRetention=15552000
但Name和Uname的类型应为&#34; string&#34;在MySQL中,我在哪里犯了错误? 感谢。
答案 0 :(得分:1)
使用String s = new String(bytes);
。表示为byte[]
,它是从选择查询返回的。
在你的程序中,就像是,
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 );
}
我可能有拼写错误。但试试这个。看起来你得到byte []
答案 1 :(得分:0)
检查此页面以了解SQL类型和java类型之间的映射
http://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html