【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.UserActionquotmethodquotregisterquotgt ltresult namequotsuccessquotgt/index.jsplt/resultgt ltresult namequoterrorquotgt/error.jsplt/resultgt lt/actiongt lt-- 配置show.action --gt ltaction namequotshowquot classquotorg.struts.useraction.UserActionquotmethodquotshowquotgt ltresult namequotsuccessquotgt/show.jsplt/resultgt ltresult namequoterrorquotgt/error.jsplt/resultgt lt/actiongt lt-- 配置update.action --gt ltaction namequotupdatequot classquotorg.struts.useraction.UserActionquotmethodquotupdatequotgt ltresult namequotsuccessquot typequotredirectActionquotgt ltparam namequotactionNamequotgtshowlt/paramgt lt/resultgt ltresult namequoterrorquotgt/error.jsplt/resultgt lt/actiongt lt-- 配置delete.action --gt ltaction namequotdeletequot classquotorg.struts.useraction.UserActionquotmethodquotdeletequotgt ltresult namequotsuccessquot typequotredirectActionquotgt ltparam namequotactionNamequotgtshowlt/paramgt lt/resultgt ltresult namequoterrorquotgt/error.jsplt/resultgt lt/actiongt lt/packagegtlt/strutsgt1.3 Beans
源码 UserBean.java:package org.struts.beanspublic class UserBean private int id private String username private String password public int getId return id public void setIdint id this.id id public String getUsername return username public void setUsernameString username this.username username public String getPassword return password public void setPasswordString password this.password password 1.4 Action
源码 UserAction.java:package org.struts.useractionimport java.sql.Connectionimport java.sql.DriverManagerimport java.sql.PreparedStatementimport java.sql.ResultSetimport java.sql.SQLExceptionimport java.util.ArrayListimport java.util.Listimport java.util.Mapimport javax.servlet.http.HttpServletRequestimport org.apache.commons.logging.Logimport org.apache.commons.logging.LogFactoryimport org.struts.beans.UserBeanimport com.opensymphony.xwork2.ActionContextimport com.opensymphony.xwork2.ActionSupportpublic class UserAction extends ActionSupport private static Log log LogFactory.getLogUserAction.class private int id private String username private String password private ListltUserBeangt userlist private Map session public int getId return id public void setIdint id this.id id public void setSessionMap session this.session session public String getUsername return username public void setUsernameString username this.username username public String getPassword return password public void setPasswordString password this.password password public ListltUserBeangt getUserlist return userlist public void setUserlistListltUserBeangt userlist this.userlist userlist / 建立数据库连接 return / 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 // log.infoquotconnect successquot catch ClassNotFoundException e e.printStackTrace catch SQLException e e.printStackTrace return conn / 用户名与密码匹配检查 return 结果标识 字符串 success 表示匹配成功 UserNotExist 表示用户不存在error 表示匹配失败 / private String userCheck log.infoquotuser checking...quot String name null String pass null Connection conn getConn String sql quotselect from users where usernamequot username quotquot PreparedStatement pstmt try pstmt conn.prepareStatementsql ResultSet rs pstmt.executeQuery while rs.next name rs.getStringquotusernamequot pass rs.getStringquotpasswordquot rs.close pstmt.close conn.close catch SQLException e e.printStackTrace if name null// 验证用户名是否存在 return quotUserNotExistquot else if name.equalsusername ampamp pass.equalspassword// 验证登录用户名与密码是否匹配 return quotsuccessquot else return quoterrorquot private UserBean queryint id log.infoquotmy query...quot Connection conn getConn String sql quotselect from users where idquot id quotquot PreparedStatement pstmt UserBean user new UserBean try pstmt conn.prepareStatementsql ResultSet rs pstmt.executeQuery while rs.next user.setIdid user.setUsernamers.getStringquotusernamequot user.setPasswordrs.getStringquotpasswordquot System.out.printlnquotmyquery: name: quot rs.getStringquotusernamequot quot tpassword: quot rs.getStringquotpasswordquot rs.close pstmt.close conn.close catch SQLException e e.printStackTrace return user/ 查看所有用户 return ListltUserBeangt list /private ListltUserBeangt findAllUser log.infoquotget all user info ...quot ListltUserBeangt list new ArrayListltUserBeangt UserBean user Connection conn getConn String sql quotSELECT FROM users ORDER BY idquot PreparedStatement pstmt try pstmt conn.prepareStatementsql ResultSet rs pstmt.executeQuery while rs.next user new UserBean user.setIdrs.getIntquotidquot user.setUsernamers.getStringquotusernamequot user.setPasswordrs.getStringquotpasswordquot list.adduser catch SQLException e e.printStackTrace return list/ 将注册信息添加到数据库表 users param username param password return /private int insertString username String password Connection conn getConn int i 0 String sql quotinsert into users usernamepassword valuesquot PreparedStatement pstmt try pstmt conn.prepareStatementsql // Statement stat conn.createStatement pstmt.setString1 username pstmt.setString2 password i pstmt.executeUpdate System.out.printlnquotinsert resutl: quot i pstmt.close conn.close catch SQLException e e.printStackTrace return iprivate static int myUpdateint id String newPass Connection conn getConn int i 0 String sql quotupdate users set passwordquot newPass quot where idquot id quotquot PreparedStatement pstmt try pstmt conn.prepareStatementsql i pstmt.executeUpdate System.out.printlnquotupdate resutl: quot i pstmt.close conn.close catch SQLException e e.printStackTrace return i/ login.action return throws Exception /public String login throws Exception log.infoquotlogin.action...quot if userCheck.equalsquotsuccessquot ActionContext.getContext.getSession .putquotusernamequot getUsername return SUCCESS else if userCheck.equalsquotUserNotExistquot return quotUserNotExistquot else return ERROR/ register.action return throws Exception /public String register throws Exception String name getUsername String pass getPassword int i insertname pass if i 0 return quotsuccessquot else return quoterrorquotpublic String show throws Exception log.infoquotshow.action...quot userlist findAllUser if userlist.size gt 0 return quotsuccessquot else return quoterrorquot public String update throws Exception log.infoquotidquotid UserBean userqueryid if nulluser setUsernameuser.getUsername log.infoquotusernamequotusername myUpdateidpassword return quotsuccessquot else return quoterrorquot public String delete throws Exception Connection conn getConn int i 0 System.out.printlnquotidquot id String sql quotdelete users where id quot id quotquot PreparedStatement pstmt try pstmt conn.prepareStatementsql i pstmt.executeUpdate System.out.printlnquotdelete resutl: quot i pstmt.close conn.close catch SQLException e e.printStackTrace if i gt 0 return quotsuccessquot else return quoterrorquot 1.5 index.jsplt page languagequotjavaquot importquotjava.util.quot contentTypequottext/html charsetGBKquotgtlt String path request.getContextPath String basePath request.getScheme quot://quot request.getServerName quot:quot request.getServerPort path quot/quotgtlttaglib uriquot/struts-tagsquot prefixquotsquotgtltDOCTYPE HTML PUBLIC quot-//W3C//DTD HTML 4.01 Transitional//ENquotgtlthtmlgtltheadgtltbase hrefquotltbasePathgtquotgtlttitlegt登陆界面lt/titlegtltmeta http-equivquotpragmaquot contentquotno-cachequotgtltmeta http-equivquotcache-controlquot contentquotno-cachequotgtltmeta http-equivquotexpiresquot contentquot0quotgtltmeta http-equivquotkeywordsquot contentquotkeyword1keyword2keyword3quotgtltmeta http-equivquotdescriptionquot contentquotThis is my pagequotgtlt-- ltlink relquotstylesheetquot typequottext/cssquot hrefquotstyles.cssquotgt --gtlt/headgtltbodygt lt-- 提交请求参数的表单 --gt ltform actionquotloginquot methodquotpostquotgt lttable alignquotcenterquotgt ltcaptiongt lth3gt用户登录lt/h3gt lt/captiongt lttrgt lt-- 用户名的表单域 --gt lttdgt用户名:ltinput typequottextquot namequotusernamequot /gt lt/tdgt lt/trgt lttrgt lt-- 密码的表单域 --gt lttdgt密ampnbspampnbsp码:ltinput typequotpasswordquot namequotpasswordquot/gt lt/tdgt lt/trgt lttr alignquotcenterquotgt lttd colspanquot2quot alignquotrightquotgtltinput typequotsubmitquot valuequot登录quot namequotmethod:loginquot /gtltinput typequotbuttonquot valuequot注册quot onclickquotwindow.location.hrefregister.jspquot/gt lt/tdgt lt/trgt lt/tablegt lt/formgtlt/bodygtlt/htmlgt1.6 register.jsplt page languagequotjavaquot importquotjava.util.quot contentTypequottext/html charsetGBKquotgtltString path request.getContextPathString basePath request.getSchemequot://quotrequest.getServerNamequot:quotrequest.getServerPortpathquot/quotgtltDOCTYPE HTML PUBLIC quot-//W3C//DTD HTML 4.01 Transitional//ENquotgtlthtmlgt ltheadgt ltbase hrefquotltbasePathgtquotgt lttitlegtMy
JSP register.
jsp starting pagelt/titlegt ltmeta http-equivquotpragmaquot contentquotno-cachequotgt ltmeta http-equivquotcache-controlquot contentquotno-cachequotgt ltmeta http-equivquotexpiresquot contentquot0quotgt ltmeta http-equivquotkeywordsquot contentquotkeyword1keyword2keyword3quotgt ltmeta http-equivquotdescriptionquot contentquotThis is my pagequotgt lt-- ltlink relquotstylesheetquot typequottext/cssquot hrefquotstyles.cssquotgt --gt lt/headgt ltbodygt lt-- 提交请求参数的表单 --gt ltform actionquotregister.actionquot methodquotpostquotgt lttable.
上一篇:
韩顺平_jdbc__课件
下一篇:
【材料工艺】纳米晶镍镀层的制备及其界面扩散与结构