Java With SQL Sever
Java With SQL Sever
Lucrare de laborator № 5
Epifanova Irina .
- Chişinău 2017
Conditiile:
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 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;
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 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;
private List() {
lista = new ArrayList<Personaj>();
}
List.getInstance().lista.add(new_per);
}
//Functia sterge un personaj
}
}
//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 {
JSP files:
Index.jsp:
<%@page import="Models.Personaj"%>
<%@page import="Models.List"%>
<%@ page errorPage="error-page.jsp"%>
</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)
{
%>
<%}%>
</h2>
<h2>Weapon: <%=p.getWeapon()%> </h2>
<h2>HP: <%=p.getHp()%> </h2>
<br />
<br />
<br /><br />
<% }%>
</div>
<div class="letrai">
<div class="reg">
<h1> MODIFICATION </h1>
<form action="control.jsp">
<br/>
<h3> </h3>
control.jsp:
<%@ page errorPage="error-page.jsp"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
%>
<% 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:
<script>
function veri()
{
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";
}
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()" />
<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 />
</div>
</div>
</div>
</body>
</html>
add-action.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:
<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:
%>
<meta http-equiv="refresh" content="2;index.jsp">
edit-form.jsp:
<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" />
<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>
<%
%>
<meta http-equiv="refresh" content="2;index.jsp">
battle-form.jsp:
<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);
%>
<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 />
</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
delete-form.jsp
Datele la baze de date dupa delete:
edit-form.jsp
Datele dupa Edit: