Connection conn = null; Statement stmt = null; Blog blog = new Blog(); try { // 注册 JDBC 驱动 Class.forName("com.mysql.jdbc.Driver"); // 打开连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis", "root", "123456"); // 执行查询 stmt = conn.createStatement(); String sql = "SELECT bid, name, author_id FROM blog where bid = 1"; ResultSet rs = stmt.executeQuery(sql); // 获取结果集 while (rs.next()) { Integer bid = rs.getInt("bid"); String name = rs.getString("name"); Integer authorId = rs.getInt("author_id"); blog.setAuthorId(authorId); blog.setBid(bid); blog.setName(name); } System.out.println(blog); rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } }
@Autowired JdbcTemplate jdbcTemplate; @Test public void myTest(){ List list = jdbcTemplate.query("select * from tbl_emp",new EmployeeRowMapper()); System.out.println(list); List list1 = jdbcTemplate.query("select * from tbl_emp", new BaseRowMapper(Employee.class)); System.out.println(list1); }
优点
缺点
没有解决
hibernate 是重量级关系数据库。
通过hbr.xml 或者 注解 使用。
问题:
特性: