how do i insert into data base

M

mak1084

i'm creating a an web application on attendance, in which a user
after selecting the subject and month he gets all the student info who
has taken that subject.

my problem is i'm able to get the multiple student with the text box
where a user can put up his monthly attendance. how do i insert the
info in database...at the same time after filling the info.

the snapshot of the code is here...
here I'm getting the roll no. along with the text box..

Code:
String str1 = "select roll_no from student where sem_id = (select
sem_id from subject where course_id ='bsc_it' and sub_id =
'"+getsub1+"')";
											ResultSet rs  = stmt.executeQuery(str1);
								%>
							<table  align="center" width="" cellpadding="0" cellspacing="0"
border="1" cellspacing="1" cellpadding="1">
									 <tr>
										<td><input type="text"  value="Total Lecture" readonly=""/></
td>
										<td><input type="text" name="total_att" maxlength="2"></td>
									 </tr>
[QUOTE="									"]
</td>[/QUOTE]
									 </tr>
										<%			while(rs.next())

													{
										%>

										<%
														stu_roll = rs.getString("roll_no");
										%>
									 <tr bordercolor="#CC3366">
										<td>
										<%
														out.println(stu_roll);
										%>
										</td>
										<td>
										  <input type="text"  name="att" />
										</td>
									</tr>&nbsp;



								<%
										}

											con.close();
										}
										catch(SQLException e)
										{
											out.println("Exception in SQL" + e);
										}
								%>
 
S

Stanimir Stamenkov

Fri, 15 Feb 2008 19:15:58 -0800 (PST), /[email protected]/:
my problem is i'm able to get the multiple student with the text box
where a user can put up his monthly attendance. how do i insert the
info in database...at the same time after filling the info.

It would help reading your messages if you use capital letters at
the start of sentences.

I haven't exactly got what problem you're having. You insert into
an SQL database using an INSERT statement. You would get the info
to insert making the user post back the form he has filled to the
server.

If you want to send the data to the server without making the
current page go away, you would need to resort to client side
scripting and usage of XMLHttpRequest object [1], I guess. In this
regard the comp.lang.javascript group may be more appropriate to ask
for further help.

[1] http://www.w3.org/TR/XMLHttpRequest/
 
L

Lew

please reply if any one can help me...

Perhaps people are responding over in comp.lang.java.databases, where you
multi-posted. Some people will not answer multi-posted queries, as they get
frustrated at the fragmentation of the thread and finding someone else
answered in one group where they answered in another.

If you read David Alex Lamb's FAQ post, "comp.lang.java.{help,programmer} -
what they're for (mini-FAQ 2006-03-31)", sent every five days, and the links
to which it posts, you will get excellent advice on how to make best use of
these newsgroups, including the strictures against multi-posting.

(Cross-posted to help the OP.)
 
M

mak

Stanimir said:
Fri, 15 Feb 2008 19:15:58 -0800 (PST), /[email protected]/:
my problem is i'm able to get the multiple student with the text box
where a user can put up his monthly attendance. how do i insert the
info in database...at the same time after filling the info.

It would help reading your messages if you use capital letters at
the start of sentences.

I haven't exactly got what problem you're having. You insert into
an SQL database using an INSERT statement. You would get the info
to insert making the user post back the form he has filled to the
server.

If you want to send the data to the server without making the
current page go away, you would need to resort to client side
scripting and usage of XMLHttpRequest object [1], I guess. In this
regard the comp.lang.javascript group may be more appropriate to ask
for further help.

[1] http://www.w3.org/TR/XMLHttpRequest/
 
M

mak

thanks for the reply
I haven't exactly got what problem you're having. You insert into
an SQL database using an INSERT statement. You would get the info
to insert making the user post back the form he has filled to the
server.

There is two procedure.
1st a user selects the subject, month, year. And clicks submit to get
the student who has taken that subject. Here in fornt of all the
students roll no. a text box is comes in which user has to put the
total no. of lecture he has attended in that subject.
all this happns in the same form.
2nd after entering the required field(i.e text box.) user clicks on
submit button...Now the textbox data should go in other table along
with students Roll. no.

Now I'm not able to understand how should i insert the value in the
'attendance' table.
 
M

mak

(e-mail address removed) wrote in comp.lang.java.programmer:




Perhaps people are responding over in comp.lang.java.databases, where you
multi-posted.  Some people will not answer multi-posted queries, as they get
frustrated at the fragmentation of the thread and finding someone else
answered in one group where they answered in another.

If you read David Alex Lamb's FAQ post, "comp.lang.java.{help,programmer} -
what they're for (mini-FAQ 2006-03-31)", sent every five days, and the links
to which it posts, you will get excellent advice on how to make best use of
these newsgroups, including the strictures against multi-posting.

(Cross-posted to help the OP.)

Hey thanks for the information... and sorry if i created any problem.
This is was my first post amd also i thought both the groups are
different.

Mak
 
L

Lew

There is two procedure.
1st a user selects the subject, month, year. And clicks submit to get
the student who has taken that subject. Here in fornt of all the
students roll no. a text box is comes in which user has to put the
total no. of lecture he has attended in that subject.
all this happns in the same form.
2nd after entering the required field(i.e text box.) user clicks on
submit button...Now the textbox data should go in other table along
with students Roll. no.

Now I'm not able to understand how should i insert the value in the
'attendance' table.

I'll attempt an answer, but be aware that I've been wrong before. I'm also
cross-posting (again) in order to unify your threads.

Side note:
Your listings will only be usable if you don't over-indent them. Use spaces,
not TABs, for Usenet indentation, and keep the indentation level to a small
number, two, three or four spaces (pick one). Also, most times they should be
complete, yet short, and illustrative.
<http://www.physci.org/codes/sscce.html>

Now back to your code:
Code:
String  str1  =  "select  roll_no  from  student  where  sem_id  =  (select
sem_id  from  subject  where  course_id  ='bsc_it'  and  sub_id  =
'"+getsub1+"')";[/QUOTE]

You've already been warned upthread about plugging in values like 'getsub1' 
directly into SQL statements.  Google for "SQL injection attack" for more details.
[QUOTE]
ResultSet  rs    =  stmt.executeQuery(str1);
%>[/QUOTE]

One way to control your logic better is to have the JSP submit its form to a 
servlet, which servlet in turn will call full-fledged Java classes to do its 
logic.  Search the Sun site for "Model 2 architecture" for some details on this.

Best practices dictate having no raw Java code ("scriptlet") in a JSP.  Use 
tag libraries, especially the JSP Standard Tag Library (JSTL), and the Model 2 
architecture instead.
<http://java.sun.com/javaee/5/docs/tutorial/doc/bnakc.html>
<http://java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp/index.html>
[QUOTE]
<table align="center"  width=""  cellpadding="0"  cellspacing="0"
border="1"  cellspacing="1"  cellpadding="1">
<tr>
<td><input  type="text" value="Total  Lecture"  readonly=""/></td>
<td><input  type="text"  name="total_att"  maxlength="2"></td>
</tr>
<tr>
<td><input  type="text"  value="Student  roll  no."  readonly="" /></td>[/QUOTE]

Why is the 'readonly' attribute there?
[QUOTE]
</tr>
<% while(rs.next())
{
%>
<%
stu_roll  =  rs.getString("roll_no");
%>
<tr  bordercolor="#CC3366">
<td><% out.println(stu_roll);  %>[/QUOTE]

The newline will look like any old whitespace to the HTML renderer.  Better is 
the tag <c:out> or the old-fashioned '<%= stu_roll %>'.  Also, side note, Java 
convention is to name variables without underscores, except for compile-time 
constants.  Use compound word with each word part capitalized except the 
first: 'stuRoll'.
[QUOTE]
</td>
<td> <input  type="text" name="att"  /> </td>[/QUOTE]

Is this the value you want to insert?
[QUOTE]
</tr>&nbsp;

<%
}

con.close();
}
catch(SQLException  e)
{
out.println("Exception  in  SQL"  +  e);
}
%>

You really need the Model-View-Controller (MVC) architecture, of which "Model
2" is an example, to do this easily.

Create a controller servlet, let's call it 'com.lewscanon.servlet.Controller'
for the sake of argument. In your web.xml, map it to a reasonable URL, let's
say '/Controller' within your application.

<form action="Controller" method="post">

In the Controller, you pass the input parameters to the business logic, in
this case the parameter named "att".

public class Controller extends HttpServlet
{
...
protected void doPost( HttpServletRequest req, HttpServletResponse rsp )
throws ServletException, IOException
{
String screen = req.getParameter( "screen" );
String att = req.getParameter( "att" );
// probably shouldn't hard-code "att"

MyDao dao = new MyDao( connectionInfo );
boolean success = dao.insertAtt( att );
req.setAttribute( "success", Boolean.valueOf( success ));
// this puts the result in the request so the next JSP can retrieve it

String nextPg = lookupView( screen, success );
// returns next JSP to view, depending on previous 'screen'
// and on the 'success' of the business logic.
// An enum is probably better than a boolean for 'success'

RequestDispatcher rd = req.getRequestDispatcher( nextPg );
rd.forward( req, rsp );
}
...
}

Now your MyDao class's 'insertAtt()' method handles the messy database logic.
You create or retrieve a connection there, instantiate a PreparedStatement
with an INSERT command, plug in the att value, execute, and Bob's your uncle.

Obviously I've left out a ton of detail, such as error-checking, or how do you
build a map of valid next views based on previous screen and logic outcome, or
how you organize your business logic in a class hierarchy of logic handlers
with a common supertype.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top