一个连接数据库的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编辑过]