国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > web前端 > htmlcss > jsp如何使用javabean

jsp如何使用javabean

来源:程序员人生   发布时间:2015-02-13 08:22:53 阅读次数:2774次

javabean1般放在src目录下

jsp文件1般放在webroot下

src和webroot在同1个目录下

下面是我的目录结构,附带jsp代码和javabean代码


jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'AddToMyFavorite.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


  </head>
  
  <body  bgcolor="c0ffc0">
  <jsp:useBean id="DB" scope="page" class="lytjb.DB"></jsp:useBean>
    <%
    String username= (String)session.getAttribute("username_session");//获得当前用户
    String bookid=request.getParameter("bookid");//获得收藏书籍的id
    //out.println(bookid);
    long time=System.currentTimeMillis();
    String favorite_id=time+" ";//该收藏记录的id
    java.text.SimpleDateFormat formatter=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    java.util.Date currenttime=new java.util.Date();
    String date=formatter.format(currenttime);//收藏时间
    //out.println("usename"+username+"bookid"+bookid+"shuocangid"+favorite_id+"date"+date);
     %>
     
     <!-- 验证该用户是不是已收藏该书籍 -->
     <%
     
      String sql_query="select * from favorite where UserName='"+username+ "' and BookID='" + bookid+ "'";
      String sql_insert="insert into favorite values('"+favorite_id+ "','" +username+ "','" +bookid+ "','" +date+"')";
      ResultSet rs_query=DB.executeQuery(sql_query);
      if(rs_query.next())
      {
      out.println("<script language='javascript'>alert('您已收藏该书籍!');window.location.href='index.jsp';</script>"); 
      //response.sendRedirect("index.jsp");
      }
      else
      {
      DB.executeUpdate(sql_insert);
      out.println("<script language='javascript'>alert('收藏成功!');window.location.href='index.jsp';</script>"); 
      //response.sendRedirect("index.jsp");
      }
     
      %>
  </body>
</html>

javabean代码

package lytjb;
import java.sql.*;
//1个用于查找数据源的工具类。
public class DB {
private Connection con = null;
private Statement stmt = null;
ResultSet rs = null;


public ResultSet executeQuery(String sql) throws Exception {

try{  
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SHB","sa","1234567");  
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
//con=DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest");  
stmt=con.createStatement();  
rs=stmt.executeQuery(sql);
}catch(Exception e){}
return rs;
}


// 履行Insert,Update语句
public void executeUpdate(String sql) throws Exception {
try{  
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SHB","sa","1234567");  
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
//con=DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest");  
stmt=con.createStatement();  
int rs=stmt.executeUpdate(sql);

}catch(Exception e){}
}


// 关闭stmt和关闭连接
public void close_all() {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}


}


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生