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

    Default insert data to PostgreSQL database using Jsp and Servlets

    hi experts

    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
    I am using Eclipse 3.4, Tomcat6.0,jdk1.6
    my code is
    //ServletUser.java
    Java Code:
    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 = "jdbcostgresql://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

    Java Code:
    <%@ 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
    Java Code:
    <?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>
    Java Code:
    Database name: postgres
    
    CREATE TABLE sample
    (
    username character(20),
    pass character(15)
    )

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

    Default

    Swastik

  3. #3
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    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. how to insert data in database
    By Vinay Bhatia in forum JDBC
    Replies: 6
    Last Post: 11-03-2010, 05:06 PM
  2. why i can't insert data to database
    By khaa_khay in forum Web Frameworks
    Replies: 0
    Last Post: 05-26-2010, 06:37 AM
  3. java insert data into database problem
    By chyeeqi in forum NetBeans
    Replies: 3
    Last Post: 03-29-2010, 02:36 AM
  4. Replies: 3
    Last Post: 03-19-2010, 01:59 AM
  5. Replies: 3
    Last Post: 02-28-2009, 10:17 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
  •