Basic approach to Oracle database connectivity

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

Import java.sql.SQLException;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import javax.naming.NamingException;


Import Javax.sql.DataSource;
	public class Dbutil {private static dbutil dbutil=null; 
	Private final String driver= "Oracle.jdbc.driver.OracleDriver";
	Private final String url= "Jdbc:oracle:thin: @localhost: 1521:ORCL";
	Private final String user= "Ndy";
	Private final String psw= "123456";
	 Public Connection Conn=null;
	 PreparedStatement Pstm=null;
	ResultSet Rs=null;
		public static Dbutil newinstance () {if (dbutil==null) {dbutil=new dbutil ();
	return dbutil; Public Connection getconnection () {//try {//Context context=new InitialContext ();//DataSource ds= (Datasou
RCE) context.lookup ("Java:comp/env/jdbc/easyui_pro");
Get connection//Conn=ds.getconnection () from connection pool;
catch (Exception e) {TODO auto-generated Catch block//E.printstacktrace ();
			try {class.forname (DRIVER);
		conn = drivermanager.getconnection (URL, user, PSW);
		catch (SQLException e) {e.printstacktrace ();
		catch (ClassNotFoundException e) {e.printstacktrace ();
	Return conn;
			public int Sqlinsert (String sql) {int num=0;
			Conn=this.getconnection ();
				try {pstm=conn.preparestatement (SQL);
			Num=pstm.executeupdate ();
			catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
		return num;
		public int Sqlinsert (String sql,object[] param) {int num=0;
		Conn=this.getconnection ();
			try {pstm=conn.preparestatement (SQL);
			SetParam (pstm, param);
		Num=pstm.executeupdate ();
		catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
	return num;
		public int sqldelete (String sql) {conn=this.getconnection ();
		int num=0; try {pstm=conn.preparestatement(SQL);
		Num=pstm.executeupdate ();
		catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
		}finally{This.pstmclose ();
	return num;
		public int Sqldelete (String sql,object[] param) {conn=this.getconnection ();
		int num=0;
			try {pstm=conn.preparestatement (SQL);
			SetParam (pstm, param);
		Num=pstm.executeupdate ();
		catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
		}finally{This.pstmclose ();
	return num;
	public int sqlupdate (String sql) {return sqlupdate (SQL, NULL);
		public int sqlupdate (String sql,object[] objects) {int num=0;
			try {conn=this.getconnection ();
			Pstm=conn.preparestatement (SQL);
			SetParam (Pstm, objects);
		Num=pstm.executeupdate ();
		catch (SQLException e) {e.printstacktrace ();
			}finally{This.pstmclose ();
		This.connclose ();
	return num;
	Public ResultSet sqlquery (String sql) {return sqlquery (SQL, NULL); } PubLic ResultSet sqlquery (String sql,object[] objects) {ResultSet rs=null;
			try {conn=this.getconnection ();
			Pstm=conn.preparestatement (SQL);
				if (Objects!=null) {for (int i=0;i<objects.length;i++) {pstm.setobject (i+1, objects[i]);
		} rs=pstm.executequery ();
		catch (SQLException e) {e.printstacktrace ();
	} return RS; } private void SetParam (PreparedStatement pstm,object[] objects) throws sqlexception{if (Objects!=null) {for (int i
			=0;i<objects.length;i++) {pstm.setobject (i+1, objects[i]);
			}} public void Connclose () {if (conn!=null) {try {conn.close ();
			catch (SQLException e) {e.printstacktrace ();
			}} public void Rsclose () {if (rs!=null) {try {rs.close ();
			catch (SQLException e) {e.printstacktrace ();
			}} public void Pstmclose () {if (pstm!=null) {try {pstm.close ();
			catch (SQLException e) {e.printstacktrace ();
 }
		}
	}

}

Related Article

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.