JSF:如何写数据库? [英] JSF: How to write to database?

查看:88
本文介绍了JSF:如何写数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSF的初学者,但我似乎做不到这一点.我正在尝试接受用户输入的表单,然后在用户单击提交"时将其插入数据库.因此,我在前端仅拥有一个表单和一个包含在表单标签中的提交按钮:

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>

FormBean:

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

public String getJobTitle() {
    return jobTitle;

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

数据库助手:

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();
        }
    }
}

}

我还配置了context.xml:

I've also configured context.xml:

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

我想这不是数据库连接问题,因为我已经测试过了.将不胜感激. JOBPROFILE表已经在db中创建,托管bean在请求范围中注册.我正在尝试遵循此示例-> http://docs.aptana.com/docs/index.php/Creating_a_JSF_Application

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

我现在遇到此错误:java.io.NotSerializableException:org.apache.tomcat.dbcp.dbcp.BasicDataSource

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

推荐答案

看来,您的bean中的某个位置(或其依赖项)有一个BasicDataSource作为成员.由于某些原因,JSF尝试序列化bean,但是数据源不可序列化.最快的解决方案是将其标记为transient.即

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;

这篇关于JSF:如何写数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆