Added SQL List handler and renamed existing handlers
This commit is contained in:
parent
432d7cb438
commit
4d60a60c1d
5 changed files with 216 additions and 148 deletions
|
|
@ -24,7 +24,7 @@
|
|||
|
||||
package zutil.db;
|
||||
|
||||
import zutil.db.handler.SimpleSQLHandler;
|
||||
import zutil.db.handler.SimpleSQLResult;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import javax.naming.InitialContext;
|
||||
|
|
@ -129,7 +129,7 @@ public class DBConnection implements Closeable{
|
|||
*/
|
||||
public long getLastInsertID(){
|
||||
try{
|
||||
return exec("SELECT LAST_INSERT_ID()", new SimpleSQLHandler<BigInteger>()).longValue();
|
||||
return exec("SELECT LAST_INSERT_ID()", new SimpleSQLResult<BigInteger>()).longValue();
|
||||
}catch(SQLException e){
|
||||
logger.log(Level.WARNING, null, e);
|
||||
}
|
||||
|
|
@ -195,8 +195,8 @@ public class DBConnection implements Closeable{
|
|||
|
||||
/**
|
||||
* Executes an query and cleans up after itself.
|
||||
* @param <T>
|
||||
*
|
||||
* @param <T>
|
||||
* @param query is the query
|
||||
* @param handler is the result handler
|
||||
* @return update count or -1 if the query is not an update query
|
||||
|
|
|
|||
68
src/zutil/db/handler/ListSQLResult.java
Executable file
68
src/zutil/db/handler/ListSQLResult.java
Executable file
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.db.handler;
|
||||
|
||||
import zutil.db.SQLResultHandler;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Adds the result of the query to a List.
|
||||
*
|
||||
* The handler will add the first column of every row to a list.
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class ListSQLResult<T> implements SQLResultHandler<List<T>> {
|
||||
|
||||
private List<T> list;
|
||||
|
||||
/**
|
||||
* Creates a new List.
|
||||
*/
|
||||
public ListSQLResult(){
|
||||
this.list = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses a existing list that items will be appended on.
|
||||
*/
|
||||
public ListSQLResult(List l){
|
||||
this.list = l;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<T> handleQueryResult(Statement stmt, ResultSet result) throws SQLException{
|
||||
while( result.next() )
|
||||
list.add((T)result.getObject(0));
|
||||
return list;
|
||||
}
|
||||
}
|
||||
6
src/zutil/db/handler/PropertiesSQLHandler.java → src/zutil/db/handler/PropertiesSQLResult.java
Normal file → Executable file
6
src/zutil/db/handler/PropertiesSQLHandler.java → src/zutil/db/handler/PropertiesSQLResult.java
Normal file → Executable file
|
|
@ -39,21 +39,21 @@ import java.util.Properties;
|
|||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class PropertiesSQLHandler implements SQLResultHandler<Properties> {
|
||||
public class PropertiesSQLResult implements SQLResultHandler<Properties> {
|
||||
|
||||
private Properties prop;
|
||||
|
||||
/**
|
||||
* Creates a new Properties object to be filled
|
||||
*/
|
||||
public PropertiesSQLHandler(){
|
||||
public PropertiesSQLResult(){
|
||||
this.prop = new Properties();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds data to a existing Properties object
|
||||
*/
|
||||
public PropertiesSQLHandler(Properties p){
|
||||
public PropertiesSQLResult(Properties p){
|
||||
this.prop = p;
|
||||
}
|
||||
|
||||
2
src/zutil/db/handler/SimpleSQLHandler.java → src/zutil/db/handler/SimpleSQLResult.java
Normal file → Executable file
2
src/zutil/db/handler/SimpleSQLHandler.java → src/zutil/db/handler/SimpleSQLResult.java
Normal file → Executable file
|
|
@ -35,7 +35,7 @@ import java.sql.Statement;
|
|||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class SimpleSQLHandler<T> implements SQLResultHandler<T> {
|
||||
public class SimpleSQLResult<T> implements SQLResultHandler<T> {
|
||||
/**
|
||||
* Is called to handle an result from an query.
|
||||
*
|
||||
6
test/zutil/test/DBConnectionTest.java
Normal file → Executable file
6
test/zutil/test/DBConnectionTest.java
Normal file → Executable file
|
|
@ -24,7 +24,7 @@
|
|||
package zutil.test;
|
||||
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.db.handler.SimpleSQLHandler;
|
||||
import zutil.db.handler.SimpleSQLResult;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
||||
|
|
@ -43,13 +43,13 @@ public class DBConnectionTest {
|
|||
db.exec("UPDATE ...");
|
||||
|
||||
// Query 3
|
||||
String s = db.exec("SELECT hello", new SimpleSQLHandler<String>());
|
||||
String s = db.exec("SELECT hello", new SimpleSQLResult<String>());
|
||||
System.out.println( s );
|
||||
|
||||
// Query 4
|
||||
PreparedStatement sql2 = db.getPreparedStatement("SELECT ?");
|
||||
sql2.setString(1, "hello");
|
||||
String s2 = DBConnection.exec(sql2, new SimpleSQLHandler<String>());
|
||||
String s2 = DBConnection.exec(sql2, new SimpleSQLResult<String>());
|
||||
System.out.println( s2 );
|
||||
|
||||
db.close();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue