简体   繁体   English

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

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

I am trying to add data to a Oracle Database table. 我正在尝试将数据添加到Oracle数据库表。 Even though the data gets added, the page does not refresh but shows the following error: java.io.NotSerializableException - oracle.jdbc.driver.T4CConnection 即使添加了数据,页面也不会刷新,但是显示以下错误: java.io.NotSerializableException-oracle.jdbc.driver.T4CConnection

Here's my java code: 这是我的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);
        }





}

Here's my .xhtml code: 这是我的.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> 

Thanks for the help. 谢谢您的帮助。

The problem is that underlying code is trying to serialize Add object. 问题在于底层代码正在尝试序列化Add对象。 But because one of the properties (Connection) is not Serializable, it throws an exception. 但是,由于其中一个属性(连接)不可序列化,因此会引发异常。 One of the ways is to declare Connection as transient. 一种方法是将Connection声明为瞬态。 But I would advise to refactor the code and take jdbc related logic out of the class 但是我建议重构代码,并从类中删除与jdbc相关的逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在oracle.jdbc.driver.T4CConnection上找到锁定的对象 - Locked object found on oracle.jdbc.driver.T4CConnection java.lang.ClassCastException:oracle.jdbc.driver.T4CConnection无法转换为com.arjuna.ats.internal.arjuna.recovery.Connection - java.lang.ClassCastException: oracle.jdbc.driver.T4CConnection cannot be cast to com.arjuna.ats.internal.arjuna.recovery.Connection 使用解包的oracle.jdbc.driver.T4CConnection的HikariCP连接泄漏 - HikariCP Connection Leak using unwrapped oracle.jdbc.driver.T4CConnection java.io.NotSerializableException - java.io.NotSerializableException java.io.NotSerializableException:org.postgresql.jdbc4.Jdbc4Connection - java.io.NotSerializableException: org.postgresql.jdbc4.Jdbc4Connection 无法将JAVA / Oracle-weblogic.jdbc.wrapper.poolconnection_oracle_jdbc_driver_t4cconnection强制转换为oracle.jdbc.driver.oracleconnection - JAVA/Oracle-weblogic.jdbc.wrapper.poolconnection_oracle_jdbc_driver_t4cconnection cannot be cast to oracle.jdbc.driver.oracleconnection java.io.NotSerializableException:com.mysql.jdbc.DatabaseMetaData - java.io.NotSerializableException: com.mysql.jdbc.DatabaseMetaData 错误:java.io.NotSerializableException - An error: java.io.NotSerializableException java.io.NotSerializableException:ArtikelController - java.io.NotSerializableException: ArtikelController 带有数据类型的java.io.NotSerializableException - java.io.NotSerializableException With datatype
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM