Results 1 to 6 of 6
  1. #1
    athi is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Smile problem with inserting data using Jsp and Servlet

    hi friends

    I am athi.
    I created a jsp page and trying to connect the database using Servlets and trying to insert the values to PostgreSQL8.3

    I got an error msg

    HTTP Status 405 - HTTP method GET is not supported by this URL

    Please help me

    //ServletUser.java
    package data;

    import java.io.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class ServletUser extends HttpServlet{

    public void init(ServletConfig config) throws ServletException{
    super.init(config);
    }
    /**Process the HTTP Get request*/

    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException{

    String connectionURL = "jdbc:postgresql://localhost:5432/postgres";
    Connection connection = null;
    ResultSet rs;
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    System.out.println("PostGreSql Connect Example.");
    //get the variables entered in the form
    String username = req.getParameter("empid");
    String pass = req.getParameter("ename");

    try {
    // Load the database driver
    Class.forName("org.postgresql.Driver");
    // Get a Connection to the database
    connection = DriverManager.getConnection(connectionURL, "postgres", "admin");
    //Add the data into the database

    //String sql = "insert into sample values(?,?)";

    PreparedStatement pst = connection.prepareStatement("INSERT into sample VALUES(?,?)");

    pst.setString(1,username);
    pst.setString(2,pass);

    int numRowsChanged = pst.executeUpdate();
    if(numRowsChanged!=0){
    out.println("<br>Record has been inserted");
    }
    else{
    out.println("failed to insert the data");
    }
    pst.close();
    }
    catch(ClassNotFoundException e){
    out.println("Couldn't load database driver: " + e.getMessage());
    }
    catch(SQLException e){
    out.println("SQLException caught: " + e.getMessage());
    }
    catch (Exception e){
    out.println(e);
    }
    finally {
    // Always close the database connection.
    try {
    if (connection != null) connection.close();
    }
    catch (SQLException ignored){
    out.println(ignored);
    }
    }

    }
    }
    --------------------------------------------------------------------------

    //AddForm.jsp

    <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Add Data To DataBase</title>
    </head>

    <body>
    <form name="TestForm" method="post" action="/ServletUser">
    <label>
    <label>Emp_Id</label>&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="text" name="empid" />
    </label>
    <p>
    <label>Emp_Name
    <input type="text" name="ename" />
    </label>
    </p>
    <p>
    &nbsp;&nbsp;&nbsp;<input type="submit" name="Submit" value="Submit" /> </p>
    </form>
    </body>
    </html>
    --------------------------------------------------------------------------

    //web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>
    AddDataBase</display-name>
    <servlet>
    <servlet-name>ServletUser</servlet-name>
    <servlet-class>data.ServletUser</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>ServletUser</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
    <welcome-file>AddForm.jsp</welcome-file>
    </welcome-file-list>

    </web-app>
    -----------------------------------------------------
    //Database name: postgres

    CREATE TABLE sample
    (
    username character(20),
    pass character(15)
    )

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    First off, when posting code can you use code tags.
    Unformatted code is hard to read.

    What URL does it think you are using?
    Can you use something like Firebug (for Firefox) to see what is being sent to the server, because it thinks you are doing a GET.

  3. #3
    athi is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    thank u
    i follow your instruction

    i am using Eclipse 3.4
    tomcat 6.0
    jdk1.6

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    What if you override doGet method as well

    Java Code:
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException{
    doPost(req,res);
    }
    Swastik

  5. #5
    athi is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    first i say thank u...

    i add this code to my servlet file.
    i getting an output only "Record has been inserted"
    and table data is null value.

  6. #6
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    I haven't worked on this particular database, but there might be some autocommit feature, which is causing the problem. Enable the autocommit feature from your code, and see if it helps



    Java Code:
    connection.setAutoCommit(true);
    Swastik

Similar Threads

  1. Getting problem inserting data in data base
    By anupama in forum New To Java
    Replies: 4
    Last Post: 12-15-2010, 11:03 PM
  2. data is not inserting into database
    By gb.rashu in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 08-21-2010, 04:05 PM
  3. Replies: 5
    Last Post: 04-05-2010, 02:24 PM
  4. Inserting data containing quotes into DB
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-06-2008, 10:28 AM
  5. Replies: 0
    Last Post: 12-24-2007, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •