Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Search this Website
Stay Informed
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:
Article Categories
.NET  .NET
  ASP (15)
  ASP.NET (29)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Latest Forum Activity
Re: Unable to insert data in an Access databa..
by Faisal Khan on 12 Nov 2008 Go To Post

Re: HOW TO MOVE FROM APACHE TO MS IIS
by Faisal Khan on 12 Nov 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by smithsan on 19 Oct 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by peeru999 on 8 Oct 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by Faisal Khan on 8 Oct 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : J2EE : Servlets : Inserting records into the Database with Java Servlets (page 2)
 

Inserting records into the Database with Java Servlets
by Faisal Khan.

Overview
This article is next in the series of articles about selecting, inserting, updating and deleting records from the database using JDBC. In this article we will learn how to insert records into the database. If you have followed my earlier article about 'Displaying Records from the Database' then this article is not going to be difficult at all. 90% of the code will be same. So if you haven't read that article then I will suggest that you go through that article before starting this one as quite a few important things have been explained in detail there.

How to Insert Records ?
To insert records into the database we will have to learn about another JDBC class, PreparedStatement. Although we can insert records using the Statement class we discussed in the last article, the INSERT operation is less efficient and not optimized at all. PreparedStatement fills that gap and lets us build SQL queries which are compiled and thus more efficient.

Note that not all database vendors support PreparedStatement class but still it is not a bad habit to use this class so that the ones that do support PreparedStatement class get the extra efficiency.

PreparedStatement
This class like other JDBC classes we have been discussing is present in the java.sql package. This is how you get handle on a PreparedStatement object :

	String sql = "INSERT INTO Names(first_name, last_name) VALUES (?,?)";

	// con is Connection object

	PreparedStatement ps = con.prepareStatement(sql);

Connection.prepareStatement() returns a reference to the PreparedStatement object. The only argument to the Connection.prepareStatement() method is an SQL statement containing optional '?' ( question mark ) containing SQL statement.

You should put '?' marks in the statement where you are going to put or change the values, for example in my example above I placed '?' marks at two places where I will put different values depending on the values inserted by the user.

So how to set the values of '?' parameters. You set the values by using a setXxx() methods of PreparedStatement class. setXxx() are over 25 methods whose syntax is setObject(int paramIndex, Object o) where paramIndex is the number of '?' mark from left to right in the SQL statement. For example we will use setString(1, value1) and setString(2, value2) methods to set the value of both parameters to two different values.

	ps.setString(1, "First Name");
	ps.setString(2, "Last Name");
	ps.executeUpdate();
	

Once the parameters are set in the PreparedStatement object, we execute the query using PreparedStatement.executeUpdate() method. You should use PreparedStatement.executeUpdate() for INSERT, UPDATE and DELETE SQL queries and PreparedStatement.executeQuery() for any SQL statement that returns records.

InsertServlet
Create a new InsertServlet.java file in the /APP_NAME/WEB-INF/classes/com/stardeveloper/servlets/db/ folder. Note /APP_NAME/ is the path of your application within your application server, in Tomcat 4.0 /APP_NAME/ will be /CATALINA_HOME/webapps/star/ where 'star' is the name of the application.

Copy and paste the following code into the InsertServlet.java file :


 ( 2 Remaining ) Next

Comments/Questions ( Threads: 9, Comments: 10 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Java servlet 'inserting' or 'retrieving' into/from SQLite database
  2. Retreving data from Database using Servlets
  3. inserting values in database using servlet and JSP
  4. Connection with Oracle database through JSP
  5. oracle connection
  6. java beans and jsp
  7. insertion
  8. Hi Again
  9. Inserting values into the databse at runtime ( 1 Reply )

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2008 Stardeveloper.com, All Rights Reserved.