Last active
December 28, 2017 06:28
-
-
Save akari0624/7851632896a06c10f2356a7eb933782e to your computer and use it in GitHub Desktop.
JAVA apache Dbutils example
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
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.commons.dbutils.DbUtils; | |
import org.apache.commons.dbutils.QueryRunner; | |
import org.apache.commons.dbutils.ResultSetHandler; | |
import com.util.Common; | |
import com.util.Database; | |
import common.Logger; | |
public class SchedulerDAOImpl implements SchedulerDAO{ | |
Logger m_Logger = Logger.getLogger(SchedulerDAOImpl.class); | |
private static final String getAllFromSomeTable = "select * from SomeTable"; | |
// use Dbutils to do Database Query | |
@Override | |
public List<String> getCaseData() { | |
Database db = new Database(); | |
Connection conn = db.getConnection(); | |
// No DataSource so we must handle Connections manually | |
QueryRunner run = new QueryRunner(true); | |
try{ | |
List<String> resultList = run.query( | |
conn, getAllFromSomeTable, new ResultSetHandler<List<String>>(){ | |
// do something with the ResultSet | |
@Override | |
public List<String> handle(ResultSet rs) | |
throws SQLException { | |
List<String> resultArr = new ArrayList<String>(); | |
while(rs.next()){ | |
resultArr.add(Common.isoToBig5(rs.getString(3))); | |
m_Logger.info(Common.isoToBig5(rs.getString(3))); | |
} | |
return resultArr; | |
} | |
}); | |
m_Logger.info("查詢完"); | |
}catch(SQLException sqlEx){ | |
m_Logger.error(sqlEx.getMessage()); | |
} | |
finally { | |
// Use this helper method so we don't have to check for null | |
db.closeAll(); | |
} | |
return resultList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment