In this example we will discuss how can we insert a data into database using Prepared Statement.
Prepared statement insert in java
In this example we will discuss how can we insert a data into database using Prepared Statement.
To accomplish the solution of this problem I used the PreparedStatement class of java.sql package. Benefit of the use of PreparedStatement is that it is compiled only once, then after it is compiled all times with that format no matter if there are dynamic parameters. In other word we can say PreparedStatement contains a pre compiled SQL statement so the DBMS doesn't have to require to compile the PreparedStatement first, it only run the PreparedStatement SQL statement, that's why the PreparedStatement is much faster than over all time. Further I have used the prepareStatement() method of Connection interface which creates an object of PreparedStatement that sends a SQL statements to the database into its parameter.
Example :
DataStore.html
<html> <head> <title>Store data into database</title> </head> <body> <form method= "get" action="/datainsert/DataInsertTable"> <p>Name <input type="text" name="name"></input></p> <p>Age <input type="text" name="age"></input></p> <p>Password <input type="password" name="psw"></input></p> <p></p> <p> <input type="submit" value="submit"></input></p> </form> </body> </html>
DataInsertTable.java
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DataInsertTable extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter pw = res.getWriter(); Connection con; PreparedStatement ps; ResultSet rs; try{ String nm = req.getParameter("name"); String ag = req.getParameter("age"); String pass = req.getParameter("psw"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:dataInsert"); ps = con.prepareStatement("INSERT INTO data (name,password) values(?,?)"); ps.setString(1,nm); ps.setString(2,pass); int i = ps.executeUpdate(); pw.println("<h1><center>Welcome</center></h1>"); pw.println(i); if(i!=0) { pw.println("Your Data has been successfully stored in database"); pw.println("Hello <b>"+nm+"</b><br>"); pw.println("Your password is "+pass); } else { pw.println("sorry failed to store ! try again......"); } } catch(Exception e) { pw.println(e.getMessage()); } } }
web.xml
<?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>DataInsertTable</display-name> <servlet> <servlet-name>DataInsertTable</servlet-name> <servlet-class>DataInsertTable</servlet-class> </servlet> <servlet-mapping> <servlet-name>DataInsertTable</servlet-name> <url-pattern>/DataInsertTable</url-pattern> </servlet-mapping> </web-app>
Output :
When you will execute the above example you will get the following output :
first entry
second entry
Record stored in database.
[ 0 ] Comments