Hi i am writing a spring mvc hibernate annotation application there i have 2 tables "team" and another table "players".Here i am using one-to-one mapping and mysql database.In players table i am keeping player records like name,years active etc.The "team" table contains two entries teamid(primary key)and teamname.
The "players" table contains the foreign key teamid.Here when i try to delete a teamname without deleting all its references in players table i am getting error message
HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute update query
so when i delete all the references ,then there is no error.So how can i find all the refrences before deleting.I am interested in showing an alert message using javascript which warns to delete all references.I know how to create simple alert message in javascript,but here i have to show the alert only if there is a reference to foreign key.And i want to know is there any alternate way to delete the foriegn key reference without affecting the "players" table.
AddTeam.java
#Entity
#Table(name="Team")
public class AddTeam {
#Id
#Column(name="teamId")
private Integer teamId;
#Column(name="teamName")
private String teamName;
public Integer getTeamId() {
return teamId;
}
public void setTeamId(Integer teamId) {
this.teamId = teamId;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
}
Player.java
#Entity
#Table(name="playerdata")
public class Player implements Serializable {
private static final long serialVersionUID = -723583058586873479L;
/**
*
*/
#Id
#Column(name="playerid")
private Integer playerId;
#Column(name="playername")
private String playerName;
#Column(name="yearsactive")
private Integer yearsActive;
#Column(name="country")
private Integer Country;
#OneToOne
#JoinColumn(name="teamId")
private AddTeam teams;
public Integer getPlayerId(){
return playerId;
}
public void setPlayerId(Integer playerId){
this.playerId=playerId;
}
public String getPlayerName(){
return playerName;
}
public void setPlayerName(String playerName){
this.playerName=playerName;
}
public String getCountry(){
return Country;
}
public void setCountry(String Country){
this.Country=Country;
}
public Integer getyearsActive(){
return yearsActive;
}
public void setyearsActive(Integer yearsActive){
this.yearsActive=yearsActive;
}
public AddTeam getTeams() {
return teams;
}
public void setTeams(AddTeam teams) {
this.teams = teams;
}
}
and this is the query used in playerDaoImplementation.java class
#Override
public void deleteResource(int playerid) {
// TODO Auto-generated method stub
sessionfactory.getCurrentSession().createQuery("DELETE FROM Resource WHERE playerid=" +playerid).executeUpdate();
}
Query used to delete in AddteamDaoImplementation.java
#Override
public void deleteTeams(int teamid) {
// TODO Auto-generated method stub
sessiofactory.getCurrentSession().createQuery("DELETE FROM AddTeam WHERE teamid="+teamid).executeUpdate();
}
deletefunction in PlayerController.java
#RequestMapping(value="/deletePlayer",method=RequestMethod.GET)
public ModelAndView deletePlayerDetails(#ModelAttribute("command") Player player,
BindingResult result){
playerService.deletePlayer(player.getPlayerId());
Map<String, Object> model = new HashMap<String, Object>();
model.put("playerkey", playerService.listPlayer());
model.put("teamKey", addteamService.listTeams());
return new ModelAndView("EditPlayer",model);
}
Team.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Players Manager</title>
<center>
<h2>Add Team Details</h2>
<form:form method="POST" action="Team.html">
<table>
<tr>
<td><form:label path="teamId">Team ID:</form:label></td>
<td><form:input path="teamId" id="demo" value="${team.teamId}"/></td>
</tr>
<tr>
<td><form:label path="teamName">Team Name:</form:label></td>
<td><form:input path="teamName" value="${team.teamName}"/></td>
</tr>
<tr>
<tr>
<td> </td>
<td><input type="submit" value="SAVE"/></td>
</tr>
</table>
</form:form>
<br/>
<c:if test="${!empty teamKey}">
<table align="center" border="1">
<tr>
<th>Category ID</th>
<th>Category Name</th>
<th>Options</th>
</tr>
<c:forEach items="${teamKey}" var="team">
<tr>
<td><c:out value="${team.teamId}"/></td>
<td><c:out value="${team.teamName}"/></td>
<td align="center">Edit |
Delete</td>
</tr>
</c:forEach>
</table>
</c:if>
<h2>Adding Publication</h2>
</center>
</body>
</html>
EditPlayer.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Resource Manager</title>
</head>
<body>
<center>
<h2>Add Players</h2>
<form:form method="POST" action="save.html">
<table>
<tr>
<td><form:label path="playerId">Player Id</form:label></td>
<td><form:input path="playerId" value= "${player.playerId }"/></td>
</tr>
<tr>
<td><form:label path="playerName">Name</form:label></td>
<td><form:input path="playerName" value="${player.playerName }"/></td>
</tr>
<tr>
<td><form:label path="YearsActive">Experience</form:label></td>
<td><form:input path="YearsActive" value="${player.YearsActive }"/></td>
</tr>
<tr>
<td><form:label path="Country">Date of Join</form:label></td>
<td><form:input path="Country" value="${player.Country }"/></td>
</tr>
<tr>
<td>
<form:label path="teams.teamId">Team Name</form:label>
</td>
<td>
<form:select path="teams.teamId" cssStyle="width: 150px;">
<option value="-1">Select a type</option>
<c:forEach items="${teamKey}" var="teams">
<option value="${teams.teamId}">${teams.teamName}</option>
</c:forEach>
</form:select>
</td>
</tr>
<tr>
<tr>
<td colspan="2"><input type="submit"value="Submit"></td>
</tr>
</table>
</form:form>
<br/>
<c:if test="${!empty playerkey}">
<table align="center" border="1">
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>YearsActive</th>
<th>Country</th>
</tr>
<c:forEach items="${playerkey}" var="player">
<tr>
<td><c:out value="${player.playerId}"/></td>
<td><c:out value="${player.playerName }"/></td>
<td><c:out value="${player.YearsActive}"/></td>
<td><c:out value="${player.Country}"/></td>
<td align="center">Edit | Delete</td>
</tr>
</c:forEach>
</table>
</c:if>
<h2>Adding Team</h2>
</center>
</body>
</html>
please help.
thanks in advance
For your use case your database modelling ( if you have schema already defined) or entity modelling( in case you are generating schema from your entity model) is incorrect. The foreign key should be on the AddTeam table pointing to the primary key of Player. In terms of entity mappings, the OneToOne should be on the AddTeam.
Related
I'm having a problem accessing a nested Set of objects.
I have defined the below objects :
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Table(name = "site")
public class Site {
#Id
#GeneratedValue(strategy = GerationType.IDENTITY)
#Column(name="id", updatable=false,nullable=false)
private Long id;
private String siteName;
private String siteLocation;
#OneToMany(cascade=CascadeType.ALL, mappedBy = "site")
private Set<Rack> rack = new HashSet<>();
}
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Table(name = "rack")
public class Rack {
#Id
#GeneratedValue(strategy = GerationType.IDENTITY)
#Column(name="id", updatable=false,nullable=false)
private Long id;
private String rackName;
private String rackAssetTag;
private String rackCMDBCode;
#ManyToOne
#JoinColumn(name = "site_id")
private Site site;
#OneToMany(cascade=CascadeType.ALL, mappedBy = "box")
private Set<Box> box = new HashSet<>();
}
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Table(name = "box")
public class Box {
#Id
#GeneratedValue(strategy = GerationType.IDENTITY)
#Column(name="id", updatable=false,nullable=false)
private Long id;
private boxAssetTag;
private boxCMDBCode;
ManyToOne
#JoinColumn(name = "rack_id")
private Rack rack;
}
All relation mapping work tiptop.
The problem is when I want to create a nice nested table for this(css formating and conditional thymeleaf validation removed since it's irrelevant) :
<div>
<table>
<thead>
<tr>
<th>Rack name</th>
<th>Rack asset tag</th>
<th>Rack CMDB code</th>
</tr>
</thead>
<tbody>
<tr th:each="rack:${site.rack}">
<td th:text="${rack.rackName}"></td>
<td th:text="${rack.rackAssetTag}"></td>
<td th:text="${rack.rackCMDBCode}"></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Box asset tag</th>
<th>Box CMDB code</th>
</tr>
</thead>
<tbody>
<tr th:each="box:${rack.box}">
<td th:text="${box.boxAssetTag}">
<td th:text="${box.boxCMDBCode}">
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
Controller adds one object 'site' to the model that holds all relation.
When accessing the page I receive and error:
Property or field 'box' cannot be found on null
I think that when I move to the second table thymeleaf looses the context of the object rack created in the outer table. Therefore when I try to invoke the th:each in the inner table the there is no rack object to perform ${rack.box}.
The question is how to be able to access the 'deeper' object in thymeleaf without loosing the context of the object above?
Regards,
Jarek.
Ok so I've managed to work out a solution.
I'll write it up. Maybe someone some day will need it.
So the idea is to cycle through each object but on the body element not on the row. This lets You have the context of the object much wider
<div>
<table>
<thead>
<th>Rack name</th>
<th>Rack asset tag</th>
<th>Rack CMDB code</th>
</thead>
<tbody th:each="rack:${site.rack}">
<tr>
<td th:text="${rack.rackName}"></td>
<td th:text="${rack.rackAssetTag}"></td>
<td th:text="${rack.rackCMDBCode}"></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Box asset tag</th>
<th>Box CMDB code</th>
</tr>
</thead>
<tbody>
<tr th:each="box:${rack.box}">
<td th:text="${box.boxAssetTag}">
<td th:text="${box.boxCMDBCode}">
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
I am using JSP as my backend for my HTML and I want to get the customer name when I start entering the customer contact number.
HTML:
<html>
<body>
<script>
function evaluation() {
var myBox1 = document.getElementById('milk').value;
var result = document.getElementById('result');
var myResult = myBox1 * 35;
result.value = myResult;
}
</script>
<form action="BillValid.jsp" method="post">
<table width="70%" cellspacing="30" color="#FFFFFF">
<tr>
<th>ENTER THE DETAILS</th>
</tr>
<tr>
<td><font color="#800000">Customercontact</font></td>
<td><input name="cus_contact" type="text" id="contact"></td>
</tr>
<tr>
<td><fontcolor="#800000">CustomerName</font></td>
<td><input type="text" name="cus_name"></td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
<tr>
<td><font color="#800000">DayConsumption</font></td>
<td><input id="milk" type="text" name="days_con"
oninput="evaluation()"></td>
</tr>
<tr>
<td><font color="#800000">SumInRs</font></td>
<td><input id="result" name="total"</td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
</table>
<input type="submit" name="Register"><input type="reset"
name="reset">
</form>
</body>
</html>
JSP:
try{
String DBuser="root";
String DBpassword="qwerty";
String Connection="jdbc:mysql://localhost:3306/online";
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn=DriverManager.getConnection(Connection,DBuser, DBpassword);
out.println("Database Succesfully connected");
String CustomerName=request.getParameter("cus_name");
String contact=request.getParameter("cus_contact");
long customerCon=Long.parseLong(contact);
String dayCons=request.getParameter("days_con");
int Consumtion=Integer.parseInt(dayCons);
String total =request.getParameter("total");
int totalConsume=Integer.parseInt(total);
String sql = "select (CustomerName) from customer where CustomerContact='"+ customerCon+"'";
java.sql.PreparedStatement st=conn.prepareStatement(sql);
java.sql.PreparedStatement pst=conn.prepareStatement("insert into invoice (CustomerContact ,CustomerName ,LitresConsumed ,TotalSum) values (?,?,?,?)");
pst.setLong(1, customerCon);
pst.setString(2, CustomerName);
pst.setInt(3, Consumtion);
pst.setInt(4, totalConsume);
int i=pst.executeUpdate();
if(i>0){
response.sendRedirect("BillData.jsp");
}else{
response.sendRedirect("addcustomer.jsp");
}
}catch(Exception e){
out.println(e);
e.getMessage();
}
Welcome to SO, I've got some points for you.
I shall very thank full to you if you could send me the code of that function as I don't have any idea about AJAX.
1) That's not how this site works. Please read How do I ask a good question?.
We'll help you to resolve your issues, before that you go to try something from your end.
2) JSP is not the right place to code Java. Please check How to avoid Java code in JSP files?
You should keep your Java code in servlet. When ajax calls a request point it to sevlet and do Java code over there and return expected output.
JSP should be used to render the output.
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String customerContact = request.getParameter("myValue");
String customerName = "Your_Response_From_DB";
response.setContentType("text/plain");
response.getWriter().write(customerName);
}
}
And in your web.xml file,
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
Few corrections in your HTML,
Add ID to Customer Name (Use meaningful names).
<tr>
<td><font color="#800000">Customercontact</font></td>
<td><input name="customer_contact" type="text" id="customer_contact"></td>
</tr>
<tr>
<td><fontcolor="#800000">CustomerName</font></td>
<td><input type="text" name="customer_name" id="customer_name"></td>
</tr>
If you want to return back the same page use jQuery ajax,
download the jQuery plugin add it in your JSP page.
$("#customer_contact").blur(function(e){
var contact = $(this).val();
$.ajax({
type : 'post',
url : 'MyServlet',
data: { myValue: contact},
success : function(data) {
console.log(data);
$("#customer_name").val(data);
}
});
});
Let me know if this helps.
Cheers..!
I am new to thymeleaf and am trying to create an html table where a boolean decides whether the text will be pass or fail in some of the columns.
SmokeTest.passOrFailArray is an array of booleans.
Right now the smokeTest.name is showing up in the column but the passed or failed text is not showing up at all.
Here is my thymeleaf/html code
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Smoke Tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table border="1" style="width:300px">
<tr>
<td>Test Name</td>
<td th:each="testsThatRan : ${testsThatRan}"
th:text="${testsThatRan}">Tests</td>
</tr>
<th:block th:each="smokeTest : ${smokeTests}">
<tr>
<td th:text="${smokeTest.name}">A Smoke Test'</td>
<th:block th:each="smokeTest.passOrFailArray : ${smokeTest.passOrFailArray}">
<td th:if="${smokeTest.passOrFailArray} == true" th:text="Passed"></td>
<td th:if="${smokeTest.passOrFailArray} == false" th:text="failed"></td>
</th:block>
</tr>
</th:block>
</table>
</body>
</html>
Here is the class that im using as a variable in thymeleaf
public testers() throws IOException {
localPath = "/Users/dansbacher14/Documents/workspace/OBI_nightly_test/src/OBI_ci_scripts_tests";
remotePath = "ssh://git#stash.ops.aol.com:2022/obi/obi_ci_scripts.git";
localRepo = new FileRepository(localPath + "/.git");
pathToSmoke = "BPS/GPS/GATHR/SMOKE";
pathToODirectory = "test-results";
git = new Git(localRepo);
}
public static <C> void testClone() throws IOException, InvalidRemoteException, TransportException, GitAPIException
{
Git.cloneRepository().setURI(remotePath).setDirectory(new File(localPath)).call();
}
//____________SETTERS AND GETTERS __________________________________________________________________________________
public void setName(String name)
{
jmxName = name;
}
public String getName()
{
return jmxName;
}
public boolean[] getPassOrFailArray()
{
return passOrFailArray;
}
public String getLocalPath()
{
return localPath;
}
}
This is how the source code is presented by the browser.
<!DOCTYPE HTML>
<html>
<head>
<title>Smoke Tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table border="1" style="width:300px">
<tr>
<td>Test Name</td>
<td>OBI01</td>
<td>DEV</td>
</tr>
<tr>
<td>authAmtTesting.jmx</td>
</tr>
<tr>
<td>authTesting.jmx</td>
</tr>
<tr>
<td>CC_Crypto.jmx</td>
</tr>
<tr>
<td>ci_address.jmx</td>
</tr>
<tr>
<td>ci_cardtype_negative.jmx</td>
</tr>
<tr>
<td>ci_cardtype_positive.jmx</td>
</tr>
<tr>
<td>promoSmokeTst.jmx</td>
</tr>
<tr>
<td>tokenizedPayment.jmx</td>
</tr>
</table>
</body>
</html>
Is it possible to do something like this in thymeleaf? If so how could I make this work? Thanks
This code works
<th:block th:each="pf : ${smokeTest.passOrFailArray}">
<td th:if="${pf} == true" th:text="Passed"></td>
<td th:if="${pf} == false" th:text="failed"></td>
</th:block>
the problem is that i was naming my variable in the each loop incorrectly. The name cannot have a period in it.
Hi i am writing a spring mvc hibernate annotation application there i have 2 tables "team" and another table "players".Here i am using one-to-one mapping and mysql database.In players table i am keeping player records like name,years active etc.The "team" table contains two entries teamid(primary key)and teamname.
The "players" table contains the foreign key teamid.Here when i try to delete a teamname without deleting all its references in players table i am getting error message
HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute update query
so when i delete all the references ,then there is no error.So how can i find all the refrences before deleting.I am interested in showing an alert message using javascript which warns to delete all references.I know how to create simple alert message in javascript,but here i have to show the alert only if there is a reference to foreign key.And i want to know is there any alternate way to delete the foriegn key reference without affecting the "players" table.
Team.java
#Entity
#Table(name="Team")
public class AddTeam {
#Id
#Column(name="teamId")
private Integer teamId;
#Column(name="teamName")
private String teamName;
public Integer getTeamId() {
return teamId;
}
public void setTeamId(Integer teamId) {
this.teamId = teamId;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
}
Player.java
#Entity
#Table(name="playerdata")
public class Player implements Serializable {
private static final long serialVersionUID = -723583058586873479L;
/**
*
*/
#Id
#Column(name="playerid")
private Integer playerId;
#Column(name="playername")
private String playerName;
#Column(name="yearsactive")
private Integer yearsActive;
#Column(name="country")
private Integer Country;
#OneToOne
#JoinColumn(name="teamId")
private AddTeam teams;
public Integer getPlayerId(){
return playerId;
}
public void setPlayerId(Integer playerId){
this.playerId=playerId;
}
public String getPlayerName(){
return playerName;
}
public void setPlayerName(String playerName){
this.playerName=playerName;
}
public String getCountry(){
return Country;
}
public void setCountry(String Country){
this.Country=Country;
}
public Integer getyearsActive(){
return yearsActive;
}
public void setyearsActive(Integer yearsActive){
this.yearsActive=yearsActive;
}
public AddTeam getTeams() {
return teams;
}
public void setTeams(AddTeam teams) {
this.teams = teams;
}
}
and this is the query used in playerDaoImplementation.java class
#Override
public void deleteResource(int playerid) {
// TODO Auto-generated method stub
sessionfactory.getCurrentSession().createQuery("DELETE FROM Resource WHERE playerid=" +playerid).executeUpdate();
}
Query used to delete in AddteamDaoImplementation.java
#Override
public void deleteTeams(int teamid) {
// TODO Auto-generated method stub
sessiofactory.getCurrentSession().createQuery("DELETE FROM AddTeam WHERE teamid="+teamid).executeUpdate();
}
deletefunction in PlayerController.java
#RequestMapping(value="/deletePlayer",method=RequestMethod.GET)
public ModelAndView deletePlayerDetails(#ModelAttribute("command") Player player,
BindingResult result){
playerService.deletePlayer(player.getPlayerId());
Map<String, Object> model = new HashMap<String, Object>();
model.put("playerkey", playerService.listPlayer());
model.put("teamKey", addteamService.listTeams());
return new ModelAndView("EditPlayer",model);
}
Team.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Players Manager</title>
<center>
<h2>Add Team Details</h2>
<form:form method="POST" action="Team.html">
<table>
<tr>
<td><form:label path="teamId">Team ID:</form:label></td>
<td><form:input path="teamId" id="demo" value="${team.teamId}"/></td>
</tr>
<tr>
<td><form:label path="teamName">Team Name:</form:label></td>
<td><form:input path="teamName" value="${team.teamName}"/></td>
</tr>
<tr>
<tr>
<td> </td>
<td><input type="submit" value="SAVE"/></td>
</tr>
</table>
</form:form>
<br/>
<c:if test="${!empty teamKey}">
<table align="center" border="1">
<tr>
<th>Category ID</th>
<th>Category Name</th>
<th>Options</th>
</tr>
<c:forEach items="${teamKey}" var="team">
<tr>
<td><c:out value="${team.teamId}"/></td>
<td><c:out value="${team.teamName}"/></td>
<td align="center">Edit |
Delete</td>
</tr>
</c:forEach>
</table>
</c:if>
<h2>Adding Publication</h2>
</center>
</body>
</html>
EditPlayer.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Resource Manager</title>
</head>
<body>
<center>
<h2>Add Players</h2>
<form:form method="POST" action="save.html">
<table>
<tr>
<td><form:label path="playerId">Player Id</form:label></td>
<td><form:input path="playerId" value= "${player.playerId }"/></td>
</tr>
<tr>
<td><form:label path="playerName">Name</form:label></td>
<td><form:input path="playerName" value="${player.playerName }"/></td>
</tr>
<tr>
<td><form:label path="YearsActive">Experience</form:label></td>
<td><form:input path="YearsActive" value="${player.YearsActive }"/></td>
</tr>
<tr>
<td><form:label path="Country">Date of Join</form:label></td>
<td><form:input path="Country" value="${player.Country }"/></td>
</tr>
<tr>
<td>
<form:label path="teams.teamId">Team Name</form:label>
</td>
<td>
<form:select path="teams.teamId" cssStyle="width: 150px;">
<option value="-1">Select a type</option>
<c:forEach items="${teamKey}" var="teams">
<option value="${teams.teamId}">${teams.teamName}</option>
</c:forEach>
</form:select>
</td>
</tr>
<tr>
<tr>
<td colspan="2"><input type="submit"value="Submit"></td>
</tr>
</table>
</form:form>
<br/>
<c:if test="${!empty playerkey}">
<table align="center" border="1">
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>YearsActive</th>
<th>Country</th>
</tr>
<c:forEach items="${playerkey}" var="player">
<tr>
<td><c:out value="${player.playerId}"/></td>
<td><c:out value="${player.playerName }"/></td>
<td><c:out value="${player.YearsActive}"/></td>
<td><c:out value="${player.Country}"/></td>
<td align="center">Edit | Delete</td>
</tr>
</c:forEach>
</table>
</c:if>
<h2>Adding Team</h2>
</center>
</body>
</html>
please help.
thanks in advance
You have to do select query for teamId in players table to find players and in DB you can put cascade delete in players Table so it will delete players when you delete Team.
I have created a table in oracle which consist of college_names, student_name,regno and result belong to particular college and college_ids of each and every college and date on which the data has been stored...
Now I want to give college_id and date and i want all the student name with their result and regno in a table like structure in a browser I have written one html page for giving college_id and date and one jsp page for retrieving different fields from database but its not working..here is my code for both the pages..
this is my html page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<html>
<head>
<script>
function check()
{
var a=result.college_id.value;
var b=result.date.value;
if(a==""|| b=="")
{
alert("fill the fields")
}
else
{
result.action="result.jsp";
result.method="post";
result.submit();
}
}
</script>
</head>
<body bgcolor="#cc99ff">
<form name="result">
<center>
<table>
<tr>
<td>college_id:</td>
<td><input type="text" name="college_id"></td>
</tr>
<tr>
<td>date:</td>
<td><input type="text" name="date"></td>
</tr>
<tr>
<td>
<input type="Button" value="Submit" onClick="check()">
</td>
</tr>
</table>
</center>
</body>
</html>
Now here is my jsp page for retrieving multiple rows...
<html>
<body background="main_BG.jpg">
<%#page language="java"%>
<%#page import="java.sql.*,java.util.*"%>
<%!
Connection con;
PreparedStatement ps;
ResultSet rs;
String college_id;
String college_name;
String regno;
String student_name;
String result;
String date;
%>
<%
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:orcl","pro","pro
");
college_id=request.getParameter("college_id");
date=request.getParameter("date");
ps=con.prepareStatement("select * from add_result where college_id=? and date=?");
ps.setString(1,college_id);
ps.setString(6,date);
rs=ps.executeQuery();
while(rs.next())
{
college_name=rs.getString(2);
regno=rs.getString(3);
student_name=rs.getString(4);
result=rs.getString(5);
}
else
{
out.println("no data found");
}
}
catch(Exception e)
{
out.println("<center><b><font color=lightblue>some error occured...please try again</font></b></center>");
out.println("<br><br><a href='result.html'><center><b><font color=lightblue>click here to return..</font></b></center>");
}
%>
<center>
<font color=lightblue>
<b>
Information of student with college_id [<%=college_id%>]:
</b>
</font>
</center>
<p style="position:absolute;left:100;top:100">
<table border="2" width="100%">
<th style="color:yellow">
college_name
</th>
<th style="color:yellow">
regno
</th>
<th style="color:yellow">
student_name
</th>
<th style="color:yellow">
result
</th>
<tr>
<td style="color:lightgreen" align="center"><%=college_name%></td>
<td style="color:lightgreen" align="center"><%=regno%></td>
<td style="color:lightgreen" align="center"><%=student_name%></td>
<td style="color:lightgreen" align="center"><%=result%></td>
</tr>
</table>
</p>
</body>
</html>
<%
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:orcl","pro","pro ");
college_id=request.getParameter("college_id");
date=request.getParameter("date");
String sql = "select * from add_result where college_id=? and date=?";
ps=con.prepareStatement(sql);
ps.setString(1,college_id);
//ps.setString(6,date);
ps.setString(2,date);
System.out.println("Sql: " + sql);
rs=ps.executeQuery();
// process resultset below
}
catch(Exception e)
{
out.println("<center><b><font color=lightblue>some error occured...please try again</font></b></center>");
out.println("<br><br><a href='result.html'><center><b><font color=lightblue>click here to return..</font></b></center>");
}
%>
<center>
<font color=lightblue>
<b>
Information of student with college_id [<%=college_id%>]:
</b>
</font>
</center>
<p style="position:absolute;left:100;top:100">
<table border="2" width="100%">
<th style="color:yellow">
college_name
</th>
<th style="color:yellow">
regno
</th>
<th style="color:yellow">
student_name
</th>
<th style="color:yellow">
result
</th>
<%
if(rs!=null){
while(rs.next())
{
college_name=rs.getString(2);
regno=rs.getString(3);
student_name=rs.getString(4);
result=rs.getString(5);
%>
<tr>
<td style="color:lightgreen" align="center"><%=college_name%></td>
<td style="color:lightgreen" align="center"><%=regno%></td>
<td style="color:lightgreen" align="center"><%=student_name%></td>
<td style="color:lightgreen" align="center"><%=result%></td>
</tr>
<%
} // result set loop ends here
}else{
%>
<tr>
<td colspan="4" style="color:red" align="center">No Data Found</td>
</tr>
<%
} //if condition ends here
%>