Package Mypackage;import Java.lang.reflect.field;import Java.sql.connection;import java.sql.drivermanager;import Java.sql.resultset;import Java.sql.resultsetmetadata;import Java.sql.sqlexception;import Java.text.simpledateformat;import Java.util.arraylist;import Java.util.hashmap;import Java.util.List;import Java.util.map;public class Jdbcutils {private final string USERNAME = "root";p rivate final String PWD = "Admin";p rivate fi nal String DRIVER = "Com.mysql.jdbc.Driver";p rivate Connection connection;private java.sql.PreparedStatement Preparedstatement;private ResultSet resultset;private String URL = "Jdbc:mysql://localhost:3306/mydb";p ublic Jdbcutils () {try {class.forname (DRIVER); SYSTEM.OUT.PRINTLN ("Registration driver Success");} catch (Exception e) {///todo:handle exception}}//definition get link to database public Connection getconnection () {try {Connection = Driverma Nager.getconnection (URL, USERNAME, PWD);} catch (Exception e) {//Todo:handle Exception}return connection;} /** * * @param SQL * @param params * @return * @Throws SQLException */public boolean updatebypreparestatement (String sql, list<object> params) throws SQLException {Boolean flag = false;int result = -1;//The number of rows affected by int index = 1;preparedstatement = Connection.preparestatement ( SQL); if (params! = null &&!params.isempty ()) {for (int i = 0; i < params.size (); i++) {Preparedstatement.setob Ject (index++, Params.get (i));}} result = Preparedstatement.executeupdate (); flag = result > 0? True:false;return Flag;} /** * Query returns a single record * * @param SQL * @param params * @return * @throws SQLException */public map<string, object> Findsim Pleresult (String sql, list<object> params) throws SQLException {map<string, object> Map = new hashmap< String, object> (), int index = 1;preparedstatement = connection.preparestatement (sql), if (params! = null &&!PA Rams.isempty ()) {for (int i = 0; i < params.size (); i++) {Preparedstatement.setobject (index++, Params.get (i));}} ResultSet = Preparedstatement.executequery ();//Return queryThe result resultsetmetadata MetaData = Resultset.getmetadata (); int col_len = Metadata.getcolumncount (); while (Resultset.next ( ) {for (int i = 0; i < Col_len; i++) {String col_name = metadata.getcolumnname (i + 1); Object col_values = resultset.ge TObject (col_name); if (col_values = = null) {col_values = "";} Map.put (Col_name, col_values);}} return map;} /** * Find multiple records * * @return */public list<map<string, object>> findmoreresultstring (String SQL,LIST<OBJECT&G T params) throws SQLException {list<map<string, object>> List = new arraylist<map<string, object> > (), int index = 1;preparedstatement = connection.preparestatement (sql), if (params! = null &&!params.isempty ( ) {for (int i = 0; i < params.size (); i++) {Preparedstatement.setobject (index++, Params.get (i));}} ResultSet = Preparedstatement.executequery ();//Return query result resultsetmetadata MetaData = Resultset.getmetadata (); int Col_ Len = Metadata.getcolumncount (); while (Resultset.next ()) {map<string, object&Gt Map = new hashmap<string, object> (), for (int i = 0; i < Col_len; i++) {String col_name = Metadata.getcolumnname (i + 1); Object col_values = Resultset.getobject (col_name); if (col_values = = null) {col_values = "";} Map.put (Col_name, col_values);} List.add (map);} return list;} Using reflection mechanism/** * Access database through reflection mechanism * * @param SQL * @param params * @param cls * @return * @throws SQLException * @throws Excepti On * @throws illegalaccessexception */public <T> list<t> findmore (String sql, list<object> params, Clas S<t> cls) throws SQLException, Exception {list<t> List = new arraylist<t> (); int index = 1;object Resultob ject=null;preparedstatement = connection.preparestatement (sql); if (params! = null &&!params.isempty ()) {for ( int i = 0; I < params.size (); i++) {preparedstatement.setobject (index++, Params.get (i));}} ResultSet = Preparedstatement.executequery ();//Return query result resultsetmetadata MetaData = Resultset.getmetadata (); int Col_ Len = Metadata.getcolumncoUNT (), while (Resultset.next ()) {resultobject = Cls.newinstance (), for (int i = 0; i < Col_len; i++) {String col_name = m Etadata.getcolumnname (i+1); Object col_values = Resultset.getobject (col_name); if (col_values = = null) {col_values = "";} Field field = Cls.getdeclaredfield (col_name); field.setaccessible (true); Field.set (Resultobject, col_values);} List.add ((T) resultobject);} return list;} /** * Close database connection */public void Releaseconnection () {if (resultset! = null) try {resultset.close ();} catch (SQLException e) {// TODO auto-generated catch Blocke.printstacktrace ();} if (PreparedStatement! = null) try {preparedstatement.close ();} catch (SQLException e) {//TODO auto-generated catch Blocke . Printstacktrace ();} if (connection! = null) try {connection.close ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktra CE ();}} /** * * @param args * @throws SQLException */public static void Main (string[] args) throws SQLException {jdbcutils jdbcut Ils = new jdbcutils (); jdbcutils.getconnectIon ();/* String sql = "INSERT into UserInfo (USERNAME,PWD) VALUES (?,?)"; * list<object> params = new arraylist<object> (); Params.add ("admin"); * Params.add ("admin"); Boolean flag = * jdbcutils.updatebypreparestatement (sql, params); * SYSTEM.OUT.PRINTLN (flag); */string sql = "SELECT * from UserInfo where id=?"; String SQL1 = "SELECT * from UserInfo"; list<object> params = new arraylist<object> ();/*params.add (1); Map map = Jdbcutils.findsimpleresult (sql, params); try {List List = jdbcutils.findmoreresultstring (SQL1, NULL); SYSTEM.OUT.PRINTLN (map); SYSTEM.OUT.PRINTLN (list);} catch (Exception e) {e.printstacktrace ();} finally {jdbcutils.releaseconnection ();} */try {System.out.println ("---fsdfas"); List<userinfo> Userlist=jdbcutils.findmore (SQL1, NULL, userinfo.class); SYSTEM.OUT.PRINTLN ("---" +userlist);} catch (Exception e) {e.printstacktrace ();} finally {jdbcutils.releaseconnection ();}}}