当前位置: 代码迷 >> Java Web开发 >> [求助]谁有用JSF连数据库并可以 增删改查 功能?
  详细解决方案

[求助]谁有用JSF连数据库并可以 增删改查 功能?

热度:517   发布时间:2007-03-24 08:34:29.0
[求助]谁有用JSF连数据库并可以 增删改查 功能?
我的代码不健全,总是有点错误,希望大哥大姐帮帮我,给我点列子!
谢谢
搜索更多相关主题的帖子: JSF  数据库  列子  

----------------解决方案--------------------------------------------------------
JSF中的连接数据库也是用的JDBC或是别的数据持久层,和JSF没太大的关系?

你哪出错?
----------------解决方案--------------------------------------------------------
楼上的博客写得不错,赞一个
----------------解决方案--------------------------------------------------------

一个关于课题Subject的JAVABean:
package com.jsfdemo.beans;

public class SubjectBean {
private String sb_id;
private String sb_name;
private String sb_desc;
private String t_name;
private String sb_state;
//--------------------------
public SubjectBean(){

}
//--------------------------
public String getSb_desc() {
return sb_desc;
}
public void setSb_desc(String sb_desc) {
this.sb_desc = sb_desc;
}
public String getSb_id() {
return sb_id;
}
public void setSb_id(String sb_id) {
this.sb_id = sb_id;
}
public String getSb_name() {
return sb_name;
}
public void setSb_name(String sb_name) {
this.sb_name = sb_name;
}
public String getSb_state() {
return sb_state;
}
public void setSb_state(String sb_state) {
this.sb_state = sb_state;
}
public String getT_name() {
return t_name;
}
public void setT_name(String t_name) {
this.t_name = t_name;
}
public SubjectBean(String sb_id,String sb_name,String sb_desc,String t_name,String sb_state ){
this.sb_id = sb_id;
this.sb_name = sb_name;
this.sb_desc = sb_desc;
this.t_name = t_name;
this.sb_state = sb_state;

}
}


一个数据库连接池的类:
package com.jsfdemo.beans;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.*;
import javax.sql.DataSource;
public class DBConnection {

static public Connection dataSource() {
try{
Context ctx = new InitialContext();
if(ctx==null)throw new NamingException ("no initial context");
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/dbtest");
if(ds==null) throw new NamingException ("no dataSource ");
// Connection con = ds.getConnection();
}catch (NamingException e){

//}catch (SQLException e){

}
return null;
}
}

一个操作数据库的JAVABean:
package com.jsfdemo.beans;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Vector;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import javax.servlet.jsp.jstl.sql.Result;
import java.sql.SQLException;

public class SbDB {
// private Result AllSubject;
//********************************************
public void addSubject(SubjectBean subject) throws Exception,SQLException{
try{
Connection con =DBConnection.dataSource();
PreparedStatement pst = null;
//try{
pst = con.prepareStatement("insert into subjectInfo(sb_id,sb_name,sb_desc,t_id,sb_state) values(?,?,?,?,?)");
pst.setString(1, subject.getSb_id());
pst.setString(2, subject.getSb_name());
pst.setString(3, subject.getSb_desc());
pst.setString(4, subject.getT_name());
pst.setString(5, subject.getSb_state());
pst.executeUpdate();
//}catch(Exception e){
// e.printStackTrace();
//}
con.close();

}catch(Exception e){
e.printStackTrace();
}

}
}
// ********************************************

一个和JSP界面及JAVABean交互的类:
package com.jsfdemo.beans;
import javax.faces.event.ActionEvent;
import javax.servlet.jsp.jstl.sql.Result;


public class SbManager {

private SubjectBean subjectB = new SubjectBean();
private SbDB sbdb = new SbDB();
//*************************************

public SbManager(){

}
//*************************************

public SubjectBean getSubjectB() {
return subjectB;
}
public void setSubjectB(SubjectBean subjectB) {
this.subjectB = subjectB;
}
public SbDB getSbdb() {
return sbdb;
}

public void setSbdb(SbDB sbdb) {
this.sbdb = sbdb;
}
public String add(){
try{
sbdb.addSubject(subjectB);
}catch(Exception e){
e.printStackTrace();
}
return "addOK";
}

JSP界面:
<%@ page language = "java" contentType = "text/html;charset = gb2312"%>
<%@ taglib uri = "http://java.sun.com/jsf/core" prefix = "f"%>
<%@ taglib uri = "http://java.sun.com/jsf/html" prefix = "h"%>
<html>
<center>
<f:view>
<h:form>

<head>
<title>
add the subject
</title>
</head>
<body>
<h:panelGrid columns = "2">
<h:outputText value = "subjectID"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.sb_id}"></h:inputText>
<h:outputText value = "subjectName"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.sb_name}"></h:inputText>
<h:outputText value = "subjectDesc"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.sb_desc}"></h:inputText>
<h:outputText value = "teacher'sName"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.t_name}"></h:inputText>
<h:outputText value = "subject'sState"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.sb_state}"></h:inputText>
<h:commandButton value = "submit..." action = "#{SbManager.add}"></h:commandButton>
</h:panelGrid>
<h:dataTable value = "#{SbManager.subjectB}" var = "sb" border = "1">
<h:column>
<f:facet name="header">
<h:outputText value = "subjectID"></h:outputText>
</f:facet>
<h:outputText value = "#{sb.sb_id}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value = "subjectName"></h:outputText>
</f:facet>
<h:outputText value = "#{sb.sb_name}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value = "subjectDesc"></h:outputText>
</f:facet>
<h:outputText value = "#{sb.sb_desc}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value = "teacher'sName"></h:outputText>
</f:facet>
<h:outputText value = "#{sb.t_name}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value = "subject'sState"></h:outputText>
</f:facet>
<h:outputText value = "#{sb.sb_state}"></h:outputText>
</h:column>
</h:dataTable>

</body>
</h:form>
</f:view>
</center>
</html>

其他配置正确(因为我做了测试),我先是把这些代码全部写在一个类里,能实现其功能,但是那样感觉太乱了,所以我把这些代码给分别写在了类里,结果是:
提交后,不显示错误,也不往数据库里插入信息,我已经是到了“山穷水尽”的地步了,实在解决不了这个问题,希望高手帮帮忙,谢谢啊!!!


----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
  相关解决方案