HTML FORM
<html>
<body>
<pre>
<form method="post" action="register1" >
Id :<input type="text" name="id"/><br>
Name:<input type="text" name="name"/><br>
<input type="submit" value="register"/>
</form>
</pre>
</body>
</html>
register1.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
/*
* @author KANHA
*/
public class register1 extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String user = request.getParameter("name");
String id = request.getParameter("id");
out.println("<h1>Insert To Database</h1>");
//load the driver
Class.forName("com.mysql.jdbc.Driver");
//Class.forName("oracle.jdbc.OracleDriver"); (oracle driver)
//establish the connection with database
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
//con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","mca", "mca"); (oracle connection)
//sending sql query to database for execution
Statement st=con.createStatement();
String q="insert into user (id,name) values ('"+id+"','"+user+"')";
int i =st.executeUpdate(q);
if(i>0){
out.println("inserted Successfully");
}
//conn.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
0 Comments