一个连接数据库的类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;
}
}
一个关于老师的类JAVABean:
package com.jsfdemo.beans;
public class TeacherBean {
//---------------------attribute----------------------------
private String t_id;
private String t_name;
private String t_sex;
private String t_post ;
private String t_telnumber;
private String t_email;
private String t_pswd;
//*******************************************
public TeacherBean(){
}
//---------------------geter and setter method------------
public String getT_email() {
return t_email;
}
public void setT_email(String t_email) {
this.t_email = t_email;
}
public String getT_id() {
return t_id;
}
public void setT_id(String t_id) {
this.t_id = t_id;
}
public String getT_name() {
return t_name;
}
public void setT_name(String t_name) {
this.t_name = t_name;
}
public String getT_post() {
return t_post;
}
public void setT_post(String t_post) {
this.t_post = t_post;
}
public String getT_pswd() {
return t_pswd;
}
public void setT_pswd(String t_pswd) {
this.t_pswd = t_pswd;
}
public String getT_sex() {
return t_sex;
}
public void setT_sex(String t_sex) {
this.t_sex = t_sex;
}
public String getT_telnumber() {
return t_telnumber;
}
public void setT_telnumber(String t_telnumber) {
this.t_telnumber = t_telnumber;
}
}
一个操作数据库的类JAVABean:
package com.jsfdemo.beans;
import java.sql.*;
public class TeacherDB {
DBConnection DBConnection = new DBConnection();
public void addTeacher(TeacherBean teacher){
try{
Connection con = DBConnection.dataSource();
PreparedStatement pst = null;
try{
pst = con.prepareStatement("insert into teacherInfo(t_id,t_name,t_sex,t_post,t_telnumber,t_email,t_pswd) values(?,?,?,?,?,?,?)");
pst.setString(1, teacher.getT_id());
pst.setString(2, teacher.getT_name());
pst.setString(3, teacher.getT_sex());
pst.setString(4, teacher.getT_post());
pst.setString(5, teacher.getT_telnumber());
pst.setString(6, teacher.getT_email());
pst.setString(7, teacher.getT_pswd());
pst.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
一个和JAVABean及JSP页面交互的Bean:
package com.jsfdemo.beans;
public class TeacherManager {
TeacherBean teacherB = new TeacherBean();
TeacherDB teacherDB = new TeacherDB();
//*******************************************
public TeacherManager(){
}
//*************************************
public TeacherBean getTeacherB() {
return teacherB;
}
public void setTeacherB(TeacherBean teacherB) {
this.teacherB = teacherB;
}
public TeacherDB getTeacherDB() {
return teacherDB;
}
public void setTeacherDB(TeacherDB teacherDB) {
this.teacherDB = teacherDB;
}
//****************************************************
public String add(){
try{
teacherDB.addTeacher(teacherB);
}catch(Exception e){
e.printStackTrace();
}
return "addOK";
}
}
JSP页面:
<%@ page 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>
<head>
<title>
<h:outputText value = "add teacher information"></h:outputText></title>
</head>
<body>
<h:outputText value = "please input the teacher's information"></h:outputText>
<h:panelGrid columns = "2">
<h:outputText value = "teacherID"></h:outputText>
<h:inputText value = "#{teacherManager.teacherB.t_id}"></h:inputText>
<h:outputText value = "teacherName"></h:outputText>
<h:inputText value = "#{teacherManager.teacherB.t_name}"></h:inputText>
<h:outputText value = "teacherSex"></h:outputText>
<h:inputText value = "#{teacherManager.teacherB.t_sex}"></h:inputText>
<h:outputText value = "teacherPost"></h:outputText>
<h:inputText value = "#{teacherManager.teacherB.t_post}"></h:inputText>
<h:outputText value = "teacherTelNumber"></h:outputText>
<h:inputText value = "#{teacherManager.teacherB.t_telnumber}"></h:inputText>
<h:outputText value = "teacherEmail"></h:outputText>
<h:inputText value = "#{teacherManager.teacherB.t_email}"></h:inputText>
<h:outputText value = "teacherPwd"></h:outputText>
<h:inputText value = "#{teacherManager.teacherB.t_pswd}"></h:inputText>
</h:panelGrid>
<h:commandButton value = "addTeacher" action = "#{teacherManager.add}"></h:commandButton>
<h:commandButton value = "Reset" type = "reset"></h:commandButton>
<h:outputText value = "#{teacherManager.teacherB.t_name}"></h:outputText>
</body>
</f:view>
</center>
</html>
问题求解:
点击“addTeacher”没有任何反映,不知道是什么错误,哪为高人帮帮我。
谁有类似的课题代码能给我参考下吗?
把你的代码发来参考下,有增、删、改、还有用DataTable显示的最好,谢谢
我做的是毕业设计,课题是毕业生选题系统。
[此贴子已经被作者于2007-5-7 14:50:24编辑过]