0% found this document useful (0 votes)
101 views30 pages

Java With SQL Sever

This document outlines an MVC program connecting to a SQL Server database. It includes: 1) Model classes like SQLServer and Personaj to connect to the database and represent characters. 2) A List class to retrieve and manage character data from the database. 3) JSP files including an Index page to display characters and a control page to modify data. The program uses classes to connect to a SQL database, represent character data, and manage retrieving and modifying that data to display on JSP pages.

Uploaded by

Trung Ng
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
101 views30 pages

Java With SQL Sever

This document outlines an MVC program connecting to a SQL Server database. It includes: 1) Model classes like SQLServer and Personaj to connect to the database and represent characters. 2) A List class to retrieve and manage character data from the database. 3) JSP files including an Index page to display characters and a control page to modify data. The program uses classes to connect to a SQL database, represent character data, and manage retrieving and modifying that data to display on JSP pages.

Uploaded by

Trung Ng
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 30

Ministerul Educaţiei al Republicii Moldova

Universitatea de Stat din Moldova


Facultatea de Matematică şi Informatică
Departamentul “Informatica »

Dare de seama la „Java”

Lucrare de laborator № 5

Efectuat: studentul (-a) grupei IA1501

numele, prenumele Ho Ngoc Trung

Verificat: lect.univers., magistru in inform.

Epifanova Irina .

- Chişinău 2017
Conditiile:

Elaborarea un MVC program legatură cu Baza de date

PROGRAM:

Structura program:
BAZA DE DATE SQLSERVER
Model Classes:

SQLServer.java:

package Models;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SQLServer {

private Connection conect;


private static SQLServer instance;

public Connection getConect() {


return conect;
}
SQLServer() throws SQLException
{
String hostName = "localhost";
String sqlInstanceName = "SQLEXPRESS";
String database = "Personaj";
String userName = "sa";
String password = "aye123456";
String connectionURL = "jdbc:jtds:sqlserver://" + hostName + "/"
+ database + ";instance=" + sqlInstanceName;
conect = DriverManager.getConnection(connectionURL, userName,
password);

}
public static SQLServer getInstance() throws SQLException {
if (instance == null) {
instance = new SQLServer();
}
return instance;
}
}
Personaj.java

package Models;

import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Random;

public class Personaj {

String name;
String pass;
String email;
String rasa;
String skill[];
String weapon;
int hp;
String img;

public Personaj(String name, String pass, String email, String rasa, String[] skill, String weapon, int hp, String
img) {
this.name = name;
this.pass = pass;
this.email = email;
this.rasa = rasa;
this.skill = skill;
this.weapon = weapon;
this.hp = hp;
this.img = img;
}

public Personaj(Personaj p) {
this.name = p.name;
this.pass = p.pass;
this.email = p.email;
this.rasa = p.rasa;
this.skill = p.skill;
this.weapon = p.weapon;
this.hp = p.hp;
this.img = p.img;
}

public Personaj() {

}
public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}

public String getRasa() {


return rasa;
}

public void setRasa(String rasa) {


this.rasa = rasa;
}

public String[] getSkill() {


return skill;
}

public void setSkill(String[] skill) {

this.skill = new String[skill.length];


for (int i = 0; i < skill.length; i++) {
this.skill[i] = skill[i];
}
}

public String getWeapon() {


return weapon;
}

public void setWeapon(String weapon) {


this.weapon = weapon;
}

public int getHp() {


return hp;
}

public void setHp(int hp) {


this.hp = hp;
}
public String getPass() {
return pass;
}

public void setPass(String pass) {


this.pass = pass;
}

public String getImg() {


return img;
}

public void setImg(String img) {


this.img = img;
}

public void inDatabase() throws SQLException // Functia pentru inscrie un personaj in Database
{
String skill_val=new String();
for (int i = 0; i < skill.length; i++) {
skill_val=skill_val.concat(" "+skill[i]);
}
Statement statement = SQLServer.getInstance().getConect().createStatement();
String sql="INSERT INTO personaj (name,pass,email,rasa,skill,weapon,hp,img) VALUES "
+ "('"+name+"',"
+ "'"+pass+"',"
+ "'"+email+"',"
+ "'"+rasa+"',"
+ "'"+skill_val+"',"
+ "'"+weapon+"',"
+ "'"+hp+"',"
+ "'"+img+"')";

int c_row=statement.executeUpdate(sql);

}
List.java:

package Models;

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class List {

private static List instance;


public ArrayList<Personaj> lista;

private List() {
lista = new ArrayList<Personaj>();
}

public static List getInstance() {


if (instance == null) {
instance = new List();
}
return instance;
}
//Functia lua datele din fisierul si salveaza in ArrayList cand lansa program prima data, Apoi program lucreaza cu
ArrayList

public void get_data() throws SQLException {

lista = new ArrayList<Personaj>();


Statement statement = SQLServer.getInstance().getConect().createStatement();

String sql = "Select * from personaj";


ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
String name = rs.getString(2);
String pass = rs.getString(3);
String mail = rs.getString(4);
String rasa = rs.getString(5);

String str = rs.getString(6);


String str2 = str.trim(); // eliminate caracterele spatiul la inceputul sau sfarsitul sirului
String[] skill = str2.split(" "); //pune fiecare cuvint intr-o variabila in array

String weapon = rs.getString(7);


int hp = rs.getInt(8);
String img = rs.getString(9);
Personaj p = new Personaj(name, pass, mail, rasa, skill, weapon, hp, img);
List.getInstance().add(p);
}

public ArrayList<Personaj> getLista() {


return lista;
}

//Functia adauga un personaj


public void add(Personaj new_per) {

List.getInstance().lista.add(new_per);

}
//Functia sterge un personaj

public void delete(String nume,int index) throws Exception {


if (List.getInstance().lista != null) {
/*for(Personaj p:List.getInstance().lista)
{ if(p.getName().equals(nume))
Personaj remove=new Personaj();
List.getInstance().lista.remove(p);
}*/
List.getInstance().lista.remove(index);
String query = "delete from personaj where name='" + nume + "'";
Statement statement = SQLServer.getInstance().getConect().createStatement();
int row = statement.executeUpdate(query);

}
}
//Functia modificare un personaj

public void edit(String oldname, String name, String pass, String mail, String rasa, String[] skill, String
weapon, int hp, String img) {
try {

String skill_2 = new String();


for (int i = 0; i < skill.length; i++) {
skill_2 = skill_2.concat(skill[i] + " ");
}

String query = "update personaj set "


+ "name='" + name + "'"
+ ",pass='" + pass + "'"
+ ",rasa='" + rasa + "'"
+ ",skill='" + skill_2 + "'"
+ ",weapon='" + weapon + "'"
+ ",hp='" + hp + "'"
+ ",img='" + img + "'"
+" where name='"+oldname+"'";

Statement statement = SQLServer.getInstance().getConect().createStatement();


int row = statement.executeUpdate(query);
} catch (SQLException ex) {
Logger.getLogger(List.class.getName()).log(Level.SEVERE, null, ex);
}

JSP files:

Index.jsp:

<%@page import="Models.Personaj"%>
<%@page import="Models.List"%>
<%@ page errorPage="error-page.jsp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Da Nang</title>
<link rel="stylesheet" type="text/css" href="css/danag.css"/>

</head>

<body>
<div class="wrapper">
<div class="contain">

<div class="noidung">
<div class="tour">
<div>.</div>
<h1> Lista de Personaj: </h1>
<% List.getInstance().getInstance().get_data();%>
<%for(Personaj p:List.getInstance().lista)
{
%>

<img id="img_java" src="<%=p.getImg()%>" /> <br />


<h2>Name: <%=p.getName()%> </h2>
<h2>Rasa: <%=p.getRasa()%> </h2>
<h2> SKILLS: <%for(String skill:p.getSkill()) { %>
<%=skill%> --

<%}%>

</h2>
<h2>Weapon: <%=p.getWeapon()%> </h2>
<h2>HP: <%=p.getHp()%> </h2>
<br />

<br />.<br />

<br />
<br /><br />

<% }%>
</div>
<div class="letrai">
<div class="reg">
<h1> MODIFICATION </h1>

<form action="control.jsp">
<br/>

<h3> </h3>

<input " name="choice" type="radio" value="Add" />


Add<br />
<input name="choice" type="radio" value="Delete" />
Delete<br />
<input name="choice" type="radio" value="Edit" />
Edit<br />
<input name="choice" type="radio" value="Battle" />
Battle<br />
<br />
<br />
<input type="hidden" name="img_java_load" id="img_java_id" value="asd"/>
<button type="submit" value="submit" > OK </button>
</form>
<br />
</div>
</div>
</div>
</div>
</div>
</body>
</html>

control.jsp:
<%@ page errorPage="error-page.jsp"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<% String res=request.getParameter("choice");

%>

<% if(res.equals("Add"))
{
%>
<jsp:forward page="add-form.jsp" />
<% } %>

<% if(res.equals("Delete"))
{
%>
<jsp:forward page="delete-form.jsp" />
<% } %>

<% if(res.equals("Edit"))
{
%>
<jsp:forward page="edit-form.jsp" />
<% } %>
<% if(res.equals("Battle"))
{
%>
<jsp:forward page="battle-form.jsp" />
<% } %>
add-form.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Da Nang</title>
<link rel="stylesheet" type="text/css" href="css/danag.css"/>

<script>
function veri()
{

var age = document.getElementsByName("age");


document.getElementById("tuoi").innerHTML = "HP: " + age[0].value;
var obj = document.getElementsByName("rasa");
if (obj[0].options[0].selected)
{
document.getElementById("img_java").src = "http://cdn2.game4v.com/2015/06/edjpa.jpg";
}
if (obj[0].options[1].selected)
{
document.getElementById("img_java").src = "http://lh4.ggpht.com/-3HdFQn54TC4/VK-
XArmoL7I/AAAAAAAAA8Y/ofLcrG2Tgvo/s1600/nhung-vi-tuong-khac-che-tot-yasuo-trong-lol.jpg";
}
if (obj[0].options[2].selected)
{
document.getElementById("img_java").src =
"http://euw.leagueoflegends.com/sites/default/files/styles/scale_xlarge/public/upload/ashe_splash-
1920x1080.jpg?itok=F2SIq9a5";
}

var skill = document.getElementsByName("skill");


if (skill[0].checked == true)
{
document.getElementById("skill1").src =
"https://s.blogcdn.com/massively.joystiq.com/media/2013/01/lol-leonashield.jpg";
} else
{
document.getElementById("skill1").src =
"https://vignette.wikia.nocookie.net/clubpenguin/images/5/5f/Red_X.png/revision/latest?cb=20120514130731";
}

if (skill[1].checked == true)
{
document.getElementById("skill2").src = "http://1.bp.blogspot.com/-
vLc1Z_Gkm7s/UuDqnPOomAI/AAAAAAAACJI/rUL1uBrhvB4/s0/ashe-ultimate-skill-ss-league-of-legends-
splash-skin-hd-wallpaper-jochi_pochi-1920x1080.jpg";
} else
{
document.getElementById("skill2").src =
"https://vignette.wikia.nocookie.net/clubpenguin/images/5/5f/Red_X.png/revision/latest?cb=20120514130731";
}
if (skill[2].checked == true)
{
document.getElementById("skill3").src = "https://i.ytimg.com/vi/davUGVu0lso/maxresdefault.jpg";
} else
{
document.getElementById("skill3").src =
"https://vignette.wikia.nocookie.net/clubpenguin/images/5/5f/Red_X.png/revision/latest?cb=20120514130731";
}

var weapon = document.getElementsByName("weapon");


if (weapon[0].checked == true)
{
document.getElementById("weap1").src =
"https://vignette.wikia.nocookie.net/finalfantasy/images/f/f9/FFXI_Sword_70.png/revision/latest?cb=2012071
3070141";
} else
{
document.getElementById("weap1").src =
"https://vignette.wikia.nocookie.net/clubpenguin/images/5/5f/Red_X.png/revision/latest?cb=20120514130731";
}

if (weapon[1].checked == true)
{
document.getElementById("weap2").src = "https://www.archery360.com/wp-
content/uploads/2016/05/compound-bows.jpg";
} else
{
document.getElementById("weap2").src =
"https://vignette.wikia.nocookie.net/clubpenguin/images/5/5f/Red_X.png/revision/latest?cb=20120514130731";
}

document.getElementById("img_java_id").value = document.getElementById("img_java").src;

</script>
</head>

<body>
<div class="wrapper">
<div class="facebook"> <a href=""> <img src="image/home/fb.png" /> </a> <a href=""><img
src="image/home/youtube.png" /></a> <a href=""><img src="image/home/twit.png" /></a> </div>
<div class="contain">
<div class="noidung">
<div class="tour">
<div>.</div>
<h1> CHAMPION: </h1>
<img id="img_java" src="http://i.imgur.com/gXzK0eL.jpg" /> <br />
<br />
<br />
<br />
<br />
<h1> SKILLS: </h1>
<br />
<img id="skill1"
src="https://vignette.wikia.nocookie.net/clubpenguin/images/5/5f/Red_X.png/revision/latest?cb=201205141307
31" />
<img id="skill2"
src="https://vignette.wikia.nocookie.net/clubpenguin/images/5/5f/Red_X.png/revision/latest?cb=201205141307
31" />
<img id="skill3"
src="https://vignette.wikia.nocookie.net/clubpenguin/images/5/5f/Red_X.png/revision/latest?cb=201205141307
31" />
<br />.<br />
<h1> WEAPONS: </h1>
<br />
<img id="weap1"
src="https://vignette.wikia.nocookie.net/clubpenguin/images/5/5f/Red_X.png/revision/latest?cb=201205141307
31" />
<img id="weap2"
src="https://vignette.wikia.nocookie.net/clubpenguin/images/5/5f/Red_X.png/revision/latest?cb=201205141307
31" />
<br />
<br />
<br /><br />

</div>
<div class="letrai">
<div class="reg">
<h1>Add </h1>

<form action="add-action.jsp">
<br/>
<input type="text" id="name" name="Name" placeholder="Your Name" onchange="veri()" />
<input type="text" name="mail" placeholder="Your Email" />
<input type="password" id="pas" name="Pass1" placeholder="Your Passwords"
onchange="veri()" />
<input type="password" id="pas" name="Pass2" placeholder="Repeat Passwords"
onchange="veri()" />

<h3 id="tuoi">Age: </h3><input type="range" name="age" min="10" max="100"


onchange="veri()" />

<br/>
<h3> Rasa: </h3>
<select name="rasa" onchange="veri()">
<option value="Magic" >Magic</option>
<option value="Assasin">Assasin</option>
<option value="Tintasi">Tintasi</option>
</select>
<h3> Alege Skill: </h3>
<input onchange="veri()" type="checkbox" name="skill" value="Speed"/>
Speed <br/>
<input onchange="veri()" type="checkbox" name="skill" value="Blaze"/>
Blaze <br/>
<input onchange="veri()" type="checkbox" name="skill" value="Bravado"/>
Baravado <br/>
<h3> Alege Weapon: </h3>
<input onchange="veri()" name="weapon" type="radio" value="Sabie" />
Sabie<br />
<input onchange="veri()" name="weapon" type="radio" value="Arc" />
Arc<br />
<br />
<br />

<input type="hidden" name="img_java_load" id="img_java_id" value="asd"/>

<button type="submit" value="submit" onclick="veri()"> GENERATE </button>


</form>
<br />
</div>
</div>

</div>
</div>

</div>
</body>
</html>
add-action.jsp:

<%@ page errorPage="error-page.jsp"%>

<%@page import="Models.List"%>
<%@page import="Models.Personaj"%>
<h1>Creating.. </h1>
<% String img=new String("");
String name = request.getParameter("Name");
String pass = request.getParameter("Pass1");
String mail = request.getParameter("mail");
String rasa = request.getParameter("rasa");
String[] skill = request.getParameterValues("skill");
String weapon = request.getParameter("weapon");
int hp = Integer.parseInt(request.getParameter("age"));
if(rasa.equals("Magic"))
img="http://cdn2.game4v.com/2015/06/edjpa.jpg";
if(rasa.equals("Assasin")
)
img="http://lh4.ggpht.com/-3HdFQn54TC4/VK-XArmoL7I/AAAAAAAAA8Y/ofLcrG2Tgvo/s1600/nhung-
vi-tuong-khac-che-tot-yasuo-trong-lol.jpg";
if(rasa.equals("Tintasi"))
img="http://euw.leagueoflegends.com/sites/default/files/styles/scale_xlarge/public/upload/ashe_splash-
1920x1080.jpg?itok=F2SIq9a5";

Personaj new_per=new Personaj(name, pass, mail, rasa, skill, weapon, hp, img);
List.getInstance().add(new_per);
new_per.inDatabase();

%>
<meta http-equiv="refresh" content="1;index.jsp">
delete-form.jsp:

<%@ page errorPage="error-page.jsp"%>


<%@page import="Models.Personaj"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="Models.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>DELETE</title>
<style>
body { background:url("http://i.imgur.com/gXzK0eL.jpg"); background-size:100%; background-
attachment:fixed; font-size:100%; }
.content{background-color:white; width:90%; margin:40px auto; height:auto; border-radius:20px}
h1,h2,h3 { text-align: center; }
table{ width:80%; margin:50px auto; font-size:120%; border-radius:20px; border:inset; }
table td,th { text-align:center; padding:7px 5px; height:50px; border-color:#630 }
input{ margin:10px 10%; border-radius:10px; height:40px; width:80%; border-style: groove; border-
radius:10px; }
button { border-radius:10px; border-radius:10px; border-color:red; width:40%; height:40px; background-
color:#06F; font-weight:bold; color:#FFF; }
</style>
</head>
<body>
<div class ="content" >

<br/>
<form action="delete-action.jsp">
<table>

<tr>
<th width="100px"> Delete</th>
<th> Personaj </th>
</tr>
<% int i = 0; %>
<% for (Personaj p : List.getInstance().lista) {
%>
<tr>
<td> <input type="radio" name="delete" value="<%=i%>" />
<td> <h2 style="color:blue"> <%= p.getName()%> </h2>
<input type="hidden" name="name" value="<%= p.getName()%>" />
</td>
</tr>
<% i++; }%>
<tr>
<th >
<input type="submit" value="DELETE"/>
</th>
</tr>
</table>
</form>
<br/>
</div>
</body>
</html>

delete-action.jsp:

<%@ page errorPage="error-page.jsp"%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="Models.List"%>
<%@page import="Models.Personaj"%>
<h1> Delete...</h1>
<%
int index= Integer.parseInt(request.getParameter("delete"));
String name=request.getParameter("name");
List.getInstance().delete(name,index);

%>
<meta http-equiv="refresh" content="2;index.jsp">

edit-form.jsp:

<%@ page errorPage="error-page.jsp"%>


<%@page import="Models.Personaj"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="Models.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>DELETE</title>
<style>
body { background:url("http://i.imgur.com/gXzK0eL.jpg"); background-size:100%; background-
attachment:fixed; font-size:100%; }
.content{background-color:white; width:90%; margin:40px auto; height:auto; border-radius:20px}
h1,h2,h3,h4 { text-align: center; }
table{ width:80%; margin:50px auto; font-size:120%; border-radius:20px; border:inset; }
table td,th { text-align:center; padding:7px 5px; height:50px; border-color:#630 }
input{ margin:10px 10%; border-radius:10px; height:40px; width:80%; border-style: groove; border-
radius:10px; }
button { border-radius:10px; border-radius:10px; border-color:red; width:40%; height:40px; background-
color:#06F; font-weight:bold; color:#FFF; }
select { border-color:red; margin:10px 30%; width:40%; height:40px; background-color:red; font-
weight:bold; color:#FFF; }

</style>
</head>
<body>
<div class ="content" >

<br/>
<form action="edit-action.jsp">
<table>

<tr>
<th width="100px"> Edit</th>
<th> Personaj </th>
</tr>
<% int i = 0; %>
<% for (Personaj p : List.getInstance().lista) {

%>

<tr>
<td> <input type="radio" name="edit" value="<%=p.getName()%>" /> </td>
<td> <h2 style="color:blue"> <%= p.getName()%> </h2> </td>

</tr>

<% i++;
}%>

<tr>
<td colspan="2">
<input type="text" id="name" name="Name" placeholder="Your Name" />
<input type="text" name="mail" placeholder="Your Email" />
<input type="password" id="pas" name="Pass1" placeholder="Your Passwords" />
<input type="password" id="pas" name="Pass2" placeholder="Repeat Passwords" />

<h3 id="tuoi">HP: </h3><input type="range" name="age" min="10" max="100" />

<br/>
<h3> Rasa: </h3>
<select name="rasa" >
<option value="Magic" >Magic</option>
<option value="Assasin">Assasin</option>
<option value="Tintasi">Tintasi</option>
</select>
<h3> Alege Skill: </h3>
<input onchange="veri()" type="checkbox" name="skill" value="Speed"/>
<h4>Speed</h4> <br/>
<input onchange="veri()" type="checkbox" name="skill" value="Blaze"/>
<h4>Blaze</h4> <br/>
<input onchange="veri()" type="checkbox" name="skill" value="Bravado"/>
<h4>Baravado</h4> <br/>
<h3> Alege Weapon: </h3>
<input name="weapon" type="radio" value="Sabie" />
<h4>Sabie</h4><br />
<input name="weapon" type="radio" value="Arc" />
<h4>Arc</h4><br />
<br />
<br />
</td>
</tr>
<tr>
<th >
<input type="submit" value="EDIT"/>
</th>

</tr>
</table>
</form>

<br/>

</div>

</body>
</html>

edit-action.jsp:

<%--
Document : delete-action
Created on : Nov 7, 2017, 4:01:35 PM
Author : TrungNg
--%>
<%@ page errorPage="error-page.jsp"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="Models.List"%>
<%@page import="Models.Personaj"%>
<h1> Updating...</h1>
<%

String img = new String("");


String name = request.getParameter("Name");
String pass = request.getParameter("Pass1");
String mail = request.getParameter("mail");
String rasa = request.getParameter("rasa");
String[] skill = request.getParameterValues("skill");
String weapon = request.getParameter("weapon");
int hp = Integer.parseInt(request.getParameter("age"));
if (rasa.equals("Magic")) {
img = "http://cdn2.game4v.com/2015/06/edjpa.jpg";
}
if (rasa.equals("Assasin")) {
img = "http://lh4.ggpht.com/-3HdFQn54TC4/VK-XArmoL7I/AAAAAAAAA8Y/ofLcrG2Tgvo/s1600/nhung-
vi-tuong-khac-che-tot-yasuo-trong-lol.jpg";
}
if (rasa.equals("Tintasi")) {
img = "http://euw.leagueoflegends.com/sites/default/files/styles/scale_xlarge/public/upload/ashe_splash-
1920x1080.jpg?itok=F2SIq9a5";
}
String oldname=request.getParameter("edit");
List.getInstance().edit(oldname, name, pass, mail, rasa, skill, weapon, hp, img);

%>
<meta http-equiv="refresh" content="2;index.jsp">
battle-form.jsp:

<%@ page errorPage="error-page.jsp"%>


<%@page import="Models.Personaj"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="Models.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>DELETE</title>
<style>
body { background:url("http://i.imgur.com/gXzK0eL.jpg"); background-size:100%; background-
attachment:fixed; font-size:100%; }
.content{background-color:white; width:90%; margin:40px auto; height:auto; border-radius:20px}
h1,h2,h3 { text-align: center; }
table{ width:80%; margin:50px auto; font-size:120%; border-radius:20px; border:inset; }
table td,th { text-align:center; padding:7px 5px; height:50px; border-color:#630 }
input{ margin:10px 10%; border-radius:10px; height:40px; width:80%; border-style: groove; border-
radius:10px; }
button { border-radius:10px; border-radius:10px; border-color:red; width:40%; height:40px; background-
color:#06F; font-weight:bold; color:#FFF; }

</style>
</head>
<body>
<div class ="content" >
<br/>
<form action="battle-action.jsp">
<table>

<tr>
<th width="100px"> Battle</th>
<th> Personaj </th>
</tr>
<% int i = 0; %>
<% for (Personaj p : List.getInstance().lista) {
%>
<tr>
<td> <input type="checkbox" name="battle" value="<%=i%>" />
<td> <h2 style="color:blue"> <%= p.getName()%> </h2> </td>
</tr>
<% i++; }%>
<tr>
<th >
<input type="submit" value="BATTLE"/>
</th>
</tr>
</table>
</form>
<br/>
</div>
</body>
</html>

battle-action.jsp:

<%@page import="Models.List"%>
<%@page import="Models.Personaj"%>
<%@ page errorPage="error-page.jsp"%>
<% String index[] = request.getParameterValues("battle");

int n=index.length;
Personaj winner=new Personaj();
int hp_max=0;
for(int i =0;i<n;i++)
{ int hp=List.getInstance().lista.get(Integer.parseInt(index[i])).getHp();
if(hp>hp_max)
{
hp_max=hp;
winner=new Personaj(List.getInstance().lista.get(Integer.parseInt(index[i])));
}
}

session.setAttribute("winner", winner);
%>

<jsp:forward page="battle-result.jsp" />


battle-result.jsp:

<%@ page errorPage="error-page.jsp"%>


<%@page import="Models.Personaj"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="Models.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>DELETE</title>
<style>
body { background:url("http://i.imgur.com/gXzK0eL.jpg"); background-size:100%; background-
attachment:fixed; font-size:100%; }
.content{background-color:white; width:90%; margin:40px auto; height:auto; border-radius:20px}
h1,h2,h3 { text-align: center; }
img{width:50%;height:500px;margin-left:25%;}
a{font-size: 140%;font-weight: bold; text-align: center; color: blue; text-decoration: none}

</style>
</head>
<body>
<div class ="content" >
<%
Personaj winner = ((Personaj) session.getAttribute("winner"));

%>
<br /><br />
<h1 style="color:red;" > The Winner is: </h1>
<img id="img_java" src="<%=winner.getImg()%>" /> <br />
<h2>Name: <%=winner.getName()%> </h2>
<h2>Rasa: <%=winner.getRasa()%> </h2>
<h2> SKILLS: <%for(String skill:winner.getSkill()) { %>
<%=skill%> --

<%}%>

</h2>
<h2>Weapon: <%=winner.getWeapon()%> </h2>
<h2>HP: <%=winner.getHp()%> </h2>
<br />

<br /><br />


<h1> <a href="index.jsp" >Go to Index</a> </h1>
<br />
<br /><br />

</div>

</body>
</html>

error-page.jsp:

<%@page import="Models.Personaj"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="Models.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page isErrorPage="true" %>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ERROR</title>
<style>
body { background:url("http://i.imgur.com/gXzK0eL.jpg"); background-size:100%; background-
attachment:fixed; font-size:100%; }
.content{background-color:white; width:90%; margin:40px auto; height:1024px; border-radius:20px}
h1,h2,h3 { text-align: center; }
img{width:50%;height:500px;margin-left:25%;}
a{font-size: 140%;font-weight: bold; text-align: center; color: blue; text-decoration: none}

</style>
</head>
<body>
<div class ="content" >
<h1>Opps...</h1>
<h2>Sorry, an error occurred.</h2>
<h2>Here is the exception : </h2>
<h2>
<%=exception%>
</h2>
</div>

</body>
</html>
Rezultatul:

Index.jsp
add-form.jsp

Rezultatul dupa a adaugat:


Si datele se duce la baza de date:

delete-form.jsp
Datele la baze de date dupa delete:

edit-form.jsp
Datele dupa Edit:

You might also like