您的位置:首页 > 编程语言 > Java开发

在Spring框架中配置和使用jdbcldap开源驱动(二)

2006-04-05 03:08 183 查看

 

5、  在DAO中访问LDAP数据库

示例代码如下:

Session session = null;

Connection con = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

       //取得与ldap连接的会话实例

       session = getSessionFactory().openSession();

 

//取得数据库连接

       con = session.connection();

} catch (HibernateException e) {

       e.printStackTrace();

}

 

6、  对数据库进行增删查改操作

示例代码如下:

//            Select

              Statement st1 = null;

              try {

                     st1 = con.createStatement();

              } catch (SQLException e) {

                     e.printStackTrace();

              }

              String SQL1 = "SELECT userExp FROM ou=User where cn=fancy";

              ResultSet rs1 = st1.executeQuery(SQL1);

              while (rs1.next() && rs.getString("cn") != null

                                       && (!("".equals(rs.getString("cn"))))) {

                     System.out.println("userExp=" + rs1.getString("userExp"));

              }

             

//            Update

              String DN= "cn=fancy ";

              String SQL2 = "UPDATE ou=User SET userTelNo=? WHERE "+DN;

              try {

                     PreparedStatement update = con.prepareStatement(SQL2);

                     update.setString(1,"13888888888");

                     int count = update.executeUpdate();

              } catch (SQLException e) {

                     e.printStackTrace();

              }

             

//            INSERT

              String SQL3= "INSERT INTO cn,ou=User "

                            + "(objectClass,userName,userTelNo) "

                            + "VALUES (userInfo,风卷流云,13888888888)";

 

              Statement insert = null;

              try {

                     insert = con.createStatement();

              } catch (SQLException e) {

                     e.printStackTrace();

              }

              try {

                     int count = insert.executeUpdate(SQL3);

              } catch (SQLException e) {

                     e.printStackTrace();

              }

             

             

//            DELETE

              String SQL4 = "DELETE FROM ou=User WHERE cn=?";

              PreparedStatement ps = con.prepareStatement(SQL4);

              ps.setString(1,"fancy");

              ps.execute();

 

7、  关闭连接和会话,示例代码如下:

              con.close();

             session.close();

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: