一个连接数据库的JAVABEAN:
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 {
Connection con ;
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 ");
con = ds.getConnection();
}catch (NamingException e){
}catch (SQLException e){
}
return con;
}
}
一个课题SUBJECT的BEAN:
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;
}
}
一个操作数据库的Bean(删除功能):
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;
import java.sql.*;
public class SbDB {
DBConnection DBConnection = new DBConnection();
public void deleteSubject(String sbName)throws Exception{
try{
Connection con = DBConnection.dataSource();
Statement st = con.createStatement();
String deleteSql = "delete from subjectInfo where sb_id = "+sbName+"";
st.executeUpdate(deleteSql);
// pst =con.prepareStatement("delete from subjectInfo where sb_name=? ");
//pst.setString(1, sbName);
//pst.executeQuery();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
一个用与JSP和BEAN交互的类(删除功能):
package com.jsfdemo.beans;
import java.util.List;
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 delete(){
try{
sbdb.deleteSubject(subjectB.getSb_name());
}catch(Exception e){
e.printStackTrace();
}
return "deleteOK";
}
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>
<f:view>
<f:loadBundle basename = "com.jsfdemo.messages" var = "msgs"/>
<center>
<h:form>
<head>
<title>
delete subject form
</title>
</head>
<body>
<h:outputText value = "#{msgs.deleteSubject}"></h:outputText>
<h:outputText value = "#{msgs.subjectID}"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.sb_name}"></h:inputText>
<h:commandButton value = "delete...." action = "#{SbManager.delete}"></h:commandButton>
<h:outputText value = "#{SbManager.subjectB.sb_name}"></h:outputText>
</body>
</h:form>
</center>
</f:view>
</html>
我是做毕业设计,是个新手,如果谁有类似的代码可以给我发来看下吗?
谢谢啊。我的毕业设计是毕业生选题系统。有谁可以给我分享下??
[此贴子已经被作者于2007-5-7 14:56:19编辑过]
----------------解决方案--------------------------------------------------------
你先在你的delete方法中看一下是否执行了,还有就是你delete方法返回字符串对应的页面存在不
----------------解决方案--------------------------------------------------------
我改了下(是因为给传了错误的值),但是还是出现了这样的错误:请问这一般是什么错误???
javax.servlet.ServletException: Error testing property 'sb_name' in bean of type null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:209)
root cause
javax.faces.el.PropertyNotFoundException: Error testing property 'sb_name' in bean of type null
com.sun.faces.el.PropertyResolverImpl.getType(PropertyResolverImpl.java:342)
com.sun.faces.el.impl.ArraySuffix.getType(ArraySuffix.java:240)
com.sun.faces.el.impl.ComplexValue.getType(ComplexValue.java:208)
com.sun.faces.el.ValueBindingImpl.getType(ValueBindingImpl.java:338)
com.sun.faces.renderkit_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:111)
javax.faces.component.UIInput.getConvertedValue(UIInput.java:702)
javax.faces.component.UIInput.validate(UIInput.java:627)
javax.faces.component.UIInput.executeValidate(UIInput.java:838)
javax.faces.component.UIInput.processValidators(UIInput.java:412)
javax.faces.component.UIForm.processValidators(UIForm.java:170)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:904)
javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:342)
com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:78)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
----------------解决方案--------------------------------------------------------
类似这样写的好像不行SbManager.subjectB.sb_name
我也遇到过这样的问题,后来用继承解决了
建议改用继承吧
----------------解决方案--------------------------------------------------------