【Jsp精品源码栏目提醒】:以下是网学会员为您推荐的Jsp精品源码-Struts2连接oracle数据库增删改查实例的实现 - 其它资料,希望本篇文章对您学习有所帮助。
1 Struts2 连接 oracle 数据库增删改查实例的实现说明:我在数据库创建了触发器来自动添加 id,所以没有出现设定 id 值。
连接 oracle 数据库需要 class12.jar 和 oracle14.jar 两个包,放入 lib在数据库创建 users 表:create table users id number not nullusername varchar220password varchar230创建序列表 users_xh 用来 id 递增Create table users_xh xh_id number创建两个触发器:create or replace trigger users_increase beforeinsert on users for each rowbegin select xh_id1 into :New.id from users_xhend和create or replace trigger users_increase_after afterinsert on users for each rowdeclare -- local variables herebegin update users_xh set xh_id xh_id1end users_increase_after创建类 DatabaseTest,内容如下:public class DatabaseTest private static Connection getConn String driver quotoracle.jdbc.driver.OracleDriverquot String url quotjdbc:oracle:thin:localhost:1521:orclquot String username quotscottquot//数据库用户名 String password quottigerquot//数据库用户密码 Connection conn null try Class.forNamedriver // new oracle.jdbc.driver.OracleDriver conn DriverManager.getConnectionurl username password System.out.printlnquotconnect successquot catch ClassNotFoundException e e.printStackTrace catch SQLException e e.printStackTrace return conn public static void mainString args getConn Run as java application 运行结果如下,表示数据库连接成功!这样数据库环境搭建无错,可以进行下一步。
创建 web project 结构如下所需的 jar 包:1.1 web.xml 内容:ltxml versionquot1.0quot encodingquotUTF-8quotgtltweb-app xmlns:xsiquothttp://www.w3.org/2001/XMLSchema-instancequotxmlnsquothttp://java.sun.com/xml/ns/javaeequotxmlns:webquothttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsdquotxsi:schemaLocationquothttp://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsdquot versionquot3.0quotgt ltdisplay-namegtlt/display-namegt ltwelcome-file-listgt ltwelcome-filegtindex.jsplt/welcome-filegt lt/welcome-file-listgt ltfiltergt ltfilter-namegtstruts2lt/filter-namegtltfilter-classgtorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterlt/filter-classgt lt/filtergt ltfilter-mappinggt ltfilter-namegtstruts2lt/filter-namegt lturl-patterngt/lt/url-patterngt lt/filter-mappinggtlt/web-appgt1.2 Action 配置 struts.xml 内容:ltxml versionquot1.0quot encodingquotUTF-8quot gtltDOCTYPE struts PUBLIC quot-//Apache Software Foundation//DTD Struts Configuration2.1//ENquot quothttp://struts.apache.org/dtds/struts-2.1.dtdquotgtltstrutsgt lt--struts2.0默认的配置文件 --gt ltinclude filequotstruts-default.xmlquotgtlt/includegt lt-- 也可以加载其他的配置文件 --gt lt-- ltinclude filequotmystrutsconfig.xmlquotgtlt/includegt --gt lt-- 添加package --gt ltpackage namequotuseractionquot extendsquotstruts-defaultquotgt lt-- 配置login.action --gt ltaction namequotloginquot classquotorg.struts.useraction.UserActionquotmethodquotloginquotgt ltresult namequotsuccessquotgt/success.jsplt/resultgt ltresult namequotUserNotExistquotgt/NoUser.jsplt/resultgt ltresult namequoterrorquotgt/error.jsplt/resultgt lt/actiongt lt-- 配置register.action --gt ltaction namequotregisterquot classquotorg.struts.useraction.UserActionquotmethodqu