public class Dbutil {/** * Get database connection/private static Connection getconnection () {String URL = "jdbc:mysql:/
/localhost:3306/test1 ";
String user = "root";
String password = "root";
String Driverclass = "Com.mysql.jdbc.Driver";
Connection con = null;
try {try {class.forname (driverclass);
catch (ClassNotFoundException e) {System.out.println ("Drive load Failed");
E.printstacktrace ();
con = drivermanager.getconnection (url, user, password);
catch (SQLException e) {e.printstacktrace ();
return con; /** * Shut down database resource/public static void close (Connection con, preparedstatement ps,resultset rs) {if (Con!=nul
L) {try {con.close ();
catch (SQLException e) {e.printstacktrace ();
} if (Ps!=null) {try {ps.close ();
catch (SQLException e) {e.printstacktrace ();
} if (Rs!=null) {try {rs.close (); catch (SQLException e) {E.printstacktraCE (); \/** * Get query Data/public static list<map<string,object>> GetData (String sql, Object...array
s) {list<map<string,object>> List = new arraylist<map<string,object>> ();
Connection con = getconnection ();
PreparedStatement Ps=null;
ResultSet rs = null;
try {PS = con.preparestatement (SQL);
if (Arrays!=null) {for (int i=0;i<arrays.length;i++) {ps.setobject (i+1, arrays[i]);
rs = Ps.executequery ();
int size = Rs.getmetadata (). getColumnCount ();
while (Rs.next ()) {map<string,object> Map = new hashmap<string, object> (); for (int i=0;i<size;i++) {//each row as a map String columnName = Rs.getmetadata (). Getcolumnlabel (i+1);//Get the field name of the query result
Called Object Columnvalue = Rs.getobject (i+1);//field name corresponding to the value Map.put (ColumnName, Columnvalue);
} list.add (map);
} catch (SQLException e) { E.printstacktrace ();
}finally{Close (CON,PS,RS);
} return list; /** * Update data (add, delete, change) */public static void UpdateData (String sql, object...arrays) {Connection con = Getco
Nnection ();
PreparedStatement Ps=null;
if (con!=null) {try {PS = con.preparestatement (SQL); if (ps!=null) {if (arrays!=null) {for (int i=0;i<arrays.length;i++)//Set the parameter value in the SQL statement ps.setobject (i+1, array
S[i]);
} ps.executeupdate ();
}else{System.out.println ("statement execution failed");
} catch (SQLException e) {e.printstacktrace ();
}finally{Close (con,ps,null);
}
}
}
}