forked from YaleGuo/Minis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request YaleGuo#15 from YaleGuo/jdbc
JdbcTemplate implemented and single connection datasource supported.
- Loading branch information
Showing
13 changed files
with
637 additions
and
2 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
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
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
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,93 @@ | ||
package com.minis.jdbc.core; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.Statement; | ||
import javax.sql.DataSource; | ||
|
||
|
||
|
||
public class JdbcTemplate { | ||
private DataSource dataSource; | ||
|
||
public void setDataSource(DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public DataSource getDataSource() { | ||
return this.dataSource; | ||
} | ||
|
||
public JdbcTemplate() { | ||
} | ||
|
||
public Object query(StatementCallback stmtcallback) { | ||
Connection con = null; | ||
Statement stmt = null; | ||
|
||
try { | ||
con = dataSource.getConnection(); | ||
|
||
stmt = con.createStatement(); | ||
|
||
return stmtcallback.doInStatement(stmt); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
finally { | ||
try { | ||
stmt.close(); | ||
con.close(); | ||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
|
||
return null; | ||
|
||
} | ||
public Object query(String sql, Object[] args, PreparedStatementCallback pstmtcallback) { | ||
Connection con = null; | ||
PreparedStatement pstmt = null; | ||
|
||
try { | ||
con = dataSource.getConnection(); | ||
|
||
pstmt = con.prepareStatement(sql); | ||
if (args != null) { | ||
for (int i = 0; i < args.length; i++) { | ||
Object arg = args[i]; | ||
if (arg instanceof String) { | ||
pstmt.setString(i+1, (String)arg); | ||
} | ||
else if (arg instanceof Integer) { | ||
pstmt.setInt(i+1, (int)arg); | ||
} | ||
else if (arg instanceof java.util.Date) { | ||
pstmt.setDate(i+1, new java.sql.Date(((java.util.Date)arg).getTime())); | ||
|
||
} | ||
} | ||
} | ||
|
||
|
||
return pstmtcallback.doInPreparedStatement(pstmt); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
finally { | ||
try { | ||
pstmt.close(); | ||
con.close(); | ||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
|
||
return null; | ||
|
||
} | ||
} |
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,45 @@ | ||
package com.minis.jdbc.core; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
|
||
|
||
public abstract class OldJdbcTemplate { | ||
public OldJdbcTemplate() { | ||
} | ||
|
||
public Object query(String sql) { | ||
Connection con = null; | ||
PreparedStatement stmt = null; | ||
ResultSet rs = null; | ||
Object rtnObj = null; | ||
|
||
try { | ||
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); | ||
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databasename=DEMO;user=sa;password=Sql2016;"); | ||
|
||
stmt = con.prepareStatement(sql); | ||
rs = stmt.executeQuery(); | ||
|
||
rtnObj = doInStatement(rs); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
finally { | ||
try { | ||
rs.close(); | ||
stmt.close(); | ||
con.close(); | ||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
|
||
return rtnObj; | ||
|
||
} | ||
protected abstract Object doInStatement(ResultSet rs); | ||
} |
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,8 @@ | ||
package com.minis.jdbc.core; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
public interface PreparedStatementCallback { | ||
Object doInPreparedStatement(PreparedStatement stmt) throws SQLException; | ||
} |
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,8 @@ | ||
package com.minis.jdbc.core; | ||
|
||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
public interface StatementCallback { | ||
Object doInStatement(Statement stmt) throws SQLException; | ||
} |
Oops, something went wrong.