Role是一个实体类,传进来的sql,查询的是rule_id和rule_name两个字段,所以要指定一下具体的值
- Role role = new Role();
- role.setId(rs.getInt("role_id"));
- role.setName(rs.getString("role_name"));
完整代码如下:
- public static Connection getConnectionByJDBC() {
- Connection conn = null;
- try { // 装载驱动类
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- System.out.println("装载驱动异常!");
- e.printStackTrace();
- }
- try { // 建立JDBC连接
- conn = DriverManager.getConnection(
- "jdbc:oracle:thin:@192.168.0.3:1521:test", "test",
- "test");
- } catch (SQLException e) {
- System.out.println("链接数据库异常!");
- e.printStackTrace();
- }
- return conn;
- }
- public static List findByUserCodeAndDeptCode(String hql) {
- Connection conn = getConnectionByJDBC();
- List roleList = new ArrayList();
- try { // 创建一个JDBC声明
- Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
- ResultSet.CONCUR_READ_ONLY);
- // 执行查询
- ResultSet rs = stmt.executeQuery(hql);
- while (rs.next()) {
- Role role = new Role();
- role.setId(rs.getInt("role_id"));
- role.setName(rs.getString("role_name"));
- roleList.add(role);
- }
- //insert ,update ,delete用executeUpdate方法
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- } finally {
- // 预防性关闭连接(避免异常发生时在try语句块关闭连接没有执行)
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
- return roleList;
- }
本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/536655,如需转载请自行联系原作者