JDBC Basedao class

Source: Internet
Author: User
Package com.it.util;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;

Import Java.sql.ResultSet; /** * Database Operations Tool class * 7 Properties 4 Methods (Connection method Close resource method General Query method universal deletion and modification method)/public class Basedao {private static final String DRIVER =
	"Oracle.jdbc.driver.OracleDriver";
	private static final String URL = "Jdbc:oracle:thin: @localhost: 1521:ORCL";
	private static final String USERNAME = "System";
	private static final String PASSWORD = "OK";
	Con public Connection con = null;
	pstmt public PreparedStatement pstmt = null;
	RST public ResultSet rst = null;
			* * Get connection method/public void Getcon () {try {class.forname (DRIVER);
		con = drivermanager.getconnection (URL, USERNAME, PASSWORD);
		catch (Exception e) {e.printstacktrace ();
			}/* * Shut down resource from inside to outside/public void CloseAll () {try {if (rst!= null) {rst.close ();
		} catch (Exception e) {e.printstacktrace ();
	try {if (pstmt!= null) {pstmt.close ();		} catch (Exception e) {e.printstacktrace ();
			try {if (con!= null) {con.close ();
		} catch (Exception e) {e.printstacktrace (); }/* * sql:sql statement * Params: parameter (in populate SQL.)
			Parameter array) */public void Commonquery (String sql,object[]params) {try {//Get connection This.getcon ();
			Gets the Execute SQL object pstmt = con.preparestatement (sql); Right.
			Processing, subscript starting from 1 for (int i = 0; i < params.length i++) {pstmt.setobject (i+1, params[i]);
		//Get result set rst = Pstmt.executequery ();
		catch (Exception e) {e.printstacktrace ();
		} public int commonupdate (String sql,object[]params) {int r =-1;
			try {//Get connection This.getcon ();
			Gets the Execute SQL object pstmt = con.preparestatement (sql); Right.
			Process for (int i = 0; i < params.length i++) {pstmt.setobject (i+1, params[i]);
		//Get the affected row number R = Pstmt.executeupdate ();
		catch (Exception e) {e.printstacktrace ();
	} return R;
 }
}

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.