java中从数据库中取数据方法
要获取实体bean的用方法:
其他的可以用
查询,或者用statement方法
或者用方法:
String Hql = "from Users as t where t.name = ? and t.password = ?"; List list = this.commDAO.getHibernateTemplate().find(Hql, new Object[] { user.getName(), user.getPassword() });
这个比用jdbctemplate一个一个赋值快
其他的可以用
List result = this.jdbcService.getJdbcTemplate().queryForList(sql); List<String> powerList = new ArrayList<String>(); Iterator iterator = result.iterator(); while (iterator.hasNext()) { Map itmap = (Map) iterator.next(); String s = (String) itmap.get("POWER"); }
查询,或者用statement方法
Session s = this.getHibernateTemplate().getSessionFactory().openSession(); Connection conn; Statement stmt = null; try { conn = s.connection(); stmt = conn.createStatement(); stmt.executeUpdate(sql);// .execute(sql); } catch (Exception ex) { ex.printStackTrace(); } finally { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } s.disconnect(); s.close(); }
或者用方法:
public String SelectOneFieldValue(String sql, String fieldName) throws Exception { String value = null; Connection conn = this.getSession().connection(); Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery(sql.toString()); while (rs.next()) { value = rs.getString(fieldName); } rs.close(); statement.close(); return value; }