aspose file tools*
The moose likes JDBC and the fly likes insertion  of checkbox values from jsp to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "insertion  of checkbox values from jsp to database" Watch "insertion  of checkbox values from jsp to database" New topic
Author

insertion of checkbox values from jsp to database

gopi sri
Greenhorn

Joined: Nov 11, 2008
Posts: 6
hai any one can help regarding this.

iam involved in e-ticketing project..

iam going to retrieve ticket values from database to jsp page. Now i want to book the ticket so i will select the ticket using checkbox..the selected ticket value should be insert into mysql db..i tried to insert but its not inserting can anyone help me..
my code is here below :


<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>�

<html>
<head>
<title>display data from the table using jsp</title>
</head>
<body>
<h2 align="center"></h2>
<select id="cbosex" name="cbosex">
<option value="true">Male</option>
<option value="false">Female</option>
</option>

<%
try {

Connection con = null;
Statement st= null;
ResultSet rs = null;


Class.forName("org.gjt.mm.mysql.Driver");
con=DriverManager.getConnection("jdbc:mysql://192.168.1.201:3306/e_busticketing","root","password");
st = con.createStatement();
out.println("connected");
String sql="select seat_no from e_busticketing.seat_mast where bus_id=1001";
rs=st.executeQuery(sql);

%>
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;" width=20% align="center">
<%
while (rs.next()) {
%>
<TR>
<TD><INPUT TYPE=CHECKBOX NAME=c1 value=<%=rs.getString("seat_no")%> ></TD>

</TR>
<% } %>
<%
// close all the connections.
rs.close();
st.close();
con.close();

}
catch (Exception ex)
{
%>
</font>

<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</TABLE><TABLE align="center">
<TR>
<TD><FORM ACTION="busdetail.jsp" method="get" >
<button type="submit"><-- back</button></TD>
</TR>
</TABLE>
</font>

<%
/*
try
{
Class.forName("org.gjt.mm.mysql.Driver");
con=DriverManager.getConnection("jdbc:mysql://192.168.1.201:3306/e_busticketing","root","password");
out.println("connected sucessfully");

Statement st = con.createStatement();
int numRowsChanged = st.executeUpdate("insert into e_busticketing.seat_details(seat) values (c1)";

out.println("data inserted");

}



catch (Exception e)
{
e.printStackTrace();
}
}
}
*/
%>
</body>
</html>
regards
gopi
[ November 11, 2008: Message edited by: Bear Bibeault ]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
What error are you getting when you do the insert or it's just not inserting?

You should probably refactor your code and move all that database logic to the backend and call the backend via a servlet. Search for MVC for more info.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
Your Logic is quite fuzzy
As said by earlier respondent please move all JDBC related code to some Controller or some Business Layer

Please don't open connections in jsp.

You can't handle exceptions in elegant way here

Besides this,move our actions event oriented Like on clicking of some button pass the values from jsp to some servlet and there insert values appropriately


SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
gopi sri
Greenhorn

Joined: Nov 11, 2008
Posts: 6
thank you friends,

i will do that...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: insertion of checkbox values from jsp to database