Using jsp and javabeans to insert into database.

[nobr]I have error in my code. I am trying to do a simple database insert program using javabeans and jsp. I get a value to be inserted in database through the jsp page in a text box and would like to be inserted into database using beans The connection to database and mysql query are in java file.

Here is the code.

<%@page contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<%@ page language="Java" import="java.sql.*" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

<html>

<head>

</head>

<body>

<form name="form1" action="beancode" method="POST">

Emp ID: <input type="text" name ="emplid"> <br><br><br>

<input type ="submit" value="Submit">

<jsp:useBean id="sampl" class="beancode" scope="page">

<jsp:setProperty name="sampl" property="*"/>

</jsp:useBean>

</form>

</body>

</html>

I know i might have made a mistake here in using the bean. Here is the java code which does the insert part.

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

/**

*

* @author Trainees

*/

publicclass beancode

{

private String employid;

private Connection con =null;

private ResultSet rs =null;

private PreparedStatement st =null;

/** Creates a new instance of beancode */

public beancode()

{

try

{

Class.forName("org.gjt.mm.mysql.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample?user=user&password=password");

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

publicvoid setemployid(String empid)

{

employid = empid;

}

public String getemployid()

{

return (employid);

}

publicvoid insert()

{

try

{

String s1="insert into samp values('"+employid+"')";

st = con.prepareStatement(s1);

st.executeUpdate();

st.clearParameters();

st.close();

}

catch(Exception m)

{

//

}

}

}

If anyone could help me out with where i went wrong in the code and if you could help me with corrections in it. Would be of real great help to me. Thanks in advance. [/nobr]

[4596 byte] By [JaVaSwInGsa] at [2007-10-2 16:50:15]
# 1

What's action="beancode" ?

You need a servlet (or a second jsp) that handles the request when you click on the submit button.

> <jsp:useBean id="sampl" class="beancode" scope="page">

><jsp:setProperty name="sampl" property="*"/>

></jsp:useBean>

this part fills the bean with the values from the request the Page is requested it is not called when you click on the submit button.

andi

andiha at 2007-7-13 18:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

>What's action="beancode" ?

this is the name of the file "beancode.java" on clicking submit and submiting the form the control passes to.

>You need a servlet (or a second jsp) that handles the request when you click on the submit button.

Id like to use a servlet. But what should be there in the servlet and how can i get this done.

> <jsp:useBean id="sampl" class="beancode" scope="page">

><jsp:setProperty name="sampl" property="*"/>

></jsp:useBean>

>this part fills the bean with the values from the request the Page is >requested it is not called when you click on the submit button.

so should i put this piece of code in the beancode.java program that i wrote and not in the jsp file?

Im confused and im just a noob trainee. Hope u can help me.

Thanks in advance

JaVaSwInGsa at 2007-7-13 18:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> >What's action="beancode" ?

>

> this is the name of the file "beancode.java" on

> clicking submit and submiting the form the control

> passes to.

Have you seen that or do you expect it to do so ?

> Id like to use a servlet. But what should be there in

> the servlet and how can i get this done.

Write a JavaClass that extends [url=http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServlet.html]HttpServlet [/url]and overwrite the doPost Method. There you can put your database related code. After you have written your Servlet you have to configure it by editing the deployment descriptor (web.xml file)

Here is a collection of some JSP / Servlet tutorials

http://www.laliluna.de/tutorials.html

> Im confused and im just a noob trainee. Hope u can

> help me.

As allready told. Put your database related code from your Bean into

the Servlet.

You should ask your teacher / trainer / tutor or even read at least one

of the tutorials for how to do this.

andi

andiha at 2007-7-13 18:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Ok i can write a servlet that is called by the jsp file wherein i write the database related code. I use netbeans ide so i think there wouldbt be a problem in configuring the xml file but what will be there in the javabeans file. or should i just drop the javabeans file? The requirement given to me was a html form to feed or retrieve data to the DB thru the bean with Servlet controller in middle

JaVaSwInGsa at 2007-7-13 18:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...