
import java.io.*; import java.lang.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*;
public class ServletInsertingDataUsingHtml extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:odbc:shweta";
Connection connection;
try{
String username = request.getParameter("username");
String password = request.getParameter("password");
pw.println(username);
pw.println(password);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(connectionURL);
PreparedStatement pst = connection.prepareStatement("insert into emp_info values(?,?)");
pst.setString(1,username);
pst.setString(2,password);
int i = pst.executeUpdate();
if(i!=0){
pw.println("
Record has been inserted");
}
else{
pw.println("failed to insert the data");
}
}
catch (Exception e){
pw.println(e);
}
}
**//where my dsn name is shweta,database name is db1 and table name is emp_info.
i am getting 404 error that is requested resource not available. is there a error in database connectivity or in my program logic?please let me know as soon as possible**

Hi Friend,
Follow these steps:
1)Go to the start->Control Panel->Administrative Tools-> data sources.
2)Click Add button and select the driver Microsoft Access Driver(*.mdb).
3)After selecting the driver, click finish button.
4)Then give Data Source Name and click ok button.
5)Your DSN will get created.
6) Compile your servlet and restart the server and run your servlet.
Here is your required servlet code:
import java.io.*;
import java.lang.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletInsertingDataUsingHtml extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:odbc:student";
Connection connection;
try{ String username = request.getParameter("username");
String password = request.getParameter("password");
pw.println(username);
pw.println(password);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(connectionURL);
PreparedStatement pst = connection.prepareStatement("insert into login(username,password) values(?,?)");
pst.setString(1,username);
pst.setString(2,password);
int i = pst.executeUpdate();
if(i!=0){
pw.println("Record has been inserted");
}
else{
pw.println("Failed to insert the data");
}
}
catch (Exception e){
pw.println(e);
}
}
}
Here student is our dsn and login is our database table.
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.