2013年11月24日 星期日

J2ee ado java bean (jsp call java)


.
TableName .java

package com.projectName.bean;

public class TableName {
private int tableNameId;
private String tableNameName;
private double price;
private double agio;


public int getTableNameId() {
return tableNameId;
}

public void setTableNameId(int tableNameId) {
this.tableNameId = tableNameId;
}

public String getTableNameName() {
return tableNameName;
}

public void setTableNameName(String tableNameName) {
this.tableNameName = tableNameName;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public double getAgio() {
return agio;
}

public void setAgio(double agio) {
this.agio = agio;
}
}

.



ProjectNameDAO.java
package com.sanqing.dao;

import java.util.List;

import com.sanqing.bean.TableName;

public interface TableNameDAO {
public void addTableName(TableName tableName);
public void updateTableName(TableName tableName);
public void deleteTableName(int tableNameId);
public List<TableName> findAllTableName();
public TableName findTableNameById(int tableNameId);
}






.ProjectNameDAOImpl.java

package com.projectName.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.projectName.bean.TableName;
import com.projectName.util.DBConnection;

public class TableNameDAOImpl implements TableNameDAO {
public void addTableName(TableName tableName) {
Connection conn = DBConnection.getConnection();
String addSQL = "insert into tableName(tableNameName,price,agio) values(?,?,?)";
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(addSQL);
pstmt.setString(1, tableName.getTableNameName());
pstmt.setDouble(2, tableName.getPrice());
pstmt.setDouble(3, tableName.getAgio());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(pstmt);
DBConnection.close(conn);
}
}

public void deleteTableName(int tableNameId) {
Connection conn = DBConnection.getConnection();
String updateSQL = "delete from tableName where tableNameId=?";
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(updateSQL);
pstmt.setInt(1, tableNameId);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(pstmt);
DBConnection.close(conn);
}
}

public List<TableName> findAllTableName() {
Connection conn = DBConnection.getConnection();
String updateSQL = "select * from tableName";
PreparedStatement pstmt = null;
List<TableName> tableNameList = new  ArrayList<TableName>();
try {
pstmt = conn.prepareStatement(updateSQL);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
TableName tableName = new TableName();
tableName.setTableNameId(rs.getInt(1));
tableName.setTableNameName(rs.getString(2));
tableName.setPrice(rs.getDouble(3));
tableName.setAgio(rs.getDouble(4));
tableNameList.add(tableName);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(pstmt);
DBConnection.close(conn);
}
return tableNameList;
}

public void updateTableName(TableName tableName) {
Connection conn = DBConnection.getConnection();
String updateSQL = "update tableName set tableNameName=?," +
"price=?,agio=? where tableNameId=?";
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(updateSQL);
pstmt.setString(1, tableName.getTableNameName());
pstmt.setDouble(2, tableName.getPrice());
pstmt.setDouble(3, tableName.getAgio());
pstmt.setInt(4, tableName.getTableNameId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(pstmt);
DBConnection.close(conn);
}
}
public TableName findTableNameById(int tableNameId) {
Connection conn = DBConnection.getConnection();
String updateSQL = "select * from tableName where tableNameId = ?";
PreparedStatement pstmt = null;
TableName tableName = new TableName();
try {
pstmt = conn.prepareStatement(updateSQL);
pstmt.setInt(1, tableNameId);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
tableName.setTableNameId(rs.getInt(1));
tableName.setTableNameName(rs.getString(2));
tableName.setPrice(rs.getDouble(3));
tableName.setAgio(rs.getDouble(4));
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(pstmt);
DBConnection.close(conn);
}
return tableName;
}

}


TableNameDAOFactory.java

package com.projectName.factory;

import com.projectName.dao.TableNameDAO;
import com.projectName.dao.TableNameDAOImpl;

public class TableNameDAOFactory {
public static TableNameDAO getTableNameDAOInstance(){
return new TableNameDAOImpl();
}
}


ShowCommodityList.jsp

<%@page language="java" pageEncoding="gb2312" import="java.util.List"%>
<%@page import="com.projectName.dao.TableNameDAO"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@page import="com.projectName.factory.TableNameDAOFactory"%>
<%@page import="com.projectName.bean.TableName"%>
<html>
<head>
<title>
show Table
</title>
</head>
<body>
<%

TableNameDAO tableNameDAO = TableNameDAOFactory.getTableNameDAOInstance();

List<TableName> tableNameList = tableNameDAO.findAllTableName();

pageContext.setAttribute("tableNameList",tableNameList);
%>
<table width="700" border="1">
<c:forEach var="tableName" items="${pageScope.tableNameList}">
<tr>
<td>${tableName.tableNameId }</td>
<td>${tableName.tableNameName }</td>
<td><fmt:formatNumber type="currency" value="${tableName.price}"/></td>
<td>${tableName.agio }</td>
<td><fmt:formatNumber type="currency" value="${tableName.price * tableName.agio}"/></td>
<td><a href="AddToCar.jsp?tableNameId=${tableName.tableNameId }">less</a></td>
</tr>
</c:forEach>
<tr>
<td><a href="AddToCar.jsp">other&gt;&gt;</a></td>
</tr>
</table>
</body>
</html>





AddToChart.jsp

<%@page language="java" pageEncoding="gb2312" import="java.util.*"%>
<%@page import="com.projectName.bean.TableName"%>
<%@page import="com.projectName.dao.EmployeeDAO"%>
<%@page import="com.projectName.factory.TableNameDAOFactory"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<html>
<head>
<title>
</title>
</head>
<body>
<%
if(request.getParameter("tableNameId") != null) {
List<TableName> car = null;
if(session.getAttribute("car") == null) {
car = new ArrayList<TableName>();
}else {
car = (List<TableName>)session.getAttribute("car");
}

EmployeeDAO tableNameDAO = TableNameDAOFactory.getTableNameDAOInstance();

int tableNameId = Integer.parseInt(request.getParameter("tableNameId"));
car.add(tableNameDAO.findTableNameById(tableNameId));
session.setAttribute("car",car);
}
if(request.getParameter("listId") != null) {

List<TableName> car = (List<TableName>)session.getAttribute("car");
int listId = Integer.parseInt(request.getParameter("listId"));
car.remove(listId);
session.setAttribute("car",car);
}
%>
Add to cart:
<table width="500" border="1">
<tr>

</tr>

<c:forEach var="tableName" items="${sessionScope.car}" varStatus="stat">
<tr>
<td>${tableName.tableNameId }</td>
<td>${tableName.tableNameName }</td>
<td><fmt:formatNumber type="currency" value="${tableName.price * tableName.agio}"/></td>
<td><a href="AddToCar.jsp?listId=${stat.index}"></a></td>
</tr>
</c:forEach>
<tr>
<td><a href="ShowTableNameList.jsp">&lt;&lt;</a></td>
</tr>
</table>
</body>
</html>







沒有留言:

張貼留言