我正在尝试将数据添加到Oracle数据库表中。即使添加了数据,页面也不会刷新,但会显示以下错误: java.io.NotSerializableException - oracle.jdbc.driver.T4CConnection
这是我的java代码:
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
@ManagedBean(name="adder")
@ViewScoped
public class Add implements Serializable {
private String pno;
private String pfname;
private String psname;
private String city;
private String ppsno;
private String insurance;
private String contact;
private String mhistory;
private Connection con;
public String getPno() {
return pno;
}
public void setPno(String pno) {
this.pno = pno;
}
public String getPfname() {
return pfname;
}
public void setPfname(String pfname) {
this.pfname = pfname;
}
public String getPsname() {
return psname;
}
public void setPsname(String psname) {
this.psname = psname;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPpsno() {
return ppsno;
}
public void setPpsno(String ppsno) {
this.ppsno = ppsno;
}
public String getInsurance() {
return insurance;
}
public void setInsurance(String insurance) {
this.insurance = insurance;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getMhistory() {
return mhistory;
}
public void setMhistory(String mhistory) {
this.mhistory = mhistory;
}
private Connection getConnection(){
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String usr = "scott";
String pass = "Silver90$";
Connection cnn = null;
try {
Class.forName(driver);
cnn = DriverManager.getConnection(url,usr,pass);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cnn;
}
public void AddAction() throws SQLException
{
this.con=this.getConnection();
Statement st=null;
ResultSet rs = null;
String sql = "insert into PATIENT1 values(?,?,?,?,?,?,?,?)";
try {
st=con.createStatement();
rs = st.executeQuery(sql);
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, pno);
pst.setString(2, pfname);
pst.setString(3, psname);
pst.setString(4, city);
pst.setString(5, ppsno);
pst.setString(6, insurance);
pst.setString(7, contact);
pst.setString(8, mhistory);
pst.executeUpdate();
FacesMessage msg = new FacesMessage("Patient added successfully.", "Patient Info generated Successfully!");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
这是我的.xhtml代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:body>
<h1 align="center"> Add the Patient's Details over here:</h1>
<h:form>
<h:outputLabel>Patient No: </h:outputLabel>
<h:inputText id="pno" value="#{adder.pno}" size="20" required="true"
label ="Pno">
</h:inputText>
<h:message for="pno" style="color:red"/>
<br/>
<br/>
<h:outputLabel>First Name: </h:outputLabel>
<h:inputText id="pfname" value="#{adder.pfname}" size="20" required="false"
label ="Pfname">
</h:inputText>
<h:message for="pfname" style="color:red"/>
<br/>
<br/>
<h:outputLabel>Last Name: </h:outputLabel>
<h:inputText id="psname" value="#{adder.psname}" size="20" required="true"
label ="Psname">
</h:inputText>
<h:message for="psname" style="color:red"/>
<br/>
<br/>
<h:outputLabel>City: </h:outputLabel>
<h:inputText id="city" value="#{adder.city}" size="20" required="true"
label ="City">
</h:inputText>
<h:message for="city" style="color:red"/>
<br/>
<br/>
<h:outputLabel>PPS No.: </h:outputLabel>
<h:inputText id="ppsno" value="#{adder.ppsno}" size="20" required="true"
label ="Ppsno">
</h:inputText>
<h:message for="ppsno" style="color:red"/>
<br/>
<br/>
<h:outputLabel>Insurance: </h:outputLabel>
<h:inputText id="insurance" value="#{adder.insurance}" size="20" required="false"
label ="Insurance">
</h:inputText>
<h:message for="insurance" style="color:red"/>
<br/>
<br/>
<h:outputLabel>Contact: </h:outputLabel>
<h:inputText id="contact" value="#{adder.contact}" size="20" required="true"
label ="Contact">
</h:inputText>
<h:message for="contact" style="color:red"/>
<br/>
<br/>
<h:outputLabel>Medical History: </h:outputLabel>
<h:inputText id="mhistory" value="#{adder.mhistory}" size="20" required="false"
label ="Mhistory">
</h:inputText>
<h:message for="mhistory" style="color:red"/>
<br/>
<br/>
<h:commandButton value="Submit" action="#{adder.AddAction}"/>
<br></br>
<h:commandButton value="Reception Home Page" action="reception2"/>
<br></br>
<h:commandButton value="Logout" action="login2"/>
</h:form>
</h:body>
</html>
感谢您的帮助。
答案 0 :(得分:0)
问题是底层代码正在尝试序列化Add对象。但由于其中一个属性(Connection)不是Serializable,因此会抛出异常。其中一种方法是将Connection声明为瞬态。但我建议重构代码并从类中获取与jdbc相关的逻辑