Java tutorial
/******************************************************************************* * Copyright (c) 2012 Cho Hyun Jong. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Cho Hyun Jong - initial API and implementation ******************************************************************************/ package com.hangum.tadpole.rdb.core.editors.main; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import com.hangum.tadpole.define.Define; /** * sql text util * * @author hangum * */ public class SQLTextUtil { private static final Logger logger = Logger.getLogger(SQLTextUtil.class); /** * ? * * 1. (Define.SQL_DILIMITER(;)) ? ?? ? ? => ? . * 2. ? ?? ? ?? ?. * * @param query * @param cursorPosition * @return */ public static String executeQuery(String query, int cursorPosition) {//throws Exception { if (query.split(Define.SQL_DILIMITER).length == 1 || query.indexOf(Define.SQL_DILIMITER) == -1) { return StringUtils.trimToEmpty(query); } String[] querys = StringUtils.split(query, Define.SQL_DILIMITER); // if(logger.isDebugEnabled()) { // logger.debug("=====[query]" + query + " =====[mouse point]" + cursorPoint); // } int queryBeforeCount = 0; for (int i = 0; i < querys.length; i++) { // dilimiter ? +1 . int firstSearch = querys[i].length() + 1; queryBeforeCount += firstSearch; if (cursorPosition <= queryBeforeCount) { if (logger.isDebugEnabled()) logger.debug("[cursorPosition]" + cursorPosition + "[find postion]" + queryBeforeCount + "[execute query]" + StringUtils.trim(querys[i])); return StringUtils.trim(querys[i]); } } if (logger.isDebugEnabled()) logger.debug("[last find execute query]" + StringUtils.trim(querys[querys.length - 1])); return StringUtils.trim(querys[querys.length - 1]); } /** * ? ?? . * * @param query * @return */ public static String delLineChar(String query) { return query.replaceAll("(\r\n|\n|\r)", ""); } public static void main(String[] args) { String query = " SELECT store_id, manager_staff_id, address_id, last_update " + " FROM store;" + " " + "" + "" + " SELECT staff_id, first_name, last_name, address_id, picture, email, store_id, active, username, password, last_update" + " FROM staff;" + "" + " SELECT city_id, city, country_id, last_update" + " FROM city;"; try { String exeQuery = SQLTextUtil.executeQuery(query, 30); System.out.println(exeQuery); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }