如何在我的表中插入数据?

发布于 2024-12-06 01:54:57 字数 4663 浏览 1 评论 0原文

我创建了一个 login.jsp 页面...在此页面提交按钮底部添加新用户链接就在那里。如果我单击该链接,则会打开新用户注册表单。我已经在oracle中创建表,如果我提供想要存储在数据库中的数据,但它指示空指针异常.....newuser_db.java中的第92行我的数据库连接编码位于newuser_db.java中。请帮我...... 错误:第 92 行是 stmt1 = con.prepareStatement("插入 userdetails 值(ka1.nextval,?,?,?,?,?,?,?,23,'as','14-nov-87 ')");

设计: 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

完整代码:

package com.sla;

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


import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class newuser_db
 */
public class newuser_db extends HttpServlet {
    private static final long serialVersionUID = 1L;
    PreparedStatement stmt=null;
    Connection con =null;
    ResultSet rs=null;


    /**
     * @see HttpServlet#HttpServlet()
     */
    public newuser_db() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public void init() throws ServletException {
        // TODO Auto-generated method stub
        super.init();
        try
        {
            Class.forName("sun.Jdbc.Odbc.JdbcOdbcDriver");
        }
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }


        try
        {
            con =DriverManager.getConnection("Jdbc:odbc:servletdb","system","balaji");

        }
        catch(SQLException ex)
        {
            ex.printStackTrace();
        }



        // TODO Auto-generated method stub
    }

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.service(arg0, arg1);
        PrintWriter out=arg1.getWriter();
        String s1=arg0.getParameter("USERID");
        String s2=arg0.getParameter("FIRSTNAME");
        String s3=arg0.getParameter("LASTNAME");
        String s4=arg0.getParameter("EMAILID");
        String s5=arg0.getParameter("ORGNAME");
        String s6=arg0.getParameter("MOBILENO");
        String s7=arg0.getParameter("PHONENO");
        String s8=arg0.getParameter("PASSWORD");

        out.println("inserted");
        out.println(s1);




        out.println("inserted1");
        //String sql="insert into userdetails values(ka.nextval,"+s1+","+s2+","+s3+","+s4+","+s5+","+s6+","+s7+",56,'de',01-jan-98)";
        PreparedStatement stmt1;
        try {
            stmt1 = con.prepareStatement("insert into userdetails values(ka1.nextval,?,?,?,?,?,?,?,23,'as','14-nov-87')");
        stmt1.setString(1,s1);
            stmt1.setString(2,s2);
            stmt1.setString(3,s3);
            stmt1.setString(4,s4);
            stmt1.setString(5,s5);
            stmt1.setString(6,s6);
            stmt1.setString(7,s7);
            stmt1.setString(8,s8);


            //stmt1.executeUpdate();
            out.println("inserted2");
            stmt1.executeUpdate();
            out.println("inserted3");
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
        out.println(e1);

        }


    }
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        super.destroy();
        try
        {
            if(stmt!=null)
                stmt.close();
            stmt=null;

        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        try
        {
            if(con!=null)
                con.close();
            con=null;

        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }



    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

请帮助我任何人......我很困惑......

I created one login.jsp page...in this page submit button bottom add new user link is there. if i click that link its open new user registration form. i am already create table in oracle, if i give data i want to store in database but its indicate null pointer exception.....line 92 from newuser_db.java my database connection coding is there in newuser_db.java. please help me......
error: line 92 is stmt1 = con.prepareStatement("insert into userdetails values(ka1.nextval,?,?,?,?,?,?,?,23,'as','14-nov-87')");

design:
enter image description here
enter image description here
enter image description here

full code:

package com.sla;

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


import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class newuser_db
 */
public class newuser_db extends HttpServlet {
    private static final long serialVersionUID = 1L;
    PreparedStatement stmt=null;
    Connection con =null;
    ResultSet rs=null;


    /**
     * @see HttpServlet#HttpServlet()
     */
    public newuser_db() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public void init() throws ServletException {
        // TODO Auto-generated method stub
        super.init();
        try
        {
            Class.forName("sun.Jdbc.Odbc.JdbcOdbcDriver");
        }
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }


        try
        {
            con =DriverManager.getConnection("Jdbc:odbc:servletdb","system","balaji");

        }
        catch(SQLException ex)
        {
            ex.printStackTrace();
        }



        // TODO Auto-generated method stub
    }

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.service(arg0, arg1);
        PrintWriter out=arg1.getWriter();
        String s1=arg0.getParameter("USERID");
        String s2=arg0.getParameter("FIRSTNAME");
        String s3=arg0.getParameter("LASTNAME");
        String s4=arg0.getParameter("EMAILID");
        String s5=arg0.getParameter("ORGNAME");
        String s6=arg0.getParameter("MOBILENO");
        String s7=arg0.getParameter("PHONENO");
        String s8=arg0.getParameter("PASSWORD");

        out.println("inserted");
        out.println(s1);




        out.println("inserted1");
        //String sql="insert into userdetails values(ka.nextval,"+s1+","+s2+","+s3+","+s4+","+s5+","+s6+","+s7+",56,'de',01-jan-98)";
        PreparedStatement stmt1;
        try {
            stmt1 = con.prepareStatement("insert into userdetails values(ka1.nextval,?,?,?,?,?,?,?,23,'as','14-nov-87')");
        stmt1.setString(1,s1);
            stmt1.setString(2,s2);
            stmt1.setString(3,s3);
            stmt1.setString(4,s4);
            stmt1.setString(5,s5);
            stmt1.setString(6,s6);
            stmt1.setString(7,s7);
            stmt1.setString(8,s8);


            //stmt1.executeUpdate();
            out.println("inserted2");
            stmt1.executeUpdate();
            out.println("inserted3");
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
        out.println(e1);

        }


    }
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        super.destroy();
        try
        {
            if(stmt!=null)
                stmt.close();
            stmt=null;

        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        try
        {
            if(con!=null)
                con.close();
            con=null;

        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }



    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

please help me anyone...i am confused.......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-12-13 01:54:57

看起来您已经遵循了 RoseIndia.net 上的一个可怕的教程。我建议阅读一个更好的 JDBC 教程网站。

您的代码可能会出现很多问题。我建议向您团队中知识渊博的人寻求帮助;互联网论坛只能帮助您这么多。

  • PreparedStatementResultSetConnection 被声明为 servlet 的类成员。这是糟糕的编码实践,因为这些成员现在将在多个请求之间共享,并且任何请求都能够更改这些成员的状态,从而导致其他请求的行为模糊且令人困惑。
  • sun.Jdbc.Odbc.JdbcOdbcDriver 用于连接到 Oracle 10g Express 版本实例。对于 JDBC 驱动程序来说,这是一个糟糕的选择,因为它是类型 1 驱动程序,并且适合在没有适用于数据库的实际 JDBC 驱动程序时使用。请改用 Oracle 10g/11g JDBC 驱动程序。
  • 您在 init 中捕获诸如 ClassNotFoundExceptionSQLException 之类的异常,并在不中止执行的情况下打印它们的堆栈跟踪。在这种情况下,容器将忽略抛出的任何异常,因为您打印了它们的堆栈跟踪。因此,servlet 将被初始化,但考虑到异常的性质,它完全无法处理请求。
  • 您正在重写 service 并且没有为 doGetdoPost 提供方法体。这又是一个糟糕的做法。不要覆盖 service,除非你有充分的理由(而且我很确定你没有理由)。

话虽如此,您在 service 方法中最有可能收到 NullPointerException 的原因可能是因为 init< 的这一行引发了异常。 /code> 方法:

con =DriverManager.getConnection("Jdbc:odbc:servletdb","system","balaji");

由于您捕获了该行引发的任何异常,并且没有对此执行任何操作,因此您会发现在此类事件中 con 变量将为 null。稍后,当 servlet 处理请求并尝试创建 PreparedStatement 实例时,它将失败,因为 Connection 实例为 null。

This looks like you've followed one of the horrible tutorials at RoseIndia.net. I would suggest reading a better site for JDBC tutorials.

There are so many things that could go wrong with your code. I would suggest that get help from someone knowledgeable in your team; an internet forum can only help you so much.

  • PreparedStatement, ResultSet and Connection are declared as class members of the servlet. This is poor coding practice, as these members will now be shared across multiple requests, and any request would be capable of changing the state of these members resulting in vague and confusing behavior for other requests.
  • sun.Jdbc.Odbc.JdbcOdbcDriver is being used to connect to an Oracle 10g Express edition instance. This is a poor choice for a JDBC driver, considering that it is a type 1 driver and meant to be used when you do not have actual JDBC drivers for the database. Use the Oracle 10g/11g JDBC drivers instead.
  • You are catching exceptions like ClassNotFoundException and SQLException in init, and printing their stack trace without aborting execution. In this case, any exceptions thrown will be ignored by the container because you printed their stacktrace. So the servlet will be initialized, but it is perfectly incapable of processing requests considering the nature of the exceptions.
  • You are overriding service and not providing method bodies for doGet and doPost. This is again a poor practice. Do not override service, unless you have a good reason to (and I'm pretty sure you don't have one).

Having said all that, the reason you're most likely getting a NullPointerException in your service method is probably because an exception was thrown in this line of the init method:

con =DriverManager.getConnection("Jdbc:odbc:servletdb","system","balaji");

Since you are catching any exception thrown by this line, and not doing anything about it, you'll find that the con variable will be null in such an event. Later, when the servlet processes a request, and attempts to create a PreparedStatement instance, it will fail, as the Connection instance is null.

苦行僧 2024-12-13 01:54:57

如果您在第 92 行收到 NullPointerException,则似乎 con 为 null。您的代码是否调用了init?我注意到它不是从构造函数中调用的。

If you are getting a NullPointerException at line 92, seems that con is null. Did your code call init? I notice that it is not called from the constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文