Results 1 to 6 of 6
- 03-29-2011, 08:20 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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>
<input type="text" name="empid" />
</label>
<p>
<label>Emp_Name
<input type="text" name="ename" />
</label>
</p>
<p>
<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)
)
- 03-29-2011, 08:40 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 03-29-2011, 11:09 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
thank u
i follow your instruction
i am using Eclipse 3.4
tomcat 6.0
jdk1.6
- 03-29-2011, 11:52 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
What if you override doGet method as well
Java Code:public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ doPost(req,res); }Swastik
- 03-29-2011, 12:01 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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.
- 03-29-2011, 12:16 PM #6
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
Similar Threads
-
Getting problem inserting data in data base
By anupama in forum New To JavaReplies: 4Last Post: 12-15-2010, 10:03 PM -
data is not inserting into database
By gb.rashu in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 08-21-2010, 03:05 PM -
Problem regarding Inserting data into Mysql via JSP
By abhi118 in forum JDBCReplies: 5Last Post: 04-05-2010, 01:24 PM -
Inserting data containing quotes into DB
By Java Tip in forum Java TipReplies: 0Last Post: 02-06-2008, 09:28 AM -
Problem inserting values to MySQL tables from the Data Source Explorer
By tip in forum EclipseReplies: 0Last Post: 12-24-2007, 09:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks