Skip to content

Commit 621381a

Browse files
committed
initial commit
0 parents  commit 621381a

File tree

8 files changed

+162
-0
lines changed

8 files changed

+162
-0
lines changed

.classpath

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
5+
<attributes>
6+
<attribute name="module" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/MySQLConnector">
10+
<attributes>
11+
<attribute name="module" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="output" path="bin"/>
15+
</classpath>

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
replay_pid*

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Java-DAO-JDBC</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+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=11
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13+
org.eclipse.jdt.core.compiler.release=enabled
14+
org.eclipse.jdt.core.compiler.source=11

db.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
user=root
2+
password=******
3+
dburl=jdbc:mysql://localhost:3306/jdbc
4+
useSSL=false

src/db/DB.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package db;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
import java.sql.Statement;
10+
import java.util.Properties;
11+
12+
public class DB {
13+
14+
private static Connection conn = null;
15+
16+
public static Connection getConnection() {
17+
if (conn == null) {
18+
try {
19+
Properties props = loadProperties();
20+
String url = props.getProperty("dburl");
21+
conn = DriverManager.getConnection(url, props);
22+
} catch (SQLException e) {
23+
throw new DbException(e.getMessage());
24+
}
25+
26+
}
27+
return conn;
28+
}
29+
30+
public static void closeConnection() {
31+
if (conn != null) {
32+
try {
33+
conn.close();
34+
} catch (SQLException e) {
35+
throw new DbException(e.getMessage());
36+
}
37+
38+
}
39+
}
40+
41+
private static Properties loadProperties() {
42+
try (FileInputStream fs = new FileInputStream("db.properties")) {
43+
Properties props = new Properties();
44+
props.load(fs);
45+
return props;
46+
} catch (IOException e) {
47+
throw new DbException(e.getMessage());
48+
}
49+
}
50+
51+
public static void closeStatement(Statement st) {
52+
if(st!=null) {
53+
try {
54+
st.close();
55+
} catch (SQLException e) {
56+
throw new DbException(e.getMessage());
57+
}
58+
}
59+
}
60+
61+
public static void closeResultSet(ResultSet rs) {
62+
if(rs!=null) {
63+
try {
64+
rs.close();
65+
} catch (SQLException e) {
66+
throw new DbException(e.getMessage());
67+
}
68+
}
69+
}
70+
}

src/db/DbException.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package db;
2+
3+
public class DbException extends RuntimeException{
4+
private static final long serialVersionUID = 1L;
5+
6+
public DbException(String msg) {
7+
super(msg);
8+
}
9+
}

src/db/DbIntegrityException.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package db;
2+
3+
public class DbIntegrityException extends RuntimeException {
4+
private static final long serialVersionUID = 1L;
5+
6+
public DbIntegrityException(String msg) {
7+
super(msg);
8+
}
9+
}

0 commit comments

Comments
 (0)