Skip to content

Commit 27dc46e

Browse files
committed
commit of java database 3.0
1 parent bcc080a commit 27dc46e

12 files changed

+477
-0
lines changed

.classpath

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
<attribute name="test" value="true"/>
14+
</attributes>
15+
</classpathentry>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
17+
<attributes>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="src" path="target/generated-sources/annotations">
27+
<attributes>
28+
<attribute name="optional" value="true"/>
29+
<attribute name="maven.pomderived" value="true"/>
30+
<attribute name="ignore_optional_problems" value="true"/>
31+
<attribute name="m2e-apt" value="true"/>
32+
</attributes>
33+
</classpathentry>
34+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
35+
<attributes>
36+
<attribute name="maven.pomderived" value="true"/>
37+
</attributes>
38+
</classpathentry>
39+
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
40+
<attributes>
41+
<attribute name="optional" value="true"/>
42+
<attribute name="maven.pomderived" value="true"/>
43+
<attribute name="ignore_optional_problems" value="true"/>
44+
<attribute name="m2e-apt" value="true"/>
45+
<attribute name="test" value="true"/>
46+
</attributes>
47+
</classpathentry>
48+
<classpathentry kind="output" path="target/classes"/>
49+
</classpath>

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
**/target

.project

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>java-database</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
<filteredResources>
24+
<filter>
25+
<id>1636674346755</id>
26+
<name></name>
27+
<type>30</type>
28+
<matcher>
29+
<id>org.eclipse.core.resources.regexFilterMatcher</id>
30+
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
31+
</matcher>
32+
</filter>
33+
</filteredResources>
34+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/main/resources=UTF-8
4+
encoding/<project>=UTF-8
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.apt.aptEnabled=false

.settings/org.eclipse.jdt.core.prefs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
3+
org.eclipse.jdt.core.compiler.compliance=17
4+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
5+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
6+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
7+
org.eclipse.jdt.core.compiler.processAnnotations=disabled
8+
org.eclipse.jdt.core.compiler.release=disabled
9+
org.eclipse.jdt.core.compiler.source=17

.settings/org.eclipse.m2e.core.prefs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "automatic"
3+
}

pom.xml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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>ca.sozoservers.dev</groupId>
8+
<artifactId>java-database</artifactId>
9+
<version>3.0</version>
10+
11+
<name>java-database</name>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.xerial</groupId>
22+
<artifactId>sqlite-jdbc</artifactId>
23+
<version>3.34.0</version>
24+
<scope>compile</scope>
25+
</dependency>
26+
</dependencies>
27+
28+
<build>
29+
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
30+
<resources>
31+
<resource>
32+
<directory>${project.basedir}/src/main/resources</directory>
33+
<includes>
34+
<include>sqlite-jdbc-3.34.0.jar</include>
35+
</includes>
36+
</resource>
37+
</resources>
38+
<plugins>
39+
<plugin>
40+
<artifactId>maven-assembly-plugin</artifactId>
41+
<configuration>
42+
<archive>
43+
<manifest>
44+
<mainClass>ca.sozoservers.dev.database.Database</mainClass>
45+
</manifest>
46+
</archive>
47+
<descriptorRefs>
48+
<descriptorRef>jar-with-dependencies</descriptorRef>
49+
</descriptorRefs>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package ca.sozoservers.dev.database;
2+
3+
import java.io.File;
4+
import java.lang.reflect.Field;
5+
import java.sql.*;
6+
7+
import ca.sozoservers.dev.database.models.DatabaseModel;
8+
import ca.sozoservers.dev.database.models.DatabaseModel.Constraints;
9+
import ca.sozoservers.dev.database.models.DatabaseModel.DataType;
10+
import ca.sozoservers.dev.database.models.DatabaseModel.SqlValue;
11+
12+
public class Database {
13+
14+
private static File db;
15+
16+
public Database(File dbPath) {
17+
db = dbPath;
18+
}
19+
20+
public static boolean createDatabase() {
21+
try (Connection conn = connect()) {
22+
if (!db.exists()) {
23+
DatabaseMetaData meta = conn.getMetaData();
24+
meta.getDriverName();
25+
}
26+
}catch (SQLException ex) {
27+
ex.printStackTrace();
28+
return false;
29+
}
30+
return true;
31+
}
32+
33+
public static boolean createTable(DatabaseModel... models) {
34+
try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
35+
for (DatabaseModel model : models) {
36+
stmt.execute(model.getSQL(SqlValue.Table));
37+
}
38+
return true;
39+
} catch (SQLException ex) {
40+
ex.printStackTrace();
41+
}
42+
return false;
43+
}
44+
45+
public static boolean set(DatabaseModel model) {
46+
47+
try (Connection conn = Database.connect();
48+
PreparedStatement pstmt = conn.prepareStatement(model.getSQL(SqlValue.Set));) {
49+
Field[] fields = model.getClass().getDeclaredFields();
50+
int count = 0;
51+
for (Field field : fields) {
52+
field.setAccessible(true);
53+
if (field.isAnnotationPresent(DataType.class)) {
54+
count++;
55+
pstmt.setObject(count, field.get(model));
56+
}
57+
}
58+
pstmt.executeUpdate();
59+
pstmt.closeOnCompletion();
60+
conn.close();
61+
} catch (SQLException | IllegalArgumentException | IllegalAccessException ex) {
62+
ex.printStackTrace();
63+
return false;
64+
}
65+
return true;
66+
67+
}
68+
69+
public static boolean get(DatabaseModel model, boolean supressError) {
70+
71+
try (Connection conn = Database.connect();
72+
PreparedStatement pstmt = conn.prepareStatement(model.getSQL(SqlValue.Get));) {
73+
Field[] fields = model.getClass().getDeclaredFields();
74+
for (Field field : fields) {
75+
field.setAccessible(true);
76+
if (field.isAnnotationPresent(Constraints.class)) {
77+
if (field.getAnnotation(Constraints.class).value().equalsIgnoreCase("PRIMARY KEY")) {
78+
pstmt.setObject(1, field.get(model));
79+
break;
80+
}
81+
}
82+
}
83+
ResultSet rs = pstmt.executeQuery();
84+
for (Field field : fields) {
85+
if (field.isAnnotationPresent(DataType.class)) {
86+
field.set(model, rs.getObject(field.getName()));
87+
}
88+
}
89+
rs.close();
90+
pstmt.closeOnCompletion();
91+
conn.close();
92+
} catch (SQLException | IllegalArgumentException | IllegalAccessException ex) {
93+
if (!supressError) {
94+
ex.printStackTrace();
95+
}
96+
return false;
97+
}
98+
return true;
99+
100+
}
101+
102+
public static boolean update(DatabaseModel model) {
103+
104+
try (Connection conn = Database.connect();
105+
PreparedStatement pstmt = conn.prepareStatement(model.getSQL(SqlValue.Update));) {
106+
Field[] fields = model.getClass().getDeclaredFields();
107+
int count = 0;
108+
Object primaryKey = null;
109+
for (Field field : fields) {
110+
field.setAccessible(true);
111+
if (field.isAnnotationPresent(DataType.class)) {
112+
if (field.isAnnotationPresent(Constraints.class)) {
113+
if (field.getAnnotation(Constraints.class).value().equalsIgnoreCase("PRIMARY KEY")) {
114+
primaryKey = field.get(model);
115+
continue;
116+
}
117+
}
118+
count++;
119+
pstmt.setObject(count, field.get(model));
120+
}
121+
}
122+
count++;
123+
pstmt.setObject(count, primaryKey);
124+
pstmt.executeUpdate();
125+
pstmt.closeOnCompletion();
126+
conn.close();
127+
} catch (SQLException | IllegalArgumentException | IllegalAccessException ex) {
128+
ex.printStackTrace();
129+
return false;
130+
}
131+
return true;
132+
}
133+
134+
public static boolean delete(DatabaseModel model) {
135+
136+
try (Connection conn = Database.connect();
137+
PreparedStatement pstmt = conn.prepareStatement(model.getSQL(SqlValue.Delete));) {
138+
Field[] fields = model.getClass().getDeclaredFields();
139+
for (Field field : fields) {
140+
field.setAccessible(true);
141+
if (field.isAnnotationPresent(Constraints.class)) {
142+
if (field.getAnnotation(Constraints.class).value().equalsIgnoreCase("PRIMARY KEY")) {
143+
pstmt.setObject(1, field.get(model));
144+
break;
145+
}
146+
}
147+
}
148+
pstmt.executeUpdate();
149+
pstmt.closeOnCompletion();
150+
conn.close();
151+
} catch (SQLException | IllegalArgumentException | IllegalAccessException ex) {
152+
ex.printStackTrace();
153+
return false;
154+
}
155+
return true;
156+
157+
}
158+
159+
public static boolean exists(DatabaseModel model) {
160+
try (Connection conn = Database.connect();
161+
PreparedStatement pstmt = conn.prepareStatement(model.getSQL(SqlValue.Get));) {
162+
Field[] fields = model.getClass().getDeclaredFields();
163+
for (Field field : fields) {
164+
field.setAccessible(true);
165+
if (field.isAnnotationPresent(Constraints.class)) {
166+
if (field.getAnnotation(Constraints.class).value().equalsIgnoreCase("PRIMARY KEY")) {
167+
pstmt.setObject(1, field.get(model));
168+
break;
169+
}
170+
}
171+
}
172+
ResultSet rs = pstmt.executeQuery();
173+
rs.close();
174+
pstmt.closeOnCompletion();
175+
conn.close();
176+
} catch (SQLException | IllegalArgumentException | IllegalAccessException ex) {
177+
ex.printStackTrace();
178+
return false;
179+
}
180+
return true;
181+
}
182+
183+
public static Connection connect() throws SQLException {
184+
return DriverManager.getConnection("jdbc:sqlite:" + db);
185+
}
186+
}

0 commit comments

Comments
 (0)