public static void showProcedureNameList() {
		Connection connection = null;
		ResultSet resultSet = null;
		try {
			Class.forName(className);
			connection = DriverManager.getConnection(url, user, password);
			resultSet = connection.getMetaData().getProcedures(null, connection.getMetaData().getUserName(), null);
			ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
			while (resultSet.next()) {
				if (resultSet.getInt(8) == 1) { ⁄⁄ StoredProcedure
					System.out.println("Procedure Name : " + resultSet.getString(3));
				} 
				for (int i = 1; i < resultSetMetaData.getColumnCount(); i++) {
					System.out.println("\t" + i + ". ColumnName : " + resultSetMetaData.getColumnName(i));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null) {
					resultSet.close();
				}
				
				if (connection != null) {
					connection.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}


ResultSetMetaData 정보를 통하여 8번째 인덱스값이 프로시저 타입을 가져오는 인덱스인걸 알수가 잇습니다. 

프로시저같은경우에는 타입이 1번이 되겠고 함수는 2번이 되겠습니다.

블로그 이미지

행복그리고..

,
     public static void run() {
		Connection conn = null;
		ResultSet rs = null;
		try {
			String theClassName = "", 
					  theURL = "", 
					  theUser = "", 
					  thePassword = "", 
					  theFunctionName = "";
			
			Class.forName(theClassName);
			conn = DriverManager.getConnection(theURL, theUser, thePassword);
			DatabaseMetaData dbmd = conn.getMetaData();
			rs = dbmd.getProcedureColumns(null, null, theFunctionName, null);
			ResultSetMetaData rsmd = rs.getMetaData();
			
			int columnIndex = 1;
			boolean setNoCount = true;
			String productName = dbmd.getDatabaseProductName();
			while (rs.next()) {
				if (productName.indexOf("Microsoft") != -1) {
					if (getParameterType(rs.getInt(5)).equalsIgnoreCase("Return") && setNoCount == true) {
						setNoCount = false;
						continue;
					}
				}
				System.out.println("----------------------------------------");
				System.out.println("Parameter " + columnIndex++);
				System.out.println("Parameter Type : " + getParameterType(rs.getInt(5)));
				System.out.println("Name : " + rs.getString(4));
				System.out.println("Data Type : " + rs.getString(7));
				for (int i = 1; i < rsmd.getColumnCount(); i ++) {
					System.out.println("\t" + i + " - ColumnLabel : " + rsmd.getColumnLabel(i) + " : " + rs.getString(i));
					System.out.println("\t\tColumnClassName : " + rsmd.getColumnClassName(i));
					System.out.println("\t\tColumnName : " + rsmd.getColumnName(i));
					System.out.println("\t\tColumnType : " + rsmd.getColumnType(i));
					System.out.println("\t\tColumnTypeName : " + rsmd.getColumnTypeName(i));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}

				if (conn != null) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private static String getParameterType(final int metadataType) {
		switch (metadataType) {
		case DatabaseMetaData.procedureColumnUnknown :
            return "Unknown";
        case DatabaseMetaData.procedureColumnIn :
        	return "In";
        case DatabaseMetaData.procedureColumnInOut :
        	return "InOut";
        case DatabaseMetaData.procedureColumnOut :
        	return "Out";
        case DatabaseMetaData.procedureColumnReturn :
            return "Return";
        case DatabaseMetaData.procedureColumnResult :
            return "Result";
        default:
        	return "Unknown";
		}
	}

Oracle, SQLServer를 기준으로 작성하였습니다. SQLServer같은 경우에는 첫번째 파라미터로 setNoCount가 오므로 두번째 파라미터부터 해당 프로시저의 첫번째 파라미터의 정보를 가져올수가 있습니다.

블로그 이미지

행복그리고..

,

예를들어 스윙 어플리케이션에서 Progress를 뛰어주고 싶을경우에 사용하면 되겠습니다. 


Progress를 JDialog에서 생성하여 뛰워줘도 됩니다. 하지만 Progress 라는 것은 어떠한 작업을 할 경우


1. DataBase

2. File Load


... 등등이 되겠지요 가져오는 값들이 대용량일 경우에는 시간이 걸리기 때문에 이러한 상황에 대부분 Progress를 사용합니다. 

이러한 경우 타이틀바가 필요가 없게되고 작업이 다 끝났을 경우 Progress 는 사라지기 때문에 JDialog보다는 JWindow를 사용하여 구현하는게 좋겟지요 UI상에서도 보기 좋습니다.


Source Code

 

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;


class SwingApp extends JFrame implements ComponentListener {

	private static final long serialVersionUID = -1706061272770838668L;
	private static final int APP_WIDTH = 600;
	private static final int APP_HEIGHT = 400;
	
	private ProgressWindow theProgressWindow;
	
	public SwingApp(String title) {
		super(title);
		setSize(APP_WIDTH, APP_HEIGHT);
		
		Dimension theScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension theFrameSize = getSize();
		int x = (int) (theScreenSize.getWidth() - theFrameSize.getWidth()) / 2;
		int y = (int) (theScreenSize.getHeight() - theFrameSize.getHeight()) / 2;
		
		this.setLocation(x, y);
		this.setResizable(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.addComponentListener(this);
	}
	
	public void appearView() {
		this.setVisible(true);
		theProgressWindow = new ProgressWindow(this);
		Thread destoryThread = new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				theProgressWindow.dispose();
			}
		});
		destoryThread.start();
	}
	
	public void componentResized(ComponentEvent e) { }
	public void componentMoved(ComponentEvent e) {
		this.theProgressWindow.setLocationRelativeTo(e.getComponent());
	}
	public void componentShown(ComponentEvent e) { }
	public void componentHidden(ComponentEvent e) { }
}
 
class ProgressWindow extends JWindow {
	
	private static final long serialVersionUID = -6524167698066722942L;
	private static final String userDir = System.getProperty("user.dir");
	
	private static final int PROGRESS_WIDTH = 50;
	private static final int PROGRESS_HEIGHT = 50;
	
	private JLabel imageLabel = new JLabel();
	
	public ProgressWindow(JFrame theFrame) {
		super(theFrame);
		
		setSize(200, 150);
		this.setLayout(null);
		this.getRootPane().getContentPane().setBackground(Color.YELLOW);
		String filePath = userDir + File.separator + "ajax-loader.gif";
		ImageIcon imageIcon = new ImageIcon(filePath);
		
		Dimension theWindowSize = getSize();
		Dimension theLabelSize = new Dimension(PROGRESS_WIDTH, PROGRESS_HEIGHT);
		int x = (int) (theWindowSize.getWidth() - theLabelSize.getWidth()) / 2;
		int y = (int) (theWindowSize.getHeight() - theLabelSize.getHeight()) / 2;
		
		imageLabel.setIcon(imageIcon);
		imageLabel.setBounds(x, y, PROGRESS_WIDTH, PROGRESS_HEIGHT);
		this.add(imageLabel);
		setLocationRelativeTo(theFrame);
		this.setVisible(true);
	}
}

public class SwingTest {
	public static void run() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				SwingApp theApp = new SwingApp("Swing App");
				theApp.appearView();
			}
		});
	}
	
	public static void main(String[] args) {
		run();
	}
}

ajax-loader.gif File Download

URL :  http://www.ajaxload.info/ 

블로그 이미지

행복그리고..

,

http://msdn.microsoft.com/ko-kr/library/ms378422.aspx

블로그 이미지

행복그리고..

,

기본적으로 AWT/Swing Component에서 제공되는 기본 Component(JFileChooser,  JColorChooser)들의 

Text는 Default Locale로 표현된다. 영어로 Component를 사용하려면 아래와 같이 Default Locale을 변경하면 되겠다.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

class MySwing extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5148305495409346518L;

	private static final int APP_FRAME_WIDTH = 500;
	private static final int APP_FRAME_HEIGHT = 300;
	
	private static Dimension theScreenSize = createScreenSize();
	
	
	public MySwing(String title) {
		super(title);
		
		setSize(APP_FRAME_WIDTH, APP_FRAME_HEIGHT);
		
		Dimension theFrameSize = getSize();
		int x = (int) (theScreenSize.getWidth() - theFrameSize.getWidth()) / 2;
		int y = (int) (theScreenSize.getHeight() - theFrameSize.getHeight()) / 2;
		setLocation(x, y);
		
		JPanel panel = new JPanel();
		JButton fileChooserButton = new JButton("JFileChooser");
		fileChooserButton.addActionListener(this);
		JButton colorChooserButton = new JButton("JColorChooser");
		colorChooserButton.addActionListener(this);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER));
		panel.add(fileChooserButton);
		panel.add(colorChooserButton);
		add(panel);
		setVisible(true);
	}
	
	private static Dimension createScreenSize() {
		return Toolkit.getDefaultToolkit().getScreenSize();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();
		if (actionCommand.equals("JFileChooser")) {
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.showOpenDialog(this);
		} else if (actionCommand.equals("JColorChooser")) {
			JColorChooser.showDialog(this, "MySwing ColorChooser", Color.WHITE);
		}
	}
}

public class SwingTest {
	public static void main(String[] args) {
		try {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Nimbus".equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
		} catch (Exception e) {
		   e.printStackTrace();
		}
		JComponent.setDefaultLocale(Locale.ENGLISH);
		// Locale.setDefault(Locale.ENGLISH);
		new MySwing("MySwing App");
	}
}

블로그 이미지

행복그리고..

,
public static String[] getEnvironmentVariables() {
	String variables = System.getenv("Path");
	String[] arrVariable = variables.split(";");
	return arrVariable;
}


블로그 이미지

행복그리고..

,

Label에 대한 Font를 나눔고딕(NanumGothic)으로 설정.

UIManager.put("Label.font", new Font("NanumGothic", Font.PLAIN, 12));


다른 Component의 Font를 설정하고 싶을때에는 해당 Key 값을 변경하면 되겠다.

Swing Component의 Font Key 값은 아래와 같다.


Swing Platform FontUIResources.

Key : OptionPane.buttonFont

Key : List.font

Key : TableHeader.font

Key : Panel.font

Key : TextArea.font

Key : ToggleButton.font

Key : ComboBox.font

Key : ScrollPane.font

Key : Spinner.font

Key : RadioButtonMenuItem.font

Key : Slider.font

Key : EditorPane.font

Key : OptionPane.font

Key : ToolBar.font

Key : Tree.font

Key : CheckBoxMenuItem.font

Key : FileChooser.listFont

Key : Table.font

Key : MenuBar.font

Key : PopupMenu.font

Key : Label.font

Key : MenuItem.font

Key : MenuItem.acceleratorFont

Key : TextField.font

Key : TextPane.font

Key : CheckBox.font

Key : ProgressBar.font

Key : FormattedTextField.font

Key : CheckBoxMenuItem.acceleratorFont

Key : Menu.acceleratorFont

Key : ColorChooser.font

Key : Menu.font

Key : PasswordField.font

Key : InternalFrame.titleFont

Key : OptionPane.messageFont

Key : RadioButtonMenuItem.acceleratorFont

Key : Viewport.font

Key : TabbedPane.font

Key : RadioButton.font

Key : ToolTip.font

Key : Button.font



해당 Swing Component 전체 Font를 나눔고딕(NanumGothic)으로 설정.

setUIFont(new javax.swing.plaf.FontUIResource("NanumGothic", java.awt.Font.PLAIN, 12));
	
public static void setUIFont(javax.swing.plaf.FontUIResource f) {
	java.util.Enumeration keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if (value instanceof javax.swing.plaf.FontUIResource) {
        	 UIManager.put(key, f);
        }
    }
}



Swing Application을 실행하기 전에 적용하고 실행하면 되겠다.


Writing...



블로그 이미지

행복그리고..

,
     public static double getJavaVersion() {
		String javaVersion = System.getProperty("java.version");
		double version = Double.parseDouble(javaVersion.substring(0, 3));
		return version;
	}
	

	public static void setLookAndFeel(String lookAndFeelName) {
		if (getJavaVersion() > 1.5) {
			try {
				LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
			    for (LookAndFeelInfo info : plafs) {
			    	if (info.getName().equals(lookAndFeelName)) {
			    		UIManager.setLookAndFeel(info.getClassName());
			    	}
			    }
			} catch (Exception pe) {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				} catch (Exception ce) {
					ce.printStackTrace();
				}
			}
		}
		
	}
	
	public static void setLookAndFeel(String lookAndFeelName, JComponent component) {
		if (getJavaVersion() > 1.5) {
			try {
				LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
			    for (LookAndFeelInfo info : plafs) {
			    	if (info.getName().equals(lookAndFeelName)) {
			    		UIManager.setLookAndFeel(info.getClassName());
			    		SwingUtilities.updateComponentTreeUI(component);
			    		break;
			    	}
			    }
			} catch (Exception pe) {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				} catch (Exception ce) {
					ce.printStackTrace();
				}
			}
		}
	}

Swing Look And Feel JRE(Java Runtime Environment) 1.6 이상 지원이 된다.


기본적으로 JRE 1.6 이상 지원이 되는 Look And Feel에는

Metal, Nimbus, CDE/Motif, Windows, Windows Classic


Writing....

블로그 이미지

행복그리고..

,


import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class MyFrame extends JFrame implements ActionListener {
	⁄**
	 * 
	 *⁄
	private static final long serialVersionUID = -9026464211228409376L;
	
	private JButton setSizeButton = new JButton("setSize");
	private JButton setPreferredSizeButton = new JButton("setPreferredSize");
	private JButton repaintButton = new JButton("Repaint");
	public MyFrame(String title) {
		super(title);
		setSize(400, 200);
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = getSize();
		int x = (int) (screenSize.getWidth() - frameSize.getWidth()) ⁄ 2;
		int y = (int) (screenSize.getHeight() - frameSize.getHeight()) ⁄ 2;
		this.setLocation(x, y);
		
		JPanel panel = new JPanel();
		setSizeButton.addActionListener(this);
		setPreferredSizeButton.addActionListener(this);
		repaintButton.addActionListener(this);
		panel.add(setSizeButton);
		panel.add(setPreferredSizeButton);
		panel.add(repaintButton);
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				super.componentResized(e);
				System.out.println("componenet Resized(ComponentEvent e)");
			}
		});
		this.add(panel);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);

		System.out.println("paint(Graphics g)");
		System.out.println("Frame getSize() : " + getSize());
		System.out.println("Frame getPreferredSize() : " + getPreferredSize());
		System.out.println();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton actionButton = (JButton)e.getSource();
		Dimension thisSize = getSize();
		thisSize.setSize(thisSize.height, thisSize.width);
		if (actionButton == setSizeButton) {
			this.setSize(thisSize);
		} else if (actionButton == setPreferredSizeButton){
			this.setPreferredSize(thisSize);
		} else if (actionButton == repaintButton) {
			this.repaint();
		}
	}
}

코드를 실행해보면 결과와 같이 setSize 같은경우 해당 Frame을 다시 그리고, setPrefereedSize 같은 경우에는 그리지 않는다.


Writing...

'JAVA Platform > JAVA' 카테고리의 다른 글

[JAVA] - Swing Component Font 설정하기  (0) 2015.04.11
[JAVA] - Swing Look And Feel 적용하기  (0) 2015.04.01
[JAVA] - File 실행하기  (0) 2015.03.22
[JAVA] - JNDI Database Connection  (0) 2015.03.19
[JAVA] - DBMS JDBC Connection  (0) 2015.02.13
블로그 이미지

행복그리고..

,
import java.io.File;

public class FileProcessor {
	public static void run(File file) {
		while(true) {
			if (isFileExists(file)) {
				try {
					Process p = null;
					p = Runtime.getRuntime().exec(new String[] {"cmd.exe", "⁄c", file.getPath()}); 
					p.waitFor();
					break;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void run(String filePath) {
		run(new File(filePath));
	}
	
	public static boolean isFileExists(String filePath) {
		File file = new File(filePath);
		return file.exists();
	}
	
	public static boolean isFileExists(File file) {
		return file.exists();
	}
}


블로그 이미지

행복그리고..

,