Java JDBC Database Connection code Encapsulation __ Database

Source: Internet
Author: User

a good encapsulation JDBC database class

Package cn.com.util;
Import java.sql.*;

Import java.util.*;
	/** * Database Operations Tool Class * * @author Administrator */public class Dbutil {private static Connection con = null;//Connection object private static PreparedStatement PSMT = null;//Precompiled Object private static ResultSet rs = null;//result set object private static CALLABL Estatement CSMT = null;//Process Object/** * Get Connection Object * @return/public static Connection getconnetion () {try {Class.
			forname ("Com.mysql.jdbc.Driver");
		con = DriverManager getconnection ("Jdbc:mysql://localhost:3306/mytest", "root", "gc7923959");
		catch (ClassNotFoundException e) {//TODO auto-generated catch block System.out.println ("Driver not Found");
		catch (Exception e) {//TODO auto-generated catch block System.out.println ("Database connection failed");
	return con; /** * * @param SQL * @return/public static PreparedStatement getpreparedstatement (String sql) {con = g
		Etconnetion ();
		try {psmt = con.preparestatement (sql);
catch (SQLException e) {			TODO auto-generated Catch block E.printstacktrace ();
	return PSMT; /** * Query gets all * @param SQL * @return result set/public static ResultSet Getresultset (String sql) {psmt = Getprepareds
		Tatement (SQL);
		try {rs = Psmt.executequery ();
		catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
	} return RS; /** * Update operation * @param SQL * @param params * @return The number of rows affected * * public static int executeupdate (String sql, L
		
		ist<object> params) {int count = 0;
		PSMT = getpreparedstatement (sql);
		Bindpramas (PSMT, params);
		try {count = Psmt.executeupdate ();
		catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
		finally {CloseAll ();
	return count; /** * Query with parameters * @param SQL * @param params * @return result set/public static ResultSet executequery (String sq
		L, list<object> params) {PSMT = getpreparedstatement (sql); if (params!= null) {bIndpramas (PSMT, params);
		try {rs = Psmt.executequery ();
		catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
		} System.out.println (PSMT);
	Return RS;
		public static CallableStatement getcallablestatement (String sql) {try {csmt = Getconnetion (). preparecall (SQL);
		catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();

	return CSMT;
		public static Object Executecallobject (String sql, Map<integer, object> params) {Object obj = null;
			try {int index = 0;//input parameter subscript csmt = getcallablestatement (sql); for (int i = 1; I <= params.size (); i++) {if (Params.get (i). toString (). Equals ("Integer")) {CSMT.REGISTEROUTP
					Arameter (i, Types.integer);
				index = i;
				else {csmt.setobject (i, Params.get (i));
			} csmt.execute ();
		obj = Csmt.getobject (index); catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
	return obj;
		public static void Bindpramas (PreparedStatement psmt, list<object> params) {int index = 0;
				if (params!= null) {for (Object p:params) {try {psmt.setobject (index + 1, p);
				catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();

			} index++;

			}} public static void CloseAll () {try {if (con!= null) {con.close ();
			} if (psmt!= null) {psmt.close ();
			} if (rs!= null) {rs.close ();
		The catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }

	}
}


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.