搜索文章:

首页  |  Java技术  |  Asp.net  |  Asp编程  |  VC/C++  |  Delphi  |  VB编程

jsp与javaBeans的结合使用

主要的目的,在JSP中使用javaBean的预置接口操纵数据库.

*********operateDB.jsp************
<html>
 <head><title></title></head>
 <body>
 <jsp:userBean id="selectDB" class="javaBean生成的class文件的路径" scope="page">
  
////数据查询方面
<%
    selectDB.setcon("jdbc:odbc:数据源名称","sql查询语句");
    selectDB.getcon();
    while(selectDB.rs.next())
    {
     out.println(selectDB.rs.getString("相应字段"));
    }
  %>
////数据更新方面
<%
   selectDB.setcon("jdbc:odbc:数据源名称","sql更新\删除\插入语句");
   selectDB.getcon();
%>
</jsp:userBean>
 </body>
</html>
********javaBean文件********
package count;
import java.sql.*;
public class odbcconnection 
{
 public ResultSet rs;
 String strsql,odbcpath;
 public void setcon(String path,String sql)
 {
  odbcpath=path;
  strsql=sql;
 }
 public void getcon()
 {
 try
 {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");    
  Connection con=DriverManager.getConnection(odbcpath);
  Statement stmt=con.createStatement();

  if(strsql.substring(0,6).equals("select"))
  {rs=stmt.executeQuery(strsql);}

  else
  {stmt.execute(strsql);}
 }catch(Exception s){}
}
}

下一篇:关于seeion的文章 >>
相关文章:
© 2006   www.java-asp.net