简体   繁体   中英

java.sql.SQLException: Io exception: Got minus one from a read call during JDBC connection with oracle 11g

Hi I am trying to run a servlet which gets the data from a simple html form and stores in oracle database and displays a message "Data Saved". I am using Eclipse Luna, Tomcat 7.0 and Oracle 11g Express Edition. When I run the the program I get this error.

java.sql.SQLException: Io exception: Got minus one from a read call during JDBC connection with oracle 11g

Here is my web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StudentManagement</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>StudentRegServlet</servlet-name>
    <servlet-class>com.serv.pkg.StudentRegServlet</servlet-class>
    <init-param>
      <description></description>
      <param-name>username</param-name>
      <param-value>scott</param-value>
    </init-param>
    <init-param>
      <param-name>password</param-name>
      <param-value>tiger</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>StudentRegServlet</servlet-name>
    <url-pattern>/stdreg</url-pattern>
  </servlet-mapping>

  <servlet>
  <servlet-name>CourseRegServlet</servlet-name>
  <servlet-class>com.serv.pkg.CourseRegServlet</servlet-class>
  <init-param>
  <param-name>username</param-name>
  <param-value>test</param-value>
   </init-param>

   <init-param>
   <param-name>password</param-name>
   <param-value>test</param-value>
   </init-param>

  </servlet>

  <servlet-mapping>
  <servlet-name>CourseRegServlet</servlet-name>
  <url-pattern>/coursereg</url-pattern>
  </servlet-mapping>

  <context-param>
  <param-name>driver</param-name>
  <param-value>oracle.jdbc.OracleDriver</param-value>
  </context-param>

  <context-param>
  <param-name>url</param-name>
  <param-value>jdbc:oracle:thin:@localhost:8080:XE</param-value>
  </context-param>
</web-app>

My Servlet

package com.serv.pkg;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

/**
 * Servlet implementation class CourseRegServlet
 */
@WebServlet("/CourseRegServlet")
public class CourseRegServlet extends GenericServlet {
    private Connection conn; 
    @Override
    public void init(ServletConfig config)throws ServletException
    {
        System.out.println("Executing init method from coursereg.html");
    ServletContext sc = config.getServletContext( );
    String driver=sc.getInitParameter("driver");
    String url=sc.getInitParameter("url");
    String username=config.getInitParameter("username");
    String password=config.getInitParameter("password");

    try {
        Class.forName(driver);
        System.out.println("driver loaded");
        conn=DriverManager.getConnection(url, username, password );
    } catch ( Exception e) {
        System.out.println("catching from init" +e);
    }

    }


    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        System.out.println("Executing service method from coursereg.html");
          int cid = Integer.parseInt(request.getParameter("cid"));
          String c_name = request.getParameter("c_name");
          int fee = Integer.parseInt(request.getParameter("fee"));

          try {
              System.out.println("exe1");
            PreparedStatement stmt=conn.prepareStatement("insert into course values(?,?,?)");
            System.out.println("exe2");
            stmt.setInt(1, cid);
            stmt.setString(2, c_name);
            stmt.setInt(3, fee);
            int i = stmt.executeUpdate();
            if(i!=0){
                System.out.println("course registered");
            }else
            {
                System.out.println("registration failed");
            }
        } catch ( Exception e) {
            System.out.println(e);
        }
          out.println("Data saved");
    }

    public void destroy(){
        try {
            System.out.println("Executing destroy method from coursereg.html");
            conn.close();
        } catch ( Exception e) {
            System.out.println(e);
        }
    }

}

The default port for the database listener is 1521, not 8080 (which is the default HTTP port, from XML DB); so your URL should be:

  <param-value>jdbc:oracle:thin:@localhost:1521:XE</param-value>

If your listener is on a non-standard port then use that instead. Also the :XE part means it'll try to connect with the SID XE. You might prefer to use the service name, which is hopefully also XE, which would be denoted by a slash instead of a colon:

  <param-value>jdbc:oracle:thin:@localhost:1521/XE</param-value>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM