⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbccnn.java

📁 导出ORACLE数据库对象DDL语句的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            // Okay the next one is a candidate for many things
            if (st.hasMoreTokens()) {
                String token = st.nextToken();

                if (token != null) {
                    if (!token.equals(":") && !token.equals("/")) {
                        // Must be hostname
                        urlProps.put("HOST", token);

                        if (st.hasMoreTokens()) {
                            token = st.nextToken();
                        } else {
                            return null;
                        }
                    }

                    // Check for Port spec
                    if (token.equals(":")) {
                        if (st.hasMoreTokens()) {
                            token = st.nextToken();
                            urlProps.put("PORT", token);

                            if (st.hasMoreTokens()) {
                                token = st.nextToken();
                            }
                        }
                    }

                    if (token.equals("/")) {
                        if (st.hasMoreTokens()) {
                            token = st.nextToken();
                            urlProps.put("DBNAME", token);

                            // We're done
                            return urlProps;
                        } else {
                            urlProps.put("DBNAME", "");

                            return urlProps;
                        }
                    }
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }

        return urlProps;
    }	
	/**
	 * @Precondiction conn不能为空
	 * @Postcondition 创建JDBCCnn对象
	 * @JDBCException conn对象为空
	 */
	/*public JDBCCnn(int id, Connection conn) 
	throws JDBCException
	{
		
		if(conn==null){
			//.log(//.WARN,JDBCCnnMgrConstants.NULL_CONNECTION);
			throw new JDBCException(JDBCCnnMgrConstants.NULL_CONNECTION);
		}	
		this.id = id;
		status=JDBCCnn.FREE;
		this.conn = conn;
	}*/
	
	/**
	 * @Precondiction conn不能为空
	 * @Postcondition 创建JDBCCnn对象
	 * @JDBCException conn对象为空
	 */
	/*public JDBCCnn(int id, Connection conn,String url,String dbUser,String dbPassword) 
	throws JDBCException
	{
		
		if(conn==null){
			//.log(//.WARN,JDBCCnnMgrConstants.NULL_CONNECTION);
			throw new JDBCException(JDBCCnnMgrConstants.NULL_CONNECTION);
		}	
		this.id = id;
		status=JDBCCnn.FREE;
		this.conn = conn;
		
		try {
			initProperties(url);
		} catch (JDBCException e) {
			throw e;
		}
		setDbUser(dbUser);
		setDbPassword(dbPassword);	
	}*/
	
	/**
	 * @Precondiction conn不能为空
	 * @Postcondition 创建JDBCCnn对象
	 * @JDBCException conn对象为空
	 */
	public JDBCCnn(String poolID,Connection conn,String url,String dbUser,String dbPassword,String backDir) 
	throws JDBCException
	{
		
		if(conn==null){
			throw new JDBCException(JDBCCnnMgrConstants.NULL_CONNECTION);
		}	
		//this.id = id;
		status=JDBCCnn.FREE;
		this.conn = conn;
		this.parent =poolID;	
		/**
		try {
			initProperties(url);
		} catch (JDBCException e) {
			throw e;
		}
		setDbUser(dbUser);
		setDbPassword(dbPassword);	
		setBackDir(backDir);
		**/
	}
	
	/**
	 * Sets the dbPassword.
	 * @param dbPassword The dbPassword to set
	 */
	private void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;
	}
	
	/**
	 * Sets the dbUser.
	 * @param dbUser The dbUser to set
	 */
	private void setDbUser(String dbUser) {
		this.dbUser = dbUser;
	}
		
	/**
	 *  关闭jdbc连接
	 * @Precondition conn已经打开
	 * @Postcondition conn关闭
	 * @JDBCException 连接已经关闭
	 * */
	public void close()
	{
		try{
			//关闭连接对象
			if(!conn.isClosed())	conn.close();
		}catch(Exception e){
			//写日志
			Logger.log(Logger.ERROR,JDBCCnnMgrConstants.EXCEP_CLOSING_CONN);
		}
	}
	
	/**
	 * 执行更新语句
	 * @Precondition sql是符合标准sql语法的DML语句
	 * @Postcondition 执行SQL操作成功
	 * @param 要执行的SQL语句
	 * @return 返回受影响的行
	 * @JDBCException 执行sql语句发生异常
	 * */
	public int executeUpdate(String sql)
	throws JDBCException{
		int rs=0;
		Statement tmpStmtUpdate=null;
		synchronized(transLocker){
			sqlString=sql;
			startTime=Calendar.getInstance();
			endTime=null;
			status=JDBCCnn.EXECUTING;
			try{
				tmpStmtUpdate=this.conn.createStatement();
				rs=tmpStmtUpdate.executeUpdate(sql);
				//tmpStmtUpdate.close();
			}catch(Exception e){
				e.printStackTrace();
				status=JDBCCnn.TAKING;			
				String errMsg=ExceptionHandle.getMessage(e);
				if(errMsg.indexOf("Communications link failure")>=0){
					//关闭该连接
					JDBCPoolManager instance=JDBCPoolManager.getInstance();
					instance.closeConnectin(this);	
				}	
				throw new JDBCException(errMsg);
			}
			endTime=Calendar.getInstance();
			status=JDBCCnn.TAKING;			
			transLocker.notifyAll();
			//System.out.println("execute update:transLocker.notifyAll()"+System.currentTimeMillis());
			return rs;
		}
	}
	
	/**
	 * 执行查询语句
	 * @Precondiction sql是符合标准sql语法的查询语句
	 * @Postcondition ResultSetType是下列三种情况之一:TYPE_FORWARD_ONLY,TYPE_SCROLL_INSENSITIVE,TYPE_SCROLL_SENSITIVE
	 * @param sql 要执行的查询语句
	 * @param ResultSetType 查询类型
	 * @return 返回查询结果集
	 * @JDBCException 执行sql查询时发生异常
	 * */
	public ResultSet executeQuery(String sql,int ResultSetType)
	throws JDBCException{		
		ResultSet rs=null;
		Statement tmpStmtQuery=null;
		synchronized(transLocker){
			sqlString=sql;
			startTime=Calendar.getInstance();
			endTime=null;
			status=JDBCCnn.EXECUTING;
			try{
				Statement stmt=conn.createStatement(ResultSetType,ResultSet.CONCUR_UPDATABLE);
				rs=stmt.executeQuery(sql);
				//stmt.close();
			}catch(Exception e){
				e.printStackTrace();
				status=JDBCCnn.TAKING;			
				String errMsg=ExceptionHandle.getMessage(e);
				if(errMsg.indexOf("Communications link failure")>=0){
					//关闭该连接
					JDBCPoolManager instance=JDBCPoolManager.getInstance();
					instance.closeConnectin(this);	
				}	
				throw new JDBCException(errMsg);
			}
			endTime=Calendar.getInstance();
			status=JDBCCnn.TAKING;
			transLocker.notifyAll();
			return rs;
		}
	}
	
	/**
	 * 执行查询语句
	 * @Precondiction sql是符合标准sql语法的查询语句
	 * @param sql 要执行的查询语句
	 * @return 返回查询结果集
	 * @JDBCException 执行sql查询时发生异常
	 * */
	public ResultSet executeQuery(String sql)
	throws JDBCException{
		return this.executeQuery(sql,ResultSet.TYPE_FORWARD_ONLY);
	}

	/**
	 * 设置连接的自动提交模式
	 * @param true:连接设置为自动提交  false:连接设置为非自动提交
	 * @JDBCException 设置自动提交时发生异常
	 * */
	public void setAutoCommit(boolean autoCommit)
	throws JDBCException
	{
		try{
			conn.setAutoCommit(autoCommit);
		}catch(Exception e){
			//.log(//.ERROR,JDBCCnnMgrConstants.EXCEP_SETING_COMMIT);
			String errMsg=ExceptionHandle.getMessage(e);
			throw  new JDBCException(errMsg);
		}
	}

	/**
	 * 返回连接的自动提交模式
	 * @JDBCException 获得自动提交模式时发生异常
	 * */	
	public boolean getAutoCommit()
	throws JDBCException{
		try{
			return conn.getAutoCommit();
		}catch(Exception e){
			//.log(//.ERROR,JDBCCnnMgrConstants.EXCEP_GETTING_COMMIT);
			String errMsg=ExceptionHandle.getMessage(e);
			throw new JDBCException(errMsg);
		}	
	}  
	
	/**
	 * 提交事务
	 * @Postcondiction 连接的AutoCommit设置为false
	 * @JDBCException 事务提交时发生异常
	 * */
	public void commit()
    throws JDBCException
    {
    	try{
    		conn.commit();
    	}catch(Exception e){
    		//.log(//.ERROR,JDBCCnnMgrConstants.EXCEP_TRANS_COMMIT);
			String errMsg=ExceptionHandle.getMessage(e);
    		throw new JDBCException(errMsg);    	
    	}
    }
    
    /**
     * 创建预编译statement
     * @Precondition sqlstr不能为空
     * @Postcondition 创建预编译statement
     * @JDBCException 创建预编译statement时候发生异常
     * */
    protected PreparedStatement prepareStatement(String sql)
    throws JDBCException
    {
		try {
			sqlString=sql;
			return conn.prepareStatement(sql);	
		} catch (SQLException e) {
			String errMsg=ExceptionHandle.getMessage(e);
			throw new JDBCException(errMsg);
		}
    }
    
    /**
     * 创建预编译statement
     * @Precondition sqlstr不能为空
     * @Postcondition 创建预编译statement
     * @JDBCException 创建预编译statement时候发生异常
     * */
    protected CallableStatement prepareCall(String sql)
    throws JDBCException
    {
		try {
			sqlString=sql;
			return conn.prepareCall(sql);	
		} catch (SQLException e) {
			String errMsg=ExceptionHandle.getMessage(e);
			throw new JDBCException(errMsg);
		}
    }
    
    /**
     * 回滚事务
     * @Postcondition 连接的AutoCommit设置为false
     * @JDBCException 事务回滚时发生异常
     * */
    public void rollback()
    throws JDBCException
    {
    	try{
    		conn.rollback();
    	}catch(Exception e){
    		//.log(//.ERROR,JDBCCnnMgrConstants.EXCEP_TRANS_ROLLBACK);
			String errMsg=ExceptionHandle.getMessage(e);
    		throw new JDBCException(errMsg);
    	}
    }
              
	public static void main(String[] args) {
		String driverClassName = "org.gjt.mm.mysql.Driver";
		String url = "jdbc:mysql://122.136.14.186:3306/cite";
		String dbUser = "root";
		String dbPassword = "toor";
		String osUser="root";
		Connection	conn;
		int id=0;
		String backDir="/usr/local/mysql";
		boolean notStop=true;
		int count=25;
		JDBCCnn jcnn=null;		
		try {
			Class.forName(driverClassName);
			conn = DriverManager.getConnection(url,dbUser,dbPassword);
			//jcnn=new JDBCCnn(id,conn,url,dbUser,dbPassword,backDir,osUser) ;
			//jcnn.executeUpdate("backup table t4 to '/usr/local/mysql/test'");
		} catch (Exception e) {
			e.printStackTrace();
			//System.out.println(e.getMessage());	
		}
		
		
	}
	/**
	 * 返回endTime,如果为空则表示sql语句没有执行完毕.
	 * @return Calendar
	 */
	public Calendar getEndTime() {
		return endTime;
	}

	/**
	 * 返回sqlString.
	 * @return String
	 */
	public String getSqlString() {
		return sqlString;
	}

	/**
	 * 返回startTime.
	 * @return Calendar
	 */
	public Calendar getStartTime() {
		return startTime;
	}

	/**
	 * 返回status.
	 * @return int
	 */
	public int getStatus() {
		return status;
	}

	/**
	 * Returns the backupRestoreTimout.
	 * @return int
	 */
	public int getBackupRestoreTimout() {
		return backupRestoreTimout;
	}

	/**
	 * Sets the backupRestoreTimout.
	 * @param backupRestoreTimout The backupRestoreTimout to set
	 */
	public void setBackupRestoreTimout(int backupRestoreTimout) {
		this.backupRestoreTimout = backupRestoreTimout;
	}

	/**
	 * Sets the osPassword.
	 * @param osPassword The osPassword to set
	 
	public void setOsPassword(String osPassword) {
		this.osPassword = osPassword;
	}*/

	/**
	 * Sets the osUser.
	 * @param osUser The osUser to set
/
	public void setOsUser(String osUser) {
		this.osUser = osUser;
	}	 *

	/**
	 * Returns the backDir.
	 * @return String
	 */
	public String getBackDir() {
		return backDir;
	}

	/**
	 * Returns the dbPassword.
	 * @return String
	 */
	public String getDbPassword() {
		return dbPassword;
	}

	/**
	 * Returns the dbUser.
	 * @return String
	 */
	public String getDbUser() {
		return dbUser;
	}

	/**
	 * Returns the osPassword.
	 * @return String
	 
	public String getOsPassword() {
		return osPassword;
	}*/

	/**
	 * Returns the osUser.
	 * @return String
	 
	public String getOsUser() {
		return osUser;
	}*/

	/**
	 * Sets the backDir.
	 * @param backDir The backDir to set
	 */
	
	public void setBackDir(String backDir) {
		this.backDir = backDir;
	}


}


class Executor {

	/* 
	 * 返回命令执行结果
	 */
	public static String loadStream(InputStream in) throws IOException {
		int ptr = 0;
		in = new BufferedInputStream(in);
		StringBuffer buffer = new StringBuffer();
		while ((ptr = in.read()) != -1) {
			buffer.append((char) ptr);
		}
		String result=buffer.toString();		
		return result;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -