Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pizzeria-del-SAE
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gwendal Margely
Pizzeria-del-SAE
Commits
7d38fafe
Commit
7d38fafe
authored
1 year ago
by
Gwendal Margely
Browse files
Options
Downloads
Patches
Plain Diff
Add DAO Ingredients
parent
818daa88
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
WEB-INF/src/DAO/DAOIngredients.java
+84
-0
84 additions, 0 deletions
WEB-INF/src/DAO/DAOIngredients.java
WEB-INF/src/POJO/Ingredient.java
+5
-7
5 additions, 7 deletions
WEB-INF/src/POJO/Ingredient.java
with
89 additions
and
7 deletions
WEB-INF/src/DAO/DAOIngredients.java
+
84
−
0
View file @
7d38fafe
package
DAO
;
import
POJO.Ingredient
;
import
java.sql.*
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
DAOIngredients
{
// JDBC URL, username, and password of PostgresSQL server
private
static
final
String
url
=
"jdbc:postgresql://"
;
//CHANGE URL
private
static
final
String
user
=
"gwendalmargelyetu"
;
private
static
final
String
password
=
"moi"
;
// JDBC variables for opening, closing and managing connection
private
static
Connection
connection
;
private
static
PreparedStatement
preparedStatement
;
private
static
ResultSet
resultSet
;
// SQL queries
private
static
final
String
SELECT_ALL_QUERY
=
"SELECT * FROM ingredients"
;
private
static
final
String
INSERT_QUERY
=
"INSERT INTO ingredients(name, price) VALUES (?, ?)"
;
private
static
final
String
SELECT_BY_ID_QUERY
=
"SELECT * FROM ingredients WHERE id = ?"
;
// Method to establish database connection
private
static
void
connect
()
throws
SQLException
{
connection
=
DriverManager
.
getConnection
(
url
,
user
,
password
);
}
// Method to close database connection
private
static
void
close
()
throws
SQLException
{
if
(
resultSet
!=
null
)
{
resultSet
.
close
();
}
if
(
preparedStatement
!=
null
)
{
preparedStatement
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
close
();
}
}
// Method to retrieve all ingredients from the database
public
static
List
<
Ingredient
>
getAllIngredients
()
throws
SQLException
{
List
<
Ingredient
>
ingredients
=
new
ArrayList
<>();
connect
();
preparedStatement
=
connection
.
prepareStatement
(
SELECT_ALL_QUERY
);
resultSet
=
preparedStatement
.
executeQuery
();
while
(
resultSet
.
next
())
{
int
id
=
resultSet
.
getInt
(
"id"
);
String
name
=
resultSet
.
getString
(
"name"
);
double
price
=
resultSet
.
getDouble
(
"price"
);
Ingredient
ingredient
=
new
Ingredient
(
id
,
name
,
price
);
ingredients
.
add
(
ingredient
);
}
close
();
return
ingredients
;
}
// Method to insert a new ingredient into the database
public
static
void
insertIngredient
(
Ingredient
ingredient
)
throws
SQLException
{
connect
();
preparedStatement
=
connection
.
prepareStatement
(
INSERT_QUERY
);
preparedStatement
.
setString
(
1
,
ingredient
.
getName
());
preparedStatement
.
setDouble
(
2
,
ingredient
.
getPrice
());
preparedStatement
.
executeUpdate
();
close
();
}
// Method to retrieve an ingredient by its ID
public
static
Ingredient
getIngredient
(
int
id
)
throws
SQLException
{
connect
();
preparedStatement
=
connection
.
prepareStatement
(
SELECT_BY_ID_QUERY
);
preparedStatement
.
setInt
(
1
,
id
);
resultSet
=
preparedStatement
.
executeQuery
();
Ingredient
ingredient
=
null
;
if
(
resultSet
.
next
())
{
String
name
=
resultSet
.
getString
(
"name"
);
double
price
=
resultSet
.
getDouble
(
"price"
);
ingredient
=
new
Ingredient
(
id
,
name
,
price
);
}
close
();
return
ingredient
;
}
}
This diff is collapsed.
Click to expand it.
WEB-INF/src/POJO/Ingredient.java
+
5
−
7
View file @
7d38fafe
...
...
@@ -3,18 +3,16 @@ package POJO;
public
class
Ingredient
{
public
int
id
;
private
String
name
;
private
Pric
e
price
;
private
doubl
e
price
;
public
Ingredient
(
int
id
,
String
name
,
Pric
e
price
)
{
public
Ingredient
(
int
id
,
String
name
,
doubl
e
price
)
{
this
.
id
=
id
;
this
.
name
=
name
;
this
.
price
=
price
;
}
public
Ingredient
(
int
id
,
String
name
)
{
this
.
id
=
id
;
this
.
name
=
name
;
this
.
price
=
new
Price
();
this
(
id
,
name
,
0.00
);
}
public
int
getId
()
{
...
...
@@ -33,11 +31,11 @@ public class Ingredient {
this
.
name
=
name
;
}
public
Pric
e
getPrice
()
{
public
doubl
e
getPrice
()
{
return
price
;
}
public
void
setPrice
(
Pric
e
price
)
{
public
void
setPrice
(
doubl
e
price
)
{
this
.
price
=
price
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment