diff --git a/src/zutil/db/DBConnection.java b/src/zutil/db/DBConnection.java index 564adbf..3606fdc 100755 --- a/src/zutil/db/DBConnection.java +++ b/src/zutil/db/DBConnection.java @@ -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; @@ -52,7 +52,7 @@ public class DBConnection implements Closeable{ /** * Creates an Connection from JNDI * - * @param jndi the name of the connection, e.g. "jdbc/mysql" + * @param jndi the name of the connection, e.g. "jdbc/mysql" */ public DBConnection(String jndi) throws NamingException, SQLException{ InitialContext ctx = new InitialContext(); @@ -63,10 +63,10 @@ public class DBConnection implements Closeable{ /** * Creates an Connection to a MySQL server * - * @param url is the URL of the MySQL server - * @param db is the database to connect to - * @param user is the user name - * @param password is the password + * @param url is the URL of the MySQL server + * @param db is the database to connect to + * @param user is the user name + * @param password is the password */ public DBConnection(String url, String db, String user, String password) throws Exception{ this(DBMS.MySQL, url, db, user, password); @@ -75,11 +75,11 @@ public class DBConnection implements Closeable{ /** * Creates an Connection to a DB server * - * @param dbms is the DB type - * @param url is the URL of the MySQL server - * @param db is the database to connect to - * @param user is the user name - * @param password is the password + * @param dbms is the DB type + * @param url is the URL of the MySQL server + * @param db is the database to connect to + * @param user is the user name + * @param password is the password */ public DBConnection(DBMS dbms, String url, String db, String user, String password) throws Exception{ String dbms_name = initDriver(dbms); @@ -89,8 +89,8 @@ public class DBConnection implements Closeable{ /** * Creates an Connection to a DB file * - * @param dbms is the DB type - * @param db is the database to connect to + * @param dbms is the DB type + * @param db is the database to connect to */ public DBConnection(DBMS dbms, String db) throws Exception{ String dbms_name = initDriver(dbms); @@ -107,7 +107,7 @@ public class DBConnection implements Closeable{ /** * Initiates the DB driver and returns its protocol name. * - * @param db is the DB type + * @param db is the DB type * @return the protocol name of the DBMS */ public String initDriver(DBMS db) throws InstantiationException, IllegalAccessException, ClassNotFoundException{ @@ -129,7 +129,7 @@ public class DBConnection implements Closeable{ */ public long getLastInsertID(){ try{ - return exec("SELECT LAST_INSERT_ID()", new SimpleSQLHandler()).longValue(); + return exec("SELECT LAST_INSERT_ID()", new SimpleSQLResult()).longValue(); }catch(SQLException e){ logger.log(Level.WARNING, null, e); } @@ -140,7 +140,7 @@ public class DBConnection implements Closeable{ * Runs a Prepared Statement.
* NOTE: Don't forget to close the PreparedStatement or it can lead to memory leaks * - * @param sql is the SQL query to run + * @param sql is the SQL query to run * @return An PreparedStatement */ public PreparedStatement getPreparedStatement(String sql) throws SQLException{ @@ -159,7 +159,7 @@ public class DBConnection implements Closeable{ /** * Executes an query and cleans up after itself. * - * @param query is the query + * @param query is the query * @return update count or -1 if the query is not an update query */ public int exec(String query) throws SQLException { @@ -170,7 +170,7 @@ public class DBConnection implements Closeable{ /** * Executes an query and cleans up after itself. * - * @param stmt is the query + * @param stmt is the query * @return update count or -1 if the query is not an update query */ public static int exec(PreparedStatement stmt) throws SQLException { @@ -195,10 +195,10 @@ public class DBConnection implements Closeable{ /** * Executes an query and cleans up after itself. - * @param - * - * @param query is the query - * @param handler is the result handler + * + * @param + * @param query is the query + * @param handler is the result handler * @return update count or -1 if the query is not an update query */ public T exec(String query, SQLResultHandler handler) throws SQLException { @@ -209,8 +209,8 @@ public class DBConnection implements Closeable{ /** * Executes an query and cleans up after itself. * - * @param stmt is the query - * @param handler is the result handler + * @param stmt is the query + * @param handler is the result handler * @return the object from the handler */ public static T exec(PreparedStatement stmt, SQLResultHandler handler) throws SQLException{ @@ -260,7 +260,7 @@ public class DBConnection implements Closeable{ /** * Sets the pool that this connection belongs to * - * @param pool is the pool + * @param pool is the pool */ protected void setPool(DBConnectionPool pool){ if( pool != null ) diff --git a/src/zutil/db/handler/ListSQLResult.java b/src/zutil/db/handler/ListSQLResult.java new file mode 100755 index 0000000..a6aee59 --- /dev/null +++ b/src/zutil/db/handler/ListSQLResult.java @@ -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 implements SQLResultHandler> { + + private List 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 handleQueryResult(Statement stmt, ResultSet result) throws SQLException{ + while( result.next() ) + list.add((T)result.getObject(0)); + return list; + } +} diff --git a/src/zutil/db/handler/PropertiesSQLHandler.java b/src/zutil/db/handler/PropertiesSQLResult.java old mode 100644 new mode 100755 similarity index 89% rename from src/zutil/db/handler/PropertiesSQLHandler.java rename to src/zutil/db/handler/PropertiesSQLResult.java index 55ebf4d..a0548ee --- a/src/zutil/db/handler/PropertiesSQLHandler.java +++ b/src/zutil/db/handler/PropertiesSQLResult.java @@ -1,69 +1,69 @@ -/* - * 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.Properties; - -/** - * Adds the result of the query to a Properties object, - * - * The handler sets the first column of the result as - * the key and the second column as the value - * - * @author Ziver - */ -public class PropertiesSQLHandler implements SQLResultHandler { - - private Properties prop; - - /** - * Creates a new Properties object to be filled - */ - public PropertiesSQLHandler(){ - this.prop = new Properties(); - } - - /** - * Adds data to a existing Properties object - */ - public PropertiesSQLHandler(Properties p){ - this.prop = p; - } - - - /** - * Is called to handle an result from an query. - */ - public Properties handleQueryResult(Statement stmt, ResultSet result) throws SQLException{ - while( result.next() ) - prop.setProperty(result.getString(0), result.getString(1)); - return prop; - } -} +/* + * 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.Properties; + +/** + * Adds the result of the query to a Properties object, + * + * The handler sets the first column of the result as + * the key and the second column as the value + * + * @author Ziver + */ +public class PropertiesSQLResult implements SQLResultHandler { + + private Properties prop; + + /** + * Creates a new Properties object to be filled + */ + public PropertiesSQLResult(){ + this.prop = new Properties(); + } + + /** + * Adds data to a existing Properties object + */ + public PropertiesSQLResult(Properties p){ + this.prop = p; + } + + + /** + * Is called to handle an result from an query. + */ + public Properties handleQueryResult(Statement stmt, ResultSet result) throws SQLException{ + while( result.next() ) + prop.setProperty(result.getString(0), result.getString(1)); + return prop; + } +} diff --git a/src/zutil/db/handler/SimpleSQLHandler.java b/src/zutil/db/handler/SimpleSQLResult.java old mode 100644 new mode 100755 similarity index 93% rename from src/zutil/db/handler/SimpleSQLHandler.java rename to src/zutil/db/handler/SimpleSQLResult.java index 48f4c65..e30949e --- a/src/zutil/db/handler/SimpleSQLHandler.java +++ b/src/zutil/db/handler/SimpleSQLResult.java @@ -1,51 +1,51 @@ -/* - * 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; - -/** - * Returns the first column of the first row from the query - * - * @author Ziver - */ -public class SimpleSQLHandler implements SQLResultHandler { - /** - * Is called to handle an result from an query. - * - * @param stmt is the query - * @param result is the ResultSet - */ - @SuppressWarnings("unchecked") - public T handleQueryResult(Statement stmt, ResultSet result) throws SQLException{ - if( result.next() ) - return (T) result.getObject(1); - return null; - } -} +/* + * 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; + +/** + * Returns the first column of the first row from the query + * + * @author Ziver + */ +public class SimpleSQLResult implements SQLResultHandler { + /** + * Is called to handle an result from an query. + * + * @param stmt is the query + * @param result is the ResultSet + */ + @SuppressWarnings("unchecked") + public T handleQueryResult(Statement stmt, ResultSet result) throws SQLException{ + if( result.next() ) + return (T) result.getObject(1); + return null; + } +} diff --git a/test/zutil/test/DBConnectionTest.java b/test/zutil/test/DBConnectionTest.java old mode 100644 new mode 100755 index 524f9e4..939c8e0 --- a/test/zutil/test/DBConnectionTest.java +++ b/test/zutil/test/DBConnectionTest.java @@ -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 s = db.exec("SELECT hello", new SimpleSQLResult()); System.out.println( s ); // Query 4 PreparedStatement sql2 = db.getPreparedStatement("SELECT ?"); sql2.setString(1, "hello"); - String s2 = DBConnection.exec(sql2, new SimpleSQLHandler()); + String s2 = DBConnection.exec(sql2, new SimpleSQLResult()); System.out.println( s2 ); db.close();