Database simple tool encapsulation, query, update, pagination to write a separate query, a lot of advice __ database

Source: Internet
Author: User
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);
 }
			
		}
	}
}


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.