看IBM网站的一个例子(2)
好,现在来建立到DB2的数据库连接,我机器上没装,随便抓几个图过过瘾好了,大概就是建立JDBC连接啦:
感觉其中的过滤器还是很强大地:可以设置取什么模式什么表或者不取满足什么条件的数据等等。
(连接名:BookData (随便起一个名字,自己知道就行了)
数据库:bookshop
用户标识:userid
密码:password
单击完成)干过瘾,没数据库,呵呵。(以上操作在“数据”透视图里面完成)
OK,到这里算是完成了对指定数据库的一个引用,接下来要把这个连接添加到我们的web应用中:
1. 在"数据库服务器"视图中展开BookData数据库连接,右键单击bookshop(jdbc:db2:bookshop),从弹出的相关菜单中选择导入至文件夹。
2. 在"导入"对话框中,单击浏览。
3. 在"文件夹选择"对话框中单击BookShopWEB,单击确定。
4. 单击完成。
(这个没办法截图啦,实在是没有这个环境。。。搞个MySQL的来骗骗它)
哈哈,成功了,
(估计这样导入之后该项目就可以利用这个连接名字来代替到数据库的连接了吧。)
以下参考IBM网站原文连接。没环境。
(图形化的数据库操作而已。。。。。。不过挺强。)
不过利用图形化来进行Insert页面构造代码会是这样:
INSERT INTO
tcd002
(
id,
"Name",
"Status"
)
VALUES
(
:id,
:name,
:status
)
有点像是本机动态SQL。
其中的BookInsertView.jsp页面关键代码大致如下:
<%String inputid = (String) request.getParameter("id");
String inputf_Status_ = (String) request.getParameter("f_Status_");
String inputf_Name_ = (String) request.getParameter("f_Name_");
%>
<%-- Connect to the database --%>
<%! com.ibm.db.beans.DBConnectionSpec dsSpec = null; %>
<%
if (dsSpec == null) {
%> <dab:driverManagerSpec id="BookConnection" scope="page"
userid='<%=config.getInitParameter("username")%>'
password='<%=config.getInitParameter("password")%>'
driver='<%=config.getInitParameter("driverName")%>'
url='<%=config.getInitParameter("url")%>' /> <%
dsSpec = BookConnection;
}
%>
<%--Execute the Insert Statement--%>
<dab:modify id="insert_statement" connectionSpecRef="<%=dsSpec%>">
<dab:sql>
INSERT INTO tcd002 ( id, "Name", "Status" ) VALUES ( :id, :name, :status )
</dab:sql>
<dab:parameter position="1" type="CHAR" value="<%=inputid%>" />
<dab:parameter position="3" type="CHAR" value="<%=inputf_Status_%>" />
<dab:parameter position="2" type="VARCHAR" value="<%=inputf_Name_%>" />
</dab:modify>
Number of rows updated:
<jsp:getProperty name="insert_statement" property="updateCount" />
生成的com.bookshop.web.BookController代码如下:
/**************************************************************
*Description - Book Front Controller
*
* The Controller is the initial point of contact for handling a request.
* Place in this class any services you would like to do on every request
* (Logging, Dubuging, Authentication)
*/
// Imports
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BookController extends HttpServlet implements Serializable {
/**************************************************************
* Initializes the servlet
* @param config The servlets configuration information
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
//Place code here to be done when the servlet is initialized
}
/***************************************************************
* Destroy the Servlet
*/
public void destroy() {
//Place code here to be done when the servlet is shutdown
//Destroy the database connection
}
/***************************************************************
* This method is run once for each request.
* @param request The incoming request information
* @param response The outgoing response information
*/
public void performServices(
HttpServletRequest request,
HttpServletResponse response) {
//Place any code here that you would like to run on every request
//Logging, Authentication, Debugging
}
/***************************************************************
* Process both HTTP GET and HTTP POST methods
* @param request The incoming request information
* @param response The outgoing response information
*/
public void performTask(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String nextPage;
try {
//Perform any specialized sevices
performServices(request, response);
//Get the web page associated with the command in the request
nextPage = getInitParameter(request.getParameter("command"));
} catch (Exception ex) {
//If an exception is thrown serve the error page
nextPage = getInitParameter("error_page");
}
//Forward the request to the next page
dispatch(request, response, nextPage);
}
/***************************************************************
* Process incoming requests for a HTTP GET method
* @param request The incoming request information
* @param response The outgoing response information
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
performTask(request, response);
}
/***************************************************************
* Process incoming requests for a HTTP POST method
* @param request The incoming request information
* @param response The outgoing response information
*/
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
performTask(request, response);
}
/****************************************************************
* Dispatches to the next page
* @param request The incoming request information
* @param response The outgoing response information
* @param nextPage The page to dispatch to
*/
public void dispatch(
HttpServletRequest request,
HttpServletResponse response,
String nextPage)
throws ServletException, IOException {
RequestDispatcher dispatch = request.getRequestDispatcher(nextPage);
dispatch.forward(request, response);
}
}