从ResultSet里取出数据后,再构造一个对象啊。^@^
解决方案 »
- 遇到一个吐血小问题!!!(之二)
- jar 反编译后 可读性很差,包名,类名全是一大串的0oo0 是怎么回事呀? 是不是加密混淆了的,怎么能还原呀? 在线等。。。。。
- 关于EJB.JAR部署问题
- 请教关于spring 定时发送邮件 乱码的问题
- 【求程序】java 抽奖
- 关于c3p0连接池的问题,似乎有没有放开的情况
- 我都不知道这个问题是不是可笑的。。
- 求助:加载jdbc驱动的问题!谢谢!
- 太荒缪了,request.getParameter(name);其中name是form里的一个input框,内容为中文,request.getParameter(name);在tomcat中出现乱玛,
- 关于mysql的一个题目
- 给你100分!!!!!我写了一个比较简单的JSP+SBEAN+CMP的调用,问题不少,请高手指点!!
- 请教:TOMCAT怎么调用JBOSS
public static void main(String[] args) {
String uri = "jdbc:postgresql://192.168.2.63/myutf8db?useUnicode=true&characterEncoding=UTF-8";
String sql = "SELECT * FROM mytable";
String sql1 = "select * from pg_database";
DataSource dataSource = setupDataSource(uri); Connection conn = null;
Statement stmt = null;
ResultSet rset = null; try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
rset = stmt.executeQuery(sql1);
int numcols = rset.getMetaData().getColumnCount();
// column names
for (int i = 1; i <= numcols; i++) {
System.out.print("\t"+rset.getMetaData().getColumnName(i));
}
System.out.println("\n------------------------------------"); while(rset.next()) {
for(int i=1;i<=numcols;i++) {
System.out.print("\t" + rset.getString(i));
}
System.out.println("");
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
try { rset.close(); } catch(Exception e) { }
try { stmt.close(); } catch(Exception e) { }
try { conn.close(); } catch(Exception e) { }
}
} public static DataSource setupDataSource(String connectURI) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.postgresql.Driver");
ds.setUsername("postgre");
ds.setPassword("postgre");
ds.setUrl(connectURI);
return ds;
} public static void printDataSourceStats(DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
System.out.println("NumActive: " + bds.getNumActive());
System.out.println("NumIdle: " + bds.getNumIdle());
} public static void shutdownDataSource(DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
bds.close();
}
}
* 执行 select sql 查询数据库单个表,得到ArrayList 行对象object是HashMap
* @param strSql sql查询语句
* @return List
* @throws SQLException
*/
public List getTable(String strSql) {
Connection dbConn = getConn();
Statement stmt = null;
ResultSet rs = null;
List retList = new ArrayList();
Map myMap = null;
try {
stmt = dbConn.createStatement();
rs = stmt.executeQuery(strSql);
if (rs == null) {
return Collections.EMPTY_LIST;
}
ResultSetMetaData mtdata = rs.getMetaData();
int col = mtdata.getColumnCount();
while (rs.next()) {
myMap = new HashMap(col);
for (int i = 1; i <= col; i++) {
myMap.put(mtdata.getColumnName(i), rs.getString(i));
}
retList.add(myMap);
}
return retList;
}
catch (SQLException e) {
return Collections.EMPTY_LIST;
}
finally {
dbclose(dbConn, stmt, rs);
}
}