domino 數據庫連接

/**
	 * 創建conn連接
	 * @return
	 */
	public boolean CONN(){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(url,mysqluser,mysqlpass);

			statement=conn.createStatement();
			return true;
		}catch(Exception e)
		{
			System.out.println(e);
			return false;
		}	

	}
	/**
	 * 關閉數據庫
	 */
	public void closedb(){

		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

/**
	 * 查詢語句
	 * @param sql
	 * @return
	 */
	public ResultSet Select(String sql){
		//if(this.CONN()){
		try{
			rs=statement.executeQuery(sql);
		}
		catch(Exception e){}
		return rs;
		//}
		//else
		//return null;

	}


	/**
	 * 執行SQL語句
	 * @param sql
	 * @return
	 */
	public boolean ExcuteSql(String sql){
		boolean x=false;
		//if(this.CONN())
		//{
		try{
			statement.executeUpdate(sql);
			x=true;
		}
		catch(Exception e){e.printStackTrace();
		}
		//}
		return x;

	}

	/**
	 * 執行數據庫事務
	 * @param al
	 * @return
	 */
	public boolean ExcuteTrans(ArrayList al){
		boolean x=false;
		//if(this.CONN()){

		try{
			conn.setAutoCommit(false);
			for(int i=0;i<al.size();i++){
				String y=al.get(i).toString();
				statement.execute(y);
			}
			conn.commit();
			conn.setAutoCommit(true);
			x=true;	
		}catch(Exception e){
			try{
				conn.rollback();
				conn.setAutoCommit(true);
			}
			catch(Exception ex){}
		}

		return x;
		//}
		//return x;

	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章