簡體   English   中英

java.io.NotSerializableException-oracle.jdbc.driver.T4CConnection

[英]java.io.NotSerializableException - oracle.jdbc.driver.T4CConnection

我正在嘗試將數據添加到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> 

謝謝您的幫助。

問題在於底層代碼正在嘗試序列化Add對象。 但是,由於其中一個屬性(連接)不可序列化,因此會引發異常。 一種方法是將Connection聲明為瞬態。 但是我建議重構代碼,並從類中刪除與jdbc相關的邏輯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM