开发者

JSF: How to write to database?

开发者 https://www.devze.com 2023-01-13 02:29 出处:网络
I\'m a beginner to JSF, and I can\'t seem to get this right. I\'m trying to take a user input in a form and insert it into a database when the users clicks submit. So what I have at at front end is ju

I'm a beginner to JSF, and I can't seem to get this right. I'm trying to take a user input in a form and insert it into a database when the users clicks submit. So what I have at at front end is just a form and a submit button enclosed in form tags:

<h:form>
    <h:panelGrid columns="2" border="1">
                <h:outputLabel value="Job Title: " />
                <h:inputTextarea rows="1" cols="65" 
                    id="jobTitle" 
                    value="#{jdFormBean.jobTitle}" />
    </h:panelGrid>
    <h:commandButton type="submit" 
                     value="Add Job Profile" 
                     action="#{formBean.addJobProfile}" /> 
</h:form>

The FormBean:

private String jobTitle;
public void setJobTitle(String jobTitle) {
    this.jobTitle = jobTitle;
}

public String getJobTitle() {
    return jobTitle;

public void addJobProfile() {
    DatabaseHelper.populateDatabase(this.getJobTitle());

The DatabaseHelper:

public class DatabaseHelper {

private static Connection createConnection() throws NamingException,
        SQLException {
    Context ctx = new InitialContext();
    if (ctx == null) {
        throw new NamingException("No initial context");
    }

    Context envContext = (Context) ctx.lookup("java:/comp/env");
    if (envContext == null) {
        throw new NamingException("No environment context");
    }

    DataSource ds = (DataSource) envContext.lookup("jdbc/ecr");
    if (ds == null) {
        throw new NamingException("No data source");
    }

    Connection conn = ds.getConnection();
    if (conn == null) {
        throw new SQLException("No database connection");
    }
    return conn;
}

public static void populateDatabase(String jt) {
    Connection connection;

    try {
        connection = createConnection();
        String insertSQL = "INSERT INTO JOBPROFILE(JOBTITLE) VALUES (' " + jt + "' )"; 
        executeUpdate(connection, insertSQL);

    }
    catch (SQLException sqle) {
        sqle.printStackTrace();
    }
    catch (NamingException ne) {
        ne.printStackTrace();
    }
}

    private static void executeUpdate(Connection connection, String sql) {
    Statement stmt = null;
    try {
        stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        stmt.setQueryTimeout(30);
        stmt.executeUpdate(sql);
    } catch (SQLException exc) {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

I've also configured context.xml:

    <Resource name="jdbc/ecr" auth="Container"
        type="javax.sql.DataSource" username="ecr" password="*****开发者_JS百科**"
        driverClassName="oracle.jdbc.OracleDriver"                 
        url="jdbc:oracle:thin:@//host:port/ecrdev"
        maxActive="20" maxIdle="10"/>

I suppose it's not a database connection problem as I've already tested that. Would appreciate any help. JOBPROFILE table is already created in db, managed bean registered in request scope. I'm trying to follow this example -> http://docs.aptana.com/docs/index.php/Creating_a_JSF_Application

I'm facing this error now: java.io.NotSerializableException:org.apache.tomcat.dbcp.dbcp.BasicDataSource


It appears that somewhere in your bean (or its dependencies) you have a BasicDataSource as a member. For some reason JSF attempts to serialize your bean, but the data source is not Serializable. The quickest solution would be to mark it as transient. I.e.

private transient BasicDataSource dataSource;
0

精彩评论

暂无评论...
验证码 换一张
取 消