Simple Java code for database processing and related operations

package util;

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/ **
* DAO base layer for processing and database-related operations
*
* 1. General method for updating
* 2. Generic query methods
* /
public class BaseDao {
/ *
General update methods:
1. the SQL
2. Parameters Different

Process:
1. Get database connection object
2. Pretreatment SQL statements
3. administering SQL statement parameters [Key]
4. execute SQL statements

Methods Analysis:
permission to modify public
return value type int number of rows affected by the operation of the database
update method name
list of formal parameters:
1. SQL
2. SQL statement parameter corresponding to
a type parameter uncertainty.
The Java classes are all the Object class subclasses
select Object as the data type
b. The number of uncertain parameters
of the underlying array
Object [] parameters

public int update(String sql, Object[] parameters)

*/

/ **
* Update method can handle insert, update, delete the corresponding SQL statements
*
* @param String type SQL statements SQL
* @param parameters corresponding to the current list of parameters SQL statement
line * @return MySQL database operations affected number
* /
public int Update (SQL String, Object [] Parameters) {
int the AffectedRows = 0;
the PreparedStatement the preparedStatement = null;
Connection Connection JdbcUtil.getConnection = ();

try {
preparedStatement = connection.prepareStatement(sql);

/ *
It should be the SQL statement parameter assignment
here need to determine the SQL statement? How many, how many parameters?

One way to use the JDBC provided, parameter metadata! ! !
* /

// Get the number of parameters corresponding to the SQL statement
int parameterCount = preparedStatement.getParameterMetaData () getParameterCount ( ).;

// SQL statements for prior parameter assignment, we need to determine a number of conditions.
IF (! = null && parameterCount Parameters == parameters.length) {
// use for loop through the array, the SQL statement corresponding to the processing parameters
for (int I = 0; I <parameters.length; I ++) {
PreparedStatement.setObject ( +. 1 I, Parameters [I]);
}
}

affectedRows = preparedStatement.executeUpdate();

} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(connection, preparedStatement);
}

return affectedRows;
}

/ *
General Query analysis methods
authority public modifier
generic:
T
Return Type
void Pass
int Pass
List <T>
method Name:
Query
form parameter list:
1. String SQL
2. the SQL statement corresponding to the parameter list, Object [] the parameters
3. Not sure what
a. corresponding to the specific constraints of generic data types
b. parameter determines the current SQL statement corresponding to specific queries which class object

T t okay wasted space used his acquired Class class object, do whatever they want! ! !
Class ... initParameterTypes

Class <T> CLS
Person.class
T ==> the Person
CLS ==> using Reflected can do whatever
public <T> List <T> Query (SQL String, Object [] Parameters, Class <T> CLS)
* /

/ **
* generic query method, the return value is a collection of List, which holds the data type is designated from Class <T>
*
* @param SQL statements to be processed SQL select statement
* @param parameters corresponding to the current SQL statement parameter erected
* @param cls class class object specified data types are currently in need of treatment, does not include the basic data types []
* @param <T> generic, the data type for the current operation in the end of which a
* @return List collection, with the specified data
* /
public <T> List <T> Query (SQL String, Object [] Parameters, Class <T> CLS) {
the ResultSet the resultSet = null;
the PreparedStatement the preparedStatement = null;

List<T> list = new ArrayList<>();

Connection connection = JdbcUtil.getConnection();

try {
preparedStatement = connection.prepareStatement(sql);

int parameterCount = preparedStatement.getParameterMetaData().getParameterCount();

if (parameters != null && parameterCount == parameters.length) {
for (int i = 0; i < parameterCount; i++) {
preparedStatement.setObject(i + 1, parameters[i]);
}
}

// SQL statement is executed, the result set object obtained
resultSet = preparedStatement.executeQuery ();

// Get the result set metadata
ResultSetMetaData metaData = resultSet.getMetaData ();

// get the current result set number of fields
int columnCount = metaData.getColumnCount ();

// analysis data
the while (ResultSet.next ()) {
T = T cls.getConstructor (null) .newInstance (null);

for (int i = 1; i <= columnCount ; i++) {
// 获取字段名
String columnName = metaData.getColumnName(i);

// remove the corresponding data, corresponding to the assigned class object member variables
BeanUtils.setProperty (T, columnName, ResultSet.getObject (columnName));
}

list.add(t);
}
} catch (SQLException | NoSuchMethodException | InstantiationException
| IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(connection, preparedStatement, resultSet);
}

return list;
}


}

Guess you like

Origin www.cnblogs.com/kongnote/p/11563720.html