Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 90ebaea
Showing
46 changed files
with
2,973 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.settings | ||
/.classpath | ||
/.project | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.ponxu</groupId> | ||
<artifactId>run</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>run</name> | ||
<url>http://www.ponxu.com</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.10</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.14</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.freemarker</groupId> | ||
<artifactId>freemarker</artifactId> | ||
<version>2.3.19</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.json</groupId> | ||
<artifactId>json</artifactId> | ||
<version>20090211</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.ponxu.run.db; | ||
|
||
import java.sql.Connection; | ||
|
||
/** | ||
* | ||
* @author xwz | ||
*/ | ||
public interface Atom { | ||
public void run(Connection transcationConnection); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package com.ponxu.run.db; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.ponxu.run.db.wrap.RowWrapper; | ||
import com.ponxu.run.lang.ExceptionUtils; | ||
import com.ponxu.run.log.Log; | ||
import com.ponxu.run.log.LogFactory; | ||
|
||
/** | ||
* 数据库操作类 | ||
* | ||
* @author xwz | ||
*/ | ||
public class DBUtils { | ||
private static final Log LOG = LogFactory.getLog(); | ||
|
||
public static void loadDriver(String dirver) { | ||
try { | ||
Class.forName(dirver); | ||
} catch (ClassNotFoundException e) { | ||
LOG.error("can not load driver %s", e, dirver); | ||
} | ||
} | ||
|
||
public static Connection getConnection(String url, String user, | ||
String password) { | ||
try { | ||
return DriverManager.getConnection(url, user, password); | ||
} catch (SQLException e) { | ||
LOG.error("%s, %s, %s", e, url, user, password); | ||
} | ||
return null; | ||
} | ||
|
||
// ------------------------------------------------------------- | ||
public static <T> List<T> query(Connection conn, RowWrapper<T> wrapper, | ||
String sql, Object... values) { | ||
List<T> list = new ArrayList<T>(); | ||
try { | ||
PreparedStatement pstmt = prepareStatement(conn, sql); | ||
setPreparedStatement(pstmt, values); | ||
|
||
ResultSet rs = pstmt.executeQuery(); | ||
while (rs.next()) { | ||
list.add(wrapper.wrap(rs)); | ||
} | ||
|
||
close(rs); | ||
close(pstmt); | ||
} catch (SQLException e) { | ||
ExceptionUtils.makeRuntime(e); | ||
} | ||
return list; | ||
} | ||
|
||
public static int execute(Connection conn, String sql, Object... values) { | ||
try { | ||
PreparedStatement pstmt = prepareStatement(conn, sql); | ||
setPreparedStatement(pstmt, values); | ||
int r = pstmt.executeUpdate(); | ||
// 插入语句, 返回自增ID | ||
if (sql.toLowerCase().trim().startsWith("insert ")) { | ||
ResultSet rs = pstmt.getGeneratedKeys(); | ||
if (rs.next()) { | ||
r = rs.getInt(1); | ||
} | ||
} | ||
close(pstmt); | ||
return r; | ||
} catch (SQLException e) { | ||
ExceptionUtils.makeRuntime(e); | ||
} | ||
return -1; | ||
} | ||
|
||
public void executeTranscation(Connection conn, Atom atom) { | ||
try { | ||
boolean oldIsAuotCommit = conn.getAutoCommit(); | ||
conn.setAutoCommit(false); | ||
|
||
atom.run(conn); | ||
|
||
conn.commit(); | ||
conn.setAutoCommit(oldIsAuotCommit); | ||
} catch (SQLException e) { | ||
ExceptionUtils.makeRuntime(e); | ||
} | ||
} | ||
|
||
// ------------------------------------------------------------- | ||
private static PreparedStatement prepareStatement(Connection conn, | ||
String sql) throws SQLException { | ||
PreparedStatement pstmt = conn.prepareStatement(sql); | ||
return pstmt; | ||
} | ||
|
||
private static void setPreparedStatement(PreparedStatement pstmt, | ||
Object... values) throws SQLException { | ||
if (values == null) | ||
return; | ||
for (int i = 0; i < values.length; i++) | ||
pstmt.setObject(i + 1, values[i]); | ||
} | ||
|
||
// ------------------------------------------------------------- | ||
public static void beginTranscation(Connection conn) { | ||
try { | ||
conn.setAutoCommit(false); | ||
} catch (SQLException e) { | ||
// quiet | ||
} | ||
} | ||
|
||
public static void commit(Connection conn) { | ||
try { | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
// quiet | ||
} | ||
} | ||
|
||
public static void close(Connection conn) { | ||
try { | ||
conn.close(); | ||
} catch (SQLException e) { | ||
// quiet | ||
} | ||
} | ||
|
||
public static void commitAndClose(Connection conn) { | ||
commit(conn); | ||
close(conn); | ||
} | ||
|
||
public static void close(ResultSet rs) { | ||
try { | ||
rs.close(); | ||
} catch (SQLException e) { | ||
// quiet | ||
} | ||
} | ||
|
||
public static void close(Statement stmt) { | ||
try { | ||
stmt.close(); | ||
} catch (SQLException e) { | ||
// quiet | ||
} | ||
} | ||
|
||
public static void close(PreparedStatement pstmt) { | ||
try { | ||
pstmt.close(); | ||
} catch (SQLException e) { | ||
// quiet | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.ponxu.run.db.wrap; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* | ||
* @author xwz | ||
*/ | ||
public abstract interface RowWrapper<T> { | ||
public abstract T wrap(ResultSet rs) throws SQLException; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ponxu/run/db/wrap/impl/LongRowWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ponxu.run.db.wrap.impl; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
import com.ponxu.run.db.wrap.RowWrapper; | ||
|
||
/** | ||
* | ||
* @author xwz | ||
*/ | ||
public class LongRowWrapper implements RowWrapper<Long> { | ||
private static LongRowWrapper instance = new LongRowWrapper(); | ||
|
||
public static LongRowWrapper getInstance() { | ||
return instance; | ||
} | ||
|
||
public Long wrap(ResultSet rs) throws SQLException { | ||
return Long.valueOf(rs.getLong(1)); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/ponxu/run/db/wrap/impl/MapRowWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.ponxu.run.db.wrap.impl; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.ponxu.run.db.wrap.RowWrapper; | ||
|
||
/** | ||
* | ||
* @author xwz | ||
*/ | ||
public class MapRowWrapper implements RowWrapper<Map<String, String>> { | ||
private static MapRowWrapper instance = new MapRowWrapper(); | ||
|
||
public static MapRowWrapper getInstance() { | ||
return instance; | ||
} | ||
|
||
public Map<String, String> wrap(ResultSet rs) throws SQLException { | ||
Map<String, String> row = new HashMap<String, String>(); | ||
ResultSetMetaData metaData = rs.getMetaData(); | ||
int colCount = metaData.getColumnCount(); | ||
for (int i = 1; i <= colCount; i++) { | ||
String colName = metaData.getColumnName(i); | ||
row.put(colName.toLowerCase(), rs.getString(i)); | ||
} | ||
return row; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ponxu/run/db/wrap/impl/StringRowWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ponxu.run.db.wrap.impl; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
import com.ponxu.run.db.wrap.RowWrapper; | ||
|
||
/** | ||
* | ||
* @author xwz | ||
*/ | ||
public class StringRowWrapper implements RowWrapper<String> { | ||
private static StringRowWrapper instance = new StringRowWrapper(); | ||
|
||
public static StringRowWrapper getInstance() { | ||
return instance; | ||
} | ||
|
||
public String wrap(ResultSet rs) throws SQLException { | ||
return rs.getString(1); | ||
} | ||
} |
Oops, something went wrong.