Monday, 26 September 2011

Insert the data into a database

import java.io.*;
import javax.servlet.http.*;
import java.sql.*;
public class Update extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String url=" ";
Connection cont;
try
{
url="jdbc:odbc:AddressBook1";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cont=DriverManager.getConnection(url);
Statement statement=cont.createStatement();
String query="INSERT INTO addresses Values ( '"+ request.getParameter ("number") + "','"+
request.getParameter("username")+"','"+request.getParameter("password")+"','"+ request.getParameter("email")+"','"+ request.getParameter("phone")+"')";
int result = statement.executeUpdate(query);
if (result == 1)
pw.println("Data was Inserted");
else
pw.println ("Data was not Inserted");
statement.close();
cont.close();
}
catch(Exception e)
{
pw.println(e.toString());
}
}
}

No comments:

Post a Comment