Google
 
Web itpro-blogger.blogspot.com
木曜日, 10月 19, 2006

[SQL関連]文字列操作

▼質問
テーブルでDATE型でない年(YEAR)月(MONTH)日(DAY)が別々で存在するフィールドがあるんだ。
これを連結してDATE型に変換したいんだ。
SQL文で文字列を連結するにはどうしたらいいの?

▼回答
文字列の結合には2種類があるようだ。

String1、String2には、文字列型のフィールドを指定するかシングルコーテイション''で文字列を
指定する。

①普通の連結:引数を単純に連結する。
CONCAT(String1, String2, ...)
②特殊な連結:第1引数のseparator(セパレータ:区切り文字)で以降の引数を連結する。
CONCAT_WS(separator, String1, String2, ...)

②CONCAT_WSは、oracleは使えないみたい。MySqlだけなの?

オラクルでこうするとできるようだ。
to_date(YEAR || '.' || MONTH || '.'|| DAY || ' 00:00:00', 'yyyy.mm.dd hh24:mi:ss')

■参考サイト
【SMART! ウェブ講座】~文字列関数
http://www.rfs.jp/sb/sql/03/02.html


火曜日, 10月 17, 2006

[Java]メモ3

▼質問
ButtonGroupで作成したボタングループを、プログラムから選択させたい時はどうやるんだい?

▼回答
2つのラジオボタンを選択されていない状態で生成してから、ラジオボタン2を選択された状態にする。

ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton radioButton1 = new JRadioButton( "ラジオボタン1", false );
JRadioButton radioButton1 = new JRadioButton( "ラジオボタン2", false );
buttonGroup.add( radioButton1 );
buttonGroup.add( radioButton2 );
buttonGroup.setSelected( radioButton2.getModel(), true );

▼質問
選択されたラジオボタンをすべて解除するにはどうしたらいいの?

▼回答
ButtonGroupを作成し直す?!しかないのかな・・・?
removeしてaddする。。。。

removeしてaddするだけでは駄目のようだ。
setSelectedメソッドでfalseをセットしてやらないと解除できないみたい。

ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton[] radioButton = new JRadioButton[2];

~略~

for ( int i = 0 ; i < radioButton.length ; i++ ) {
if ( radioButton[i].isSelected() ) {
buttonGroup.remove( radioButton[i] );
radioButton[i].setSelected( false );
buttonGroup.add( radioButton[i] );
}
}

▼質問
テキストフィールドの入力文字数をバイト長で制限したいんだけどどうやればいいの?

▼回答
public class LimitedJTextField extends javax.swing.JTextField {

public LimitedJTextField( int limit ) {
this.setDocument( new ByteLengthLimitedDocument( limit ) );
}

static class ByteLengthLimitedDocument extends javax.swing.text.PlainDocument {

private int limit;

public ByteLengthLimitedDocument( int limit ) {
this.limit = limit;
}

public void insertString( int offs, String str, AttributeSet a ) throws BadLocationException {

if ( str == null ) return;

String text = this.getText( 0, getLength() );
byte[] bytes = text.getBytes();

if ( bytes.length >= limit ) return;

super.insertString( offs, str, a );

}
}

}

■参考サイト
http://java-house.jp/ml/archive/j-h-b/020938.html

▼質問
同じような処理でクラスを分けなくて済む方法はないのかな?

クラスのタイプを判断して処理を分けたいんだ。

▼回答
getClassメソッドを使えばできるよ!

cとdはparentを継承して作成されている。
Object parent;
Object c;
Object d;
public void setParent(Object obj) {
parent = obj;
if ( parent.getClass().equals( c ) ) {
c = (c)obj;
}
else if ( parent.getClass().equals( d ) ) {
d = (d)obj;
}
}

■参考サイト
http://www.asahi-net.or.jp/~dp8t-asm/java/tips/HowToGetClass.html


日曜日, 10月 15, 2006

[Java]フォーマット

▼質問
数値をフォーマットしたい時はどうすんだい?

▼回答
DecimalFormatクラスを使用すればできるよ。使用方法は次のようになるよ。

DecimalFormat decimalFormat = new DecimalFormat( "###.0" );
String str = decimalFormat.format( 1234 );

▼質問
文字列をフォーマットしたい時はどうすんだい?

▼回答


▼質問
テキストフィールドの入力値をフィルタしたい時はどうすればいいの?

▼回答

JFormattedTextFieldクラスを使うとこうなるけど・・・。
文字数でしか入力制限ができない。。。

private MaskFormatter setMaskFormatter( int length ) {
MaskFormatter mf = null;
try {
String format = "";
for ( int i = 0 ; i < length-1 ; i++ ) format = format + "*";
mf = new MaskFormatter( format );
mf.setPlaceholderCharacter('_');
} catch (ParseException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
return mf;
}
MaskFormatter mf = null;
try{
mf = new MaskFormatter("〒###-####");
mf.setPlaceholderCharacter('_');
}catch(ParseException pe){
}
JFormattedTextField ftf3 = new JFormattedTextField(mf);
ftf3.setColumns(10);

private MaskFormatter setMaskFormatter() {
MaskFormatter mf = null;
try {
mf = new MaskFormatter("###-##-####");
mf.setPlaceholderCharacter('_');
} catch (ParseException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
return mf;
}


小数点を含む数値の入力のフォーマットはこうするとできたぞっ!

JFormattedTextField ftf = new JFormattedTextField();
DecimalFormat df = new DecimalFormat( "0.00" );

// 整数部分の最小桁数を設定する.
df.setMinimumIntegerDigits( 1 );
// 整数部分の最大桁数を設定する.
df.setMaximumIntegerDigits( 1 );
// 小数部分の最小桁数を設定する.
df.setMinimumFractionDigits( 2 );
// 小数部分の最大桁数を設定する.
df.setMaximumFractionDigits( 2 );
NumberFormatter nf = new NumberFormatter( df );
// 編集時には必要に応じてリテラル文字がスキップする.
nf.setAllowsInvalid( false );
nf.install( ftf );

■参考サイト
【Java:Swing:JFormattedTextField】
http://www.javadrive.jp/tutorial/jformattedtextfield/index.html

▼質問
入力ベリファイア(InputVerifier)って何?

▼回答
フォーカスが移動した時に実行され、trueになるまでフォーカスが移動できないみたい!?

JFormattedTextField tf = new JFormattedTextField();
tf.setInputVerifier( new InputVerifier() {
public boolean verify( javax.swing.JComponent input ) {
System.out.println( "InputVerifier" );
boolean returnValue = false;
JFormattedTextField tf = (JFormattedTextField)input;
try {
Integer.parseInt( tf.getText() );
returnValue = true;
}
catch ( NumberFormatException e ) {
tf.setText( "" );
returnValue = false;
}
return returnValue;
}
});

▼質問
JFormattedTextFieldでNumberFormatterをinstallしたんだけど・・・。
入力したものをクリアするためにsetText( "" )したんだけどうまくいかないんだ!
どうしたらうまくいくのか教えてくれ!!

▼回答
こうやるとできるよ(^^)v
JFormattedTextField ftf = new JFormattedTextField();
PlainDocument doc = new PlainDocument();
doc.insertString( 0, "", ftfgetDocument().getDefaultRootElement().getAttributes() );
ftf.setDocument( doc );

悲しい・・・お知らせです。

この方法では、設定したフォーマットが削除されてしまうことがわかりました。(;_;)

回避方法は、もう一度フォーマットし直す方法しか見つけれなかった。

とほほ。。。


日曜日, 10月 08, 2006

[Java]文字化け

▼質問
インプットフィールドの全角文字が文字化けする。

入力文字:12345678901234567890
文字化け後:?P?Q?R?S?T?U?V?W?X?O?P?Q?R?S?T?U?V?W?X?O

▼回答
String strText = request.getParameter( "TEXT" );
String strToText = new String ( strText.getBytes( "ISO8859_1" ), "JISAutoDetect" );


■参考サイト
【HTMLエンコード処理】
http://ash.jp/java/htmlencode.htm
【JAVA】
http://hisa-net.ddo.jp/php/java/encode.php


This page is powered by Blogger. Isn't yours?