Skip to content

Commit 87231e7

Browse files
committed
Added Model, SELECT Query working now
1 parent 5ca1a60 commit 87231e7

File tree

11 files changed

+663
-0
lines changed

11 files changed

+663
-0
lines changed

.classpath

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
<attribute name="test" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
<attribute name="test" value="true"/>
25+
</attributes>
26+
</classpathentry>
27+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
28+
<attributes>
29+
<attribute name="maven.pomderived" value="true"/>
30+
</attributes>
31+
</classpathentry>
32+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
33+
<attributes>
34+
<attribute name="maven.pomderived" value="true"/>
35+
</attributes>
36+
</classpathentry>
37+
<classpathentry kind="output" path="target/classes"/>
38+
</classpath>

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>jdbc-connection-generic</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+
</projectDescription>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.release=disabled
6+
org.eclipse.jdt.core.compiler.source=1.8

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

Lines changed: 4 additions & 0 deletions
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
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
/**
2+
*
3+
*/
4+
package com.sdcworld.controller;
5+
6+
import java.lang.reflect.Field;
7+
import java.lang.reflect.Modifier;
8+
import java.sql.Connection;
9+
import java.sql.DriverManager;
10+
import java.sql.PreparedStatement;
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
import java.util.ArrayList;
15+
import java.util.Arrays;
16+
import java.util.HashMap;
17+
import java.util.LinkedHashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
import com.sdcworld.controller.helper.EnumDatabaseCase;
22+
import com.sdcworld.utility.ConvertUtility;
23+
import com.sdcworld.utility.StringUtility;
24+
25+
/**
26+
* @author souro
27+
*
28+
*/
29+
public class JdbcConnection<T> {
30+
31+
private String url = "jdbc:mysql://localhost/sdc";
32+
private String username = "devuser";
33+
private String password = "Demo@123";
34+
private String driver = "com.mysql.jdbc.Driver";
35+
private String tableName;
36+
private Map<String, String> columnsMap = new HashMap<String, String>();
37+
38+
private EnumDatabaseCase databaseCase = EnumDatabaseCase.SENTENCECASE_UNDERSCORE;
39+
40+
private static final String SELECT_ALL = "SELECT * FROM ${table}";
41+
private static final String INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})";
42+
private static final String TABLE_NAME = "\\$\\{table\\}";
43+
private static final String KEYS = "\\$\\{keys\\}";
44+
private static final String VALUES = "\\$\\{values\\}";
45+
46+
private Class<T> genericType;
47+
48+
private boolean isInitializationCompleted;
49+
private boolean hasAutomaticField;
50+
private boolean hasPrimaryKey;
51+
private boolean hasForeignKey;
52+
private boolean isUnique;
53+
54+
private Map<String, Field> privateFields = new LinkedHashMap<>();
55+
56+
private Connection dbConnection = null;
57+
private Statement statement = null;
58+
private PreparedStatement preparedStatement = null;
59+
private ResultSet resultSet = null;
60+
61+
// Constructors
62+
public JdbcConnection(final Class<T> type)
63+
{
64+
this.genericType = type;
65+
connectToDb();
66+
initialize();
67+
}
68+
69+
public JdbcConnection(final Class<T> type, EnumDatabaseCase databaseCase)
70+
{
71+
this.genericType = type;
72+
connectToDb();
73+
this.databaseCase = databaseCase;
74+
initialize();
75+
}
76+
77+
public JdbcConnection(final Class<T> type, String url, String username, String password)
78+
{
79+
this.genericType = type;
80+
this.url = url;
81+
this.username = username;
82+
this.password = password;
83+
84+
connectToDb();
85+
initialize();
86+
}
87+
88+
public JdbcConnection(final Class<T> type, String url, String username, String password, EnumDatabaseCase databaseCase)
89+
{
90+
this.genericType = type;
91+
this.url = url;
92+
this.username = username;
93+
this.password = password;
94+
this.databaseCase = databaseCase;
95+
96+
connectToDb();
97+
initialize();
98+
}
99+
100+
public JdbcConnection(final Class<T> type, String url, String username, String password, String driver)
101+
{
102+
this.genericType = type;
103+
this.url = url;
104+
this.username = username;
105+
this.password = password;
106+
this.driver = driver;
107+
108+
connectToDb();
109+
initialize();
110+
}
111+
112+
public JdbcConnection(final Class<T> type, String url, String username, String password, String driver, EnumDatabaseCase databaseCase)
113+
{
114+
this.genericType = type;
115+
this.url = url;
116+
this.username = username;
117+
this.password = password;
118+
this.driver = driver;
119+
this.databaseCase = databaseCase;
120+
121+
connectToDb();
122+
initialize();
123+
}
124+
125+
private void initialize()
126+
{
127+
if (!this.isInitializationCompleted) {
128+
// this section is only for finding private fields
129+
// in a class to add them in field class
130+
Field[] fields = genericType.getDeclaredFields();
131+
for (Field field : fields) {
132+
if (Modifier.isPrivate(field.getModifiers())) {
133+
privateFields.put(field.getName(), field);
134+
}
135+
}
136+
137+
try
138+
{
139+
// getSimpleName() will not work for static methods
140+
this.tableName = genericType.newInstance().getClass().getSimpleName().toLowerCase();
141+
142+
List<String> listOfFieldNames = new ArrayList<>(privateFields.keySet());
143+
144+
for (String string : listOfFieldNames) {
145+
this.columnsMap.put(string, StringUtility.toSentenceUnderScore(string));
146+
}
147+
}
148+
catch(Exception e)
149+
{
150+
e.printStackTrace();
151+
}
152+
153+
this.isInitializationCompleted = true;
154+
}
155+
}
156+
157+
/**
158+
* A method for connecting to the database
159+
*/
160+
private void connectToDb() {
161+
try
162+
{
163+
Class.forName(driver);
164+
dbConnection = DriverManager.getConnection(url, username, password);
165+
}
166+
catch(Exception e)
167+
{
168+
e.printStackTrace();
169+
}
170+
}
171+
172+
public Connection getConnection()
173+
{
174+
return dbConnection;
175+
}
176+
177+
public List<T> getAll()
178+
{
179+
List<T> list = new ArrayList<>();
180+
181+
try
182+
{
183+
// List<String> columns = new ArrayList<String>(columnsMap.values());
184+
String query = SELECT_ALL.replaceFirst(TABLE_NAME, tableName);
185+
statement = dbConnection.createStatement();
186+
resultSet = statement.executeQuery(query);
187+
188+
while (resultSet.next()) {
189+
T refObject = genericType.newInstance();
190+
this.loadResultSetIntoObject(resultSet, refObject);
191+
list.add(refObject);
192+
}
193+
194+
return list;
195+
}
196+
catch(Exception e)
197+
{
198+
e.printStackTrace();
199+
}
200+
201+
return null;
202+
}
203+
204+
private void loadResultSetIntoObject(ResultSet resultSet2, T refObject) throws IllegalArgumentException, IllegalAccessException, SQLException {
205+
Class<?> classDenoter = refObject.getClass();
206+
for (Field field : classDenoter.getDeclaredFields()) {
207+
String name = field.getName();
208+
field.setAccessible(true);
209+
Object value = resultSet2.getObject(columnsMap.get(name));
210+
Class<?> type = field.getType();
211+
if (ConvertUtility.isPrimitive(type)) {
212+
Class<?> boxed = ConvertUtility.boxPrimitiveClass(type);
213+
value = boxed.cast(value);
214+
}
215+
field.set(refObject, value);
216+
}
217+
}
218+
219+
public T getAll(Object condition)
220+
{
221+
return null;
222+
}
223+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
*
3+
*/
4+
package com.sdcworld.controller.helper;
5+
6+
/**
7+
* @author souro
8+
*
9+
*/
10+
public enum EnumDatabaseCase {
11+
12+
// account_id
13+
LOWERCASE_UNDERSCORE,
14+
15+
// ACCOUNT_ID
16+
UPPERCASE_UNDERSCORE,
17+
18+
// Account_Id
19+
SENTENCECASE_UNDERSCORE,
20+
21+
// accountId
22+
LOWERCAMELCASE,
23+
24+
// AccountId
25+
UPPERCAMELCASE,
26+
27+
// ACCOUNTID
28+
UPPERCASE,
29+
30+
// accountid
31+
LOWECASE,
32+
33+
// Accountid
34+
SENTENCECASE
35+
}

0 commit comments

Comments
 (0)