Skip to content

Commit 5d4e1c0

Browse files
authored
Add files via upload
0 parents  commit 5d4e1c0

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

DBConnection.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.sql.Connection;
2+
import java.sql.DriverManager;
3+
import java.sql.SQLException;
4+
5+
class DBConnection {
6+
7+
private static final String USERNAME = "eci";
8+
private static final String PASSWORD = "eci123";
9+
private static final String CONN = "jdbc:mysql://localhost:3306/eci";
10+
11+
static Connection getConnection() throws SQLException {
12+
return DriverManager.getConnection(CONN, USERNAME, PASSWORD);
13+
}
14+
}

Item.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.io.IOException;
2+
import java.io.InputStream;
3+
import java.net.URL;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
9+
class Item {
10+
11+
private static String[] match = {"/", "& ", "&", "-", ","};
12+
13+
static void getItems(ResultSet resultSet, String type) throws SQLException {
14+
15+
System.out.println("Displaying all items");
16+
17+
if ( type.equals("wheels") ) {
18+
while( resultSet.next() )
19+
System.out.println( downloadWheels( resultSet ) );
20+
21+
} else if ( type.equals("tires") ) {
22+
while( resultSet.next() )
23+
System.out.println( downloadTires( resultSet ) );
24+
}
25+
26+
System.out.println("Finished");
27+
}
28+
29+
private static String downloadWheels(ResultSet resultSet) throws SQLException {
30+
String item_number = resultSet.getString( "item_number");
31+
String brand = resultSet.getString( "brand" );
32+
String model = resultSet.getString( "model" );
33+
String finish = resultSet.getString( "finish" );
34+
35+
brand = sanitizeData( brand );
36+
model = sanitizeData( model );
37+
finish = sanitizeData( finish );
38+
39+
String image_location = "http://website.com/version_2/img/wheels/" + brand + "/" + model + "_" + finish + ".jpg";
40+
41+
try(InputStream in = new URL( image_location ).openStream()){
42+
Files.copy(in, Paths.get("D:/DB/FCWT/applications/eci-website-images/wheels/" + item_number + ".jpg"));
43+
} catch (IOException e) {
44+
//try with png
45+
try(InputStream in = new URL( image_location.replace( ".jpg", ".png" ) ).openStream()) {
46+
Files.copy(in, Paths.get("D:/DB/FCWT/applications/eci-website-images/wheels/" + item_number + ".jpg"));
47+
} catch ( IOException ignored ) {}
48+
}
49+
50+
return image_location;
51+
}
52+
53+
private static String downloadTires(ResultSet resultSet) throws SQLException {
54+
String item_number = resultSet.getString( "item_number");
55+
String brand = resultSet.getString( "brand" );
56+
String model = resultSet.getString( "model" );
57+
58+
brand = sanitizeData( brand );
59+
model = sanitizeData( model );
60+
61+
String image_location = "http://website.com/version_2/img/tires/" + brand + "/" + model + ".jpg";
62+
63+
try(InputStream in = new URL( image_location ).openStream()){
64+
Files.copy(in, Paths.get("D:/DB/FCWT/applications/eci-website-images/tires/" + item_number + ".jpg"));
65+
} catch (IOException e) {
66+
//try with png
67+
try(InputStream in = new URL( image_location.replace( ".jpg", ".png" ) ).openStream()) {
68+
Files.copy(in, Paths.get("D:/DB/FCWT/applications/eci-website-images/tires/" + item_number + ".jpg"));
69+
} catch ( IOException ignored ) {}
70+
}
71+
72+
return image_location;
73+
}
74+
75+
private static String sanitizeData( String str ) {
76+
str = str.toLowerCase();
77+
78+
for ( String replacement : match )
79+
str = str.replace( replacement, "" );
80+
81+
str = str.replace(" ", "_");
82+
83+
return str;
84+
}
85+
}

Main.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.sql.*;
2+
3+
public class Main {
4+
5+
private static String type = "wheels"; // or tires
6+
7+
public static void main(String[] args) {
8+
9+
String sql_query = ( type.equals("wheels") ) ? getWheelQuery() : getTireQuery() ;
10+
11+
try (
12+
Connection connection = DBConnection.getConnection();
13+
Statement statement = connection.createStatement(
14+
ResultSet.TYPE_SCROLL_INSENSITIVE,
15+
ResultSet.CONCUR_READ_ONLY
16+
);
17+
ResultSet resultSet = statement.executeQuery( sql_query );
18+
){
19+
20+
Item.getItems( resultSet, type );
21+
22+
} catch (SQLException e) {
23+
e.printStackTrace();
24+
}
25+
}
26+
27+
private static String getWheelQuery() {
28+
return "SELECT " +
29+
"wheels.id as id, " +
30+
"wheels.item_number as item_number, " +
31+
"brand.brand_name as brand, " +
32+
"model.model_name as model, " +
33+
"wheel_finish.finish as finish " +
34+
"FROM wheels " +
35+
"JOIN brand ON wheels.brand = brand.id " +
36+
"JOIN model ON wheels.model = model.id " +
37+
"JOIN wheel_finish ON wheels.full_finish = wheel_finish.id WHERE wheels.active = 1";
38+
}
39+
40+
private static String getTireQuery() {
41+
return "SELECT " +
42+
"tires.id as id, " +
43+
"tires.item_number as item_number, " +
44+
"brand.brand_name as brand, " +
45+
"model.model_name as model " +
46+
"FROM tires " +
47+
"JOIN brand ON tires.brand = brand.id " +
48+
"JOIN model ON tires.model = model.id WHERE tires.active = 1";
49+
}
50+
}

pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>eci-website-images</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>mysql</groupId>
26+
<artifactId>mysql-connector-java</artifactId>
27+
<version>8.0.19</version>
28+
</dependency>
29+
</dependencies>
30+
31+
</project>

0 commit comments

Comments
 (0)