Servlet Databse Insert Operation
//HTML FILE
<form action="http://localhost/servlet/Servlet3_insert_db" method="get">
ID : <input type="Text" name="id"><br><br>
Username : <input type="Text" name="unm"><br><br>
Password : <input type="Text" name="pass"><br><br>
<input type="submit" name="sb" value="submit">
</form>
//Servlet Databse Insert Operation
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import pack.database_connection;
public class Servlet3_insert_db extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try
{
String dbDriver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql://localhost:3306/";
String dbname = "test_mydb";
String unm = "root";
String pass ="5044";
Class.forName(dbDriver);
Connection con = DriverManager.getConnection(dbURL + dbname, unm, pass);
PreparedStatement st = con.prepareStatement("insert into login values(?, ?, ?)");
st.setInt(1, Integer.parseInt(req.getParameter("id")));
st.setString(2, req.getParameter("unm"));
st.setString(3, req.getParameter("pass"));
st.executeUpdate();
st.close();
con.close();
out.println("<h3>Data Successfully Inserted</h3>" );
}
catch(Exception ex)
{
out.println("<b>" + ex + "</b>");
}
}
}
Comments
Post a Comment