SQL语句的同构与异构的区别

来源:互联网 发布:立林jb2000 进入编程 编辑:程序博客网 时间:2024/06/05 14:22
同构SQL语句:    指的是两个SQL语句可编译的部分是相同的,只是参数不一样而已异构SQL语句:    指的是两个SQL语句整个的格式都是不同的。在JDBC中,PreparedStatement执行同构SQL语句的效率是比较高的,因为PreparedStatement对象一旦绑定了SQL语句,就只能执行这一条SQL语句,例如:
public static void main(String[] args) {        Connection conn = null;        PreparedStatement pstmt = null;        ResultSet rs  = null;        People people = new People();        try {            conn = ConnectionFactory.getConnection();            String selectSQL = "SELECT id,name,salary FROM emp_t WHERE id = ?";            pstmt = conn.prepareStatement(selectSQL);            pstmt.setLong(1,3);            rs = pstmt.executeQuery();            if(rs.next()){                people.setId(rs.getLong("id"));                people.setName(rs.getString("name"));                people.setSalary(rs.getDouble("salary"));            }            System.out.println(people);        } catch (SQLException e) {            e.printStackTrace();        } finally {            DBUtil.close(conn, null, pstmt, rs);        }    }
    这里只要传入不同的参数就可以得到不同的查询结果。    Statement则执行异构的SQL语句效率更高,这里就不做演示了。

如果有错误或者可以改进的地方,请各位大神指点。
原创粉丝点击