public class Daoimpl implements Daointer {
Private Connection conn;//Get database connection
@Override
Public list<map<string, string>> selectentity (String sql, object[] objects, string[] items) {
conn = Sqlconnection.getconnection ();
list<map<string, string>> list = null;//declares a list collection to hold the results of the query
try {
PreparedStatement PST = conn.preparestatement (SQL);
if (objects! = null) {//If a condition is included
for (int i = 0; i < objects.length; i++) {
Pst.setobject (i + 1, objects[i]);//Add conditions to PreparedStatement
}
}
ResultSet rst = Pst.executequery ();//query results and store in result set
List = new arraylist<map<string, string>> ();//Instantiate Collection Object
Map<string, string> map;//instantiate a map object to hold a query to the data
while (Rst.next ()) {
Map = new hashmap<string, string> ();
for (int i = 0; i < items.length; i++) {
key--column name, value--the value of the column
Override method to convert object to string to prevent object from being null times wrong
Map.put (Items[i], rules.objecttostring (Rst.getobject (items[i)));
}
List.add (map);//Put a piece of data into the list collection
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} finally {
Sqlconnection.closeconnection (conn);
}
return list;
}
@Override
public int changeentity (String sql, object[] objects) {
conn = Sqlconnection.getconnection ();
int result =-1;
try {
PreparedStatement PST = conn.preparestatement (SQL);
if (objects! = null) {
for (int i = 0; i < objects.length; i++) {
Assign a value to the parameters used in each SQL statement, and order one by one corresponds to
Pst.setobject (i + 1, objects[i]);
}
}
result = Pst.executeupdate ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} finally {
Sqlconnection.closeconnection (conn);
}
return result;
}}
Using MySQL to interact with a database (ii)