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