Skip to content
Snippets Groups Projects
Commit 65e26ebe authored by Kelvez's avatar Kelvez
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 355 additions and 0 deletions
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="productdb@localhost" uuid="3aa5152e-d3de-4ae4-a973-132693772dcc">
<driver-ref>mysql.8</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://localhost:3306/productdb</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/cart.iml" filepath="$PROJECT_DIR$/cart.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>
\ No newline at end of file
File added
File added
File added
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="shopping.cart" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
File added
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/7.4/userguide/building_java_projects.html
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13.2'
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.1.1-jre'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
implementation 'mysql:mysql-connector-java:8.0.28'
}
application {
// Define the main class for the application.
mainClass = 'shopping.cart.App'
}
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="productdb@localhost" uuid="3aa5152e-d3de-4ae4-a973-132693772dcc">
<driver-ref>mysql.8</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://localhost:3306/productdb</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/cart.iml" filepath="$PROJECT_DIR$/cart.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>
\ No newline at end of file
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package shopping.cart;
import java.sql.Timestamp;
public class App {
public static void main(String[] args) {
ProductManagement pm = new ProductManagement();
pm.printProductByName("testz");
pm.printProductById(2);
pm.printAllProducts();
}
}
package shopping.cart;
import java.sql.Timestamp;
public class Product {
private int id;
private String name;
private double price;
private java.sql.Timestamp created;
public Product(int id, String name, double price, java.sql.Timestamp created) {
if (name.length() > 100) {
System.err.println("ERREUR : name too long (>100 char)");
} else {
this.name = name;
}
this.id = id;
this.price = price;
this.created = created;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
if (name.length() > 100) {
System.err.println("ERREUR : name too long (>100 char)");
} else {
this.name = name;
}
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Timestamp getCreated() {
return created;
}
public void setCreated(Timestamp created) {
this.created = created;
}
public void print() {
System.out.println("");
}
}
package shopping.cart;
import java.sql.*;
public class ProductManagement {
private Connection con;
private Statement stmt;
public ProductManagement() {
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/productdb?user=root&password=");
stmt = con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
con = null;
}
}
public void insertProduct(Product prod) {
try {
String sql = "INSERT INTO products VALUES (?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, prod.getId());
pstmt.setString(2, prod.getName());
pstmt.setDouble(3, prod.getPrice());
pstmt.setTimestamp(4, prod.getCreated());
pstmt.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
public void printAllProducts() {
try {
ResultSet resultats = stmt.executeQuery("SELECT * FROM products");
this.printResultats(resultats);
} catch (Exception e) {
e.printStackTrace();
}
}
public void printProductByName(String name) {
try {
ResultSet resultats = stmt.executeQuery("SELECT * FROM products WHERE name = '"+name+"'");
this.printResultats(resultats);
} catch (Exception e) {
e.printStackTrace();
}
}
public void printProductById(int id) {
try {
ResultSet resultats = stmt.executeQuery("SELECT * FROM products WHERE id = '"+id+"'");
this.printResultats(resultats);
} catch (Exception e) {
e.printStackTrace();
}
}
private void printResultats(ResultSet resultats) {
try {
ResultSetMetaData rsmd = resultats.getMetaData();
int nbCols = rsmd.getColumnCount();
boolean encore = resultats.next();
if (!encore) {
System.out.println("No results");
}
while (encore) {
for (int i = 1; i <= nbCols; i++)
System.out.print(resultats.getString(i) + "\t");
System.out.println();
encore = resultats.next();
}
resultats.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteProductById(int id) {
try {
String sql = "DELETE FROM products WHERE id = " + id;
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteProductByName(String name) {
try {
String sql = "DELETE FROM products WHERE name = '" + name + "'";
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateProductById(int id, String newName, double newPrice, Timestamp newCreated) {
if (newName != null) {
try {
String sql = "UPDATE products SET name = '" + newName + "' WHERE id = "+id;
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
if (newPrice >= 0) {
try {
String sql = "UPDATE products SET price = '" + newPrice + "' WHERE id = "+id;
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
if (newCreated != null) {
try {
String sql = "UPDATE products SET created = '" + newCreated + "' WHERE id = "+id;
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="shopping.cart" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment