开发第一个动态网站―――使用JavaBean

         这是一个用jsp+javabean写的一个和数据库打交道的的例子.是在这篇文章的基础上改的 http://nileader.blog.51cto.com/1381108/309034
 
         Javabean 就是一个类,这个类就定义一系列 get<Name> set<Name> 方法。
    jsp 中使用 javabean 的类的两种方法:
    方法一 jsp 中使用 <jsp:useBean> 标记符访问 javabean:
<jsp:useBean id="test" class="test.TestBean" />
 
    方法二: jsp 中嵌入 java 代码方式访问 javabean
首行导入 javabean <%@ page import="com.javaBean.TestBean" %>
下边就可以像在 java 语言中那样用了:
<% TestBean testBean=new TestBean(); %>
以上两种用法的区别在于:
 
    方法一已经实例化这个javabean对象了,对象名为test,而第二种方法只是导入了这个类.
 
http://nileader.blog.51cto.com/1381108/309034 这个例子中,我们已经用纯jsp实现了与数据库的连接.这里在它的基础上修改.

 在src目录下新建一个类,cn.nileader.abc.TestBean
package cn.nileader.abc;

import java.sql.*;

import java.util.*;

public class TestBean {
    
   /*
    * 向数据库插入记录
    * @uname
    * @upwd
    */
   public void insert(String uname, String upwd) throws Exception{
    Class.forName( "com.mysql.jdbc.Driver").newInstance();        
        
    Connection connection=DriverManager.getConnection(url);        
        
    Statement statement = connection.createStatement();        
                                                            
                                                                                    
    String sql_add = "insert into users(uname,upwd)values('"+uname+ "','"+upwd+ "')";        
    statement.executeUpdate(sql_add);        
    statement.close();    
    
  }
    
   private String driverName= "zhege com.mysql.jdbc.Driver";     //驱动程序名        
   private String userName= "root";                                         //数据库用户名        
   private String userPasswd= "123456";                                 //密码        
   private String dbName= "test";                                             //数据库名        
   private String url= "jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
  public String getDriverName() {
    return driverName;
  }
  public void setDriverName(String driverName) {
    this.driverName = driverName;
  }
  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
  public String getUserPasswd() {
    return userPasswd;
  }
  public void setUserPasswd(String userPasswd) {
    this.userPasswd = userPasswd;
  }
  public String getDbName() {
    return dbName;
  }
  public void setDbName(String dbName) {
    this.dbName = dbName;
  }
  public String getUrl() {
    return url;
  }
  public void setUrl(String url) {
    this.url = url;
  }        

}
 
然后个性当初的success.jsp:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<jsp:useBean id="testBean" class="cn.nileader.abc.TestBean" />
<html>
    <head>
    <title>注册成功</title>
    </head>
    <body>    
<%                                                
request.setCharacterEncoding("UTF-8");        
String uname = request.getParameter("uname");        
String upwd = request.getParameter("upwd");        
                                                                                
testBean.insert(uname, upwd);        
%>        
        注册成功
    </body>
</html>
 

你可能感兴趣的:(开发,javabean,动态)