insert into the databse

how to insert texbox data into database

Page 1 of 1

10 Replies - 3211 Views - Last Post: 24 March 2009 - 05:59 AM Rate Topic: -----

#1 sunitha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-January 08

insert into the databse

Posted 21 January 2008 - 10:20 PM

hi

i try to insert data into oracle database from a text box but i got error

my code is
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class pos1 extends HttpServlet{
PrintWriter out = null;
	
	Connection con = null;
	ResultSet rs;
	Statement Statement;
	   PreparedStatement ps;
		private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
	try{	
			PrintWriter out = response.getWriter();
	
			response.setContentType("text/html");
		
Class.forName("oracle.jdbc.driver.OracleDriver");

	  con = DriverManager.getConnection("jdbc:oracle:thin:@sanguine:1521:sanguine","retail","retail");
  Statement = con.createStatement();
		out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'><html><head><title>Add New Product</title>");
		out.println("<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>");
		out.println("<link rel='STYLESHEET' href='mystyle.css' type='text/css'>");
		
		out.println("<body><center><form name='form1' method='get'>");
		out.println("<table border='0' cellspacing='2' cellpadding='2'><thead><tr><th colspan='2'>NEW ENTIRES</th></tr></thead>");
		out.println("<tr> <td height='20'> </td> </tr><tr>");
		out.println("<td><label>NewBillno</label></td><td><input type='text' name='plu'></td></tr>");
		out.println("<tr><td><label>Itemcount</label> </td><td><input type='text' name='upc'></td></tr>");
		out.println("<tr><td> Prd_Id_Plu </td><td> <input type='text' name='desc'> </td></tr>");
		out.println("<tr><td> Price </td><td> <select < name='price'>");
			   try{
			PreparedStatement ps = con.prepareStatement("select price from price_master");
			rs = ps.executeQuery();
			while(rs.next()){
				out.println("<option>"+rs.getString(1).toString()+"</option>");
			}
		}catch(SQLException sql){
			sql.printStackTrace();
		}
		out.println("</select> </td></tr>");
		
		
		
		out.println("<tr><td> Qty </td><td> <input type='text' name='cat'> </td></tr>");
		
		out.println("<tr><td> Tax Id </td><td> <select name='tax'>");
			
		try{
			PreparedStatement ps = con.prepareStatement("select tax_name from tax");
			rs = ps.executeQuery();
			while(rs.next()){
				out.println("<option>"+rs.getString(1).toString()+"</option>");
			}
		}catch(SQLException sql){
			sql.printStackTrace();
		}
				out.println("</select> </td></tr>");
		
				out.println("<tr><td><label>Amount</label> </td><td><input type='text' name='lvl'></td></tr>"); 
		
				
				
				out.println("<tr> <td height='20'> </td> </tr><tr><td align='right'> <input type='submit' name='save' value='Save' style='width:70;height:30;' onclick='insert()' class='button'> </td>");
		out.println("<td align='center'> <input type='reset' name='cancel' value='Cancel' style='width:70;height:30;' class='button'> </td>	</tr></table>");
		 con.setAutoCommit(false);
			  
				 String str = "insert into trans_details values(?,?,?,?,?,?,?)";
				ps = con.prepareStatement(str);
			 
				 int pid = Integer.parseInt(request.getParameter("plu"));
			
			int p = Integer.parseInt(request.getParameter("upc"));
			int tax = Integer.parseInt(request.getParameter("desc"));
			int lvl = Integer.parseInt(request.getParameter("price"));
			int l = Integer.parseInt(request.getParameter("cat"));
			int r = Integer.parseInt(request.getParameter("tax"));
	int o = Integer.parseInt(request.getParameter("lvl"));	
		
			ps.setInt(1,pid);
			ps.setInt(2,p);
			ps.setInt(3,tax);
			ps.setInt(4,lvl);
			ps.setInt(5,l);
			ps.setInt(6,r);
			ps.setInt(7,o);
			
				
		
			 ps.executeUpdate();  
			   
				out.println("</form></body></html>");
		} catch (ClassNotFoundException e) {
	  out.println("Driver Error");
	} catch (SQLException e) {
	  out.println("SQLException: " + 
e.getMessage());
	}
		}
}


edit: added code tags PB

when i press submit button watever u enter into the texbox that will be stored in daztabse that is my requirment

pls can any one help
it is very urgent pls pls

thanks in advance

This post has been edited by sunitha: 22 January 2008 - 02:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: insert into the databse

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: insert into the databse

Posted 22 January 2008 - 03:35 AM

Please post the error, too!
Was This Post Helpful? 0
  • +
  • -

#3 sunitha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-January 08

Re: insert into the databse

Posted 22 January 2008 - 03:39 AM

View Post1lacca, on 22 Jan, 2008 - 03:35 AM, said:

Please post the error, too!

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
pos1.doGet(pos1.java:85)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.
Apache Tomcat/5.5.17
Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: insert into the databse

Posted 22 January 2008 - 06:11 AM

Quote

java.lang.NullPointerException
pos1.doGet(pos1.java:85)

shows that you try to use an object that is null. It is in the place (line 85) where you parse the parameters, so probably one of them is missing, or misspelled.
Was This Post Helpful? 0
  • +
  • -

#5 lovable_siva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-January 08

Re: insert into the databse

Posted 22 January 2008 - 09:43 AM

hi,
u have to override dopost() method or,check variable name or database data types correctly.try and tell

siva

Was This Post Helpful? 0
  • +
  • -

#6 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: insert into the databse

Posted 22 January 2008 - 10:35 AM

Quote

u have to override dopost()

It is common practice to override both doPost and doGet, but it is not an error not to.
He clearly uses GET correctly, since the null pointer exception is thrown inside the doGet function, so it is not the cause of the error.
Was This Post Helpful? 0
  • +
  • -

#7 sunitha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-January 08

Re: insert into the databse

Posted 22 January 2008 - 09:47 PM

View Post1lacca, on 22 Jan, 2008 - 10:35 AM, said:

Quote

u have to override dopost()

It is common practice to override both doPost and doGet, but it is not an error not to.
He clearly uses GET correctly, since the null pointer exception is thrown inside the doGet function, so it is not the cause of the error.

but i cant get the proper result again it shows an error
Was This Post Helpful? 0
  • +
  • -

#8 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: insert into the databse

Posted 23 January 2008 - 11:35 AM

Did you check post#4?
Was This Post Helpful? 0
  • +
  • -

#9 sunitha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-January 08

Re: insert into the databse

Posted 30 January 2008 - 02:28 AM

View Post1lacca, on 23 Jan, 2008 - 11:35 AM, said:

Did you check post#4?

hi
i got the answer
Was This Post Helpful? 0
  • +
  • -

#10 rajapcs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-January 09

Re: insert into the databse

Posted 22 January 2009 - 06:20 AM

View Postsunitha, on 21 Jan, 2008 - 09:20 PM, said:

hi

i try to insert data into oracle database from a text box but i got error

my code is
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class pos1 extends HttpServlet{
PrintWriter out = null;
	
	Connection con = null;
	ResultSet rs;
	Statement Statement;
	   PreparedStatement ps;
		private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
	try{	
			PrintWriter out = response.getWriter();
	
			response.setContentType("text/html");
		
Class.forName("oracle.jdbc.driver.OracleDriver");

	  con = DriverManager.getConnection("jdbc:oracle:thin:@sanguine:1521:sanguine","retail","retail");
  Statement = con.createStatement();
		out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'><html><head><title>Add New Product</title>");
		out.println("<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>");
		out.println("<link rel='STYLESHEET' href='mystyle.css' type='text/css'>");
		
		out.println("<body><center><form name='form1' method='get'>");
		out.println("<table border='0' cellspacing='2' cellpadding='2'><thead><tr><th colspan='2'>NEW ENTIRES</th></tr></thead>");
		out.println("<tr> <td height='20'> </td> </tr><tr>");
		out.println("<td><label>NewBillno</label></td><td><input type='text' name='plu'></td></tr>");
		out.println("<tr><td><label>Itemcount</label> </td><td><input type='text' name='upc'></td></tr>");
		out.println("<tr><td> Prd_Id_Plu </td><td> <input type='text' name='desc'> </td></tr>");
		out.println("<tr><td> Price </td><td> <select < name='price'>");
			   try{
			PreparedStatement ps = con.prepareStatement("select price from price_master");
			rs = ps.executeQuery();
			while(rs.next()){
				out.println("<option>"+rs.getString(1).toString()+"</option>");
			}
		}catch(SQLException sql){
			sql.printStackTrace();
		}
		out.println("</select> </td></tr>");
		
		
		
		out.println("<tr><td> Qty </td><td> <input type='text' name='cat'> </td></tr>");
		
		out.println("<tr><td> Tax Id </td><td> <select name='tax'>");
			
		try{
			PreparedStatement ps = con.prepareStatement("select tax_name from tax");
			rs = ps.executeQuery();
			while(rs.next()){
				out.println("<option>"+rs.getString(1).toString()+"</option>");
			}
		}catch(SQLException sql){
			sql.printStackTrace();
		}
				out.println("</select> </td></tr>");
		
				out.println("<tr><td><label>Amount</label> </td><td><input type='text' name='lvl'></td></tr>"); 
		
				
				
				out.println("<tr> <td height='20'> </td> </tr><tr><td align='right'> <input type='submit' name='save' value='Save' style='width:70;height:30;' onclick='insert()' class='button'> </td>");
		out.println("<td align='center'> <input type='reset' name='cancel' value='Cancel' style='width:70;height:30;' class='button'> </td>	</tr></table>");
		 con.setAutoCommit(false);
			  
				 String str = "insert into trans_details(your database field names) values(?,?,?,?,?,?,?)";
				ps = con.prepareStatement(str);
			 
				 int pid = Integer.parseInt(request.getParameter("plu"));
			
			int p = Integer.parseInt(request.getParameter("upc"));
			int tax = Integer.parseInt(request.getParameter("desc"));
			int lvl = Integer.parseInt(request.getParameter("price"));
			int l = Integer.parseInt(request.getParameter("cat"));
			int r = Integer.parseInt(request.getParameter("tax"));
	int o = Integer.parseInt(request.getParameter("lvl"));	
		
			ps.setInt(1,pid);
			ps.setInt(2,p);
			ps.setInt(3,tax);
			ps.setInt(4,lvl);
			ps.setInt(5,l);
			ps.setInt(6,r);
			ps.setInt(7,o);
			
				
		
			 ps.executeUpdate();  
			   
				out.println("</form></body></html>");
		} catch (ClassNotFoundException e) {
	  out.println("Driver Error");
	} catch (SQLException e) {
	  out.println("SQLException: " + 
e.getMessage());
	}
		}
}


edit: added code tags PB

when i press submit button watever u enter into the texbox that will be stored in daztabse that is my requirment

pls can any one help
it is very urgent pls pls

thanks in advance

Was This Post Helpful? 0
  • +
  • -

#11 rajapcs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-January 09

Re: insert into the databse

Posted 24 March 2009 - 05:59 AM

U use CreateStament instead of using PrepareStatement.
Post error,html code,ur servlet code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1