| 发表于:2007-07-14 12:45:543楼 得分:0 |
<%@page contenttype= "text/html; charset=gbk "%> <%@taglib uri= "http://java.sun.com/jsf/core " prefix= "f "%> <%@taglib uri= "http://java.sun.com/jsf/html " prefix= "h "%> <html> <head> <title> reg </title> </head> <body bgcolor= "#ffffff "> <h1> 注册 </h1> <f:view> <h:form id= "regform "> <h:outputlabel value= "姓名 " for= "name "> </h:outputlabel> <h:inputtext id= "name " value= "#{userbean.name} " required= "true "> </h:inputtext> <h:message for= "name "> </h:message> <p> </p> <h:outputlabel value= "年龄 " for= "age "> </h:outputlabel> <h:inputtext id= "age " value= "#{userbean.age} " required= "true "> </h:inputtext> <h:message for= "age "> </h:message> <p> </p> <h:commandbutton id= "submit " action= "#{userbean.add} " value= "提交 "> </h:commandbutton> </h:form> </f:view> </body> </html> ======================================== package jsforacle; import java.util.arraylist; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.resultset; import java.sql.statement; import java.sql.preparedstatement; public class userbean { private string name; private int age; private int id; private connection getcon(){ connection con = null; string user = "magus "; string password = "magus "; try { class.forname( "oracle.jdbc.driver.oracledriver ").newinstance(); con = drivermanager.getconnection( "jdbc:oracle:thin:@127.0.0.1:1521:magus ", user, password); } catch (classnotfoundexception ex) { system.out.println(ex); } catch (illegalaccessexception ex) { system.out.println(ex); } catch (instantiationexception ex) { system.out.println(ex); } catch (sqlexception e) { system.out.println(e); } return con; } private int getmaxid(){ int maxid = 1; connection con = getcon(); resultset rs = null; statement stmt = null; string sql = "select max(id) as maxid from users "; try { stmt = con.createstatement(); rs = stmt.EXECutequery(sql); if(rs.next()){ maxid = rs.getint( "maxid "); } } catch (sqlexception ex) { system.out.println(ex); }finally{ try { con.close(); } catch (sqlexception ex1) { system.out.println(ex1); } } return maxid; } public string add() { connection con = getcon(); try { preparedstatement pstmt = con.preparestatement( "insert into users values (?,?,?) "); pstmt.setstring(1,name); pstmt.setint(2,age); pstmt.setint(3,getmaxid()+1); pstmt.EXECuteupdate(); return "success "; } catch (sqlexception ex) { system.out.println(ex); return "fales "; } finally { try { con.close(); } catch (sqlexception ex1) { system.out.println(ex1); } } } public userbean() { } public void setname(string name) { this.name = name; } public void setage(int age) { this.age = age; } public void setid(int id) { this.id = id; } public string getname() { return name; } public int getage() { return age; } public int getid() { return id; } } ========================================== <?xml version= "1.0 " encoding= "utf-8 "?> <!doctype faces-config public "-//sun microsystems, inc.//dtd javaserver faces config 1.1//en " "http://java.sun.com/dtd/web-facesconfig_1_1.dtd "> <faces-config xmlns= "http://java.sun.com/jsf/configuration "> <managed-bean> <managed-bean-name> userbean </managed-bean-name> <managed-bean-class> jsforacle.userbean </managed-bean-class> <managed-bean-scope> session </managed-bean-scope> </managed-bean> <navigation-rule> <from-view-id> /reg.jsp </from-view-id> <navigation-case> <from-outcome> success </from-outcome> <to-view-id> /result.jsp </to-view-id> </navigation-case> </navigation-rule> <navigation-rule> <from-view-id> /reg.jsp </from-view-id> <navigation-case> <from-outcome> false </from-outcome> <to-view-id> /result2.jsp </to-view-id> </navigation-case> </navigation-rule> </faces-config> =========================================== <?xml version= "1.0 " encoding= "utf-8 "?> <web-app xmlns= "http://java.sun.com/xml/ns/j2ee " xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance " xsi:schemalocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd " version= "2.4 "> <display-name> webmodule1 </display-name> <servlet> <servlet-name> faces servlet </servlet-name> <servlet-class> javax.faces.webapp.facesservlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> faces servlet </servlet-name> <url-pattern> *.faces </url-pattern> </servlet-mapping> </web-app> | | |
|