Using MySQL to interact with a database (ii)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.