Monthly Archives: 8月 2016

C#入門–正規表現式で画像をフィルタ操作する

参考コード
///<summary>
///<summary>
public static string replaceImgUrl(string html)
{
if (html == null)
return “”;

System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@”\<img[^\>]+\>”, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex.Replace(html, “”);
return html;
}

asp.net入門–FileUploadを使って画像ファイルをアップロードする

asp.netコード:
using System.IO;
protected void btnPic_Click(object sender, EventArgs e)
{
Boolean fileOk = false;
String path = Server.MapPath(“~/”);
if (FileUpload1.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
//ファイル拡張子
String[] allowedExtensions ={ “.gif”, “.png”, “.jpeg”, “.jpg”, “.bmp” };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOk = true;
}
}
}
if (fileOk)
{
try
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
Response.Write(“<script>alert(‘ファイルアップロード成功’);</script>”);
}
catch
{
Response.Write(“<script>alert(‘ファイルアップロード失敗!’);</script>”);
}
}
else
{
Response.Write(“<script>alert(‘gif,png,jpeg,jpg,bmpファイルアップロード可能!’);</script>”);
}
}

asp.net開発入門–印刷機能を実現する方法

asp.netコード
<OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0></OBJECT>
<input type=button value=印刷 onclick=document.all.WebBrowser.ExecWB(6,1)>
<input type=button value=直接印刷 onclick=document.all.WebBrowser.ExecWB(6,6)>
<input type=button value=ページ設定 onclick=document.all.WebBrowser.ExecWB(8,1)>
<input type=button value=”印刷開始” onclick=” return myprint(this);”>
<input type=button value=印刷プレビュー onclick=document.all.WebBrowser.ExecWB(7,1)>

【Java】Serializableの基本使い方(オブジェクトをファイルに保存)

Javaコード:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class CtjSeria implements Serializable {

private static final long serialVersionUID = -3450064362986273896L;

private int width;
private int height;

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public static void main(String[] args) {
CtjSeria tmdSer = new CtjSeria();
tmdSer.setHeight(30);
tmdSer.setWidth(100);
try {
FileOutputStream fs = new FileOutputStream(“D:\\foo.ser”);
ObjectOutputStream ots = new ObjectOutputStream(fs);
ots.writeObject(tmdSer);
ots.close();

FileInputStream fit = new FileInputStream(“D:\\foo.ser”);
ObjectInputStream ois = new ObjectInputStream(fit);
CtjSeria cta = (CtjSeria) ois.readObject();
ois.close();
System.out.println(cta.height + “,” + cta.width);
} catch (Exception e) {
e.printStackTrace();
}

}

}

JavaプログラムでLinkedListとArrayListの要素追加比較サンプル

Javaコード

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ListTestSpeed {

static final int N=10000;
static long timeList(List list){
long start=System.currentTimeMillis();
Object o = new Object();
for(int i=0;i<N;i++)
list.add(0, o);
return System.currentTimeMillis()-start;
}
public static void main(String[] args) {

System.out.println(“ArrayList 速度:”+timeList(new ArrayList()));
System.out.println(“LinkedList 速度:”+timeList(new LinkedList()));
}
}

実行結果
ArrayList 速度:31
LinkedList 速度:16

Java開発でEnumerationインタフェースの使い方

Javaコード:
package com.startnews24.study;
import java.util.Enumeration;

@SuppressWarnings(“rawtypes”)
class TestEnumeration implements Enumeration
{
int count;
int length;
Object[] dataArray;
TestEnumeration(int count, int length, Object[] dataArray) {
this.count = count;
this.length = length;
this.dataArray = dataArray;
}

public boolean hasMoreElements() {
return (count < length);
}

public Object nextElement() {
return dataArray[count++];
}
}

class TestDataStruct
{
String[] obj;

TestDataStruct() {
obj = new String[4];
obj[0] = “品川-11”;
obj[1] = “品川-12”;
obj[2] = “品川-13”;
obj[3] = “品川-14”;
}

@SuppressWarnings(“rawtypes”)
Enumeration getEnum() {
return new TestEnumeration(0,obj.length,obj);
}
}

public class EnumerationDemo {

public static void main(String[] args) {
TestDataStruct cntruct = new TestDataStruct();
@SuppressWarnings(“rawtypes”)
Enumeration cnEnumeration =cntruct.getEnum();
while (cnEnumeration.hasMoreElements())
System.out.println(cnEnumeration.nextElement());

}

}

実行結果:
品川-11
品川-12
品川-13
品川-14

JavaでStringBuilderクラスのinsert()とappend()を利用する方法

Javaコード:
class Employee {
int age =24;

public String toString() {
return “社員年齢:” + age;
}
}
public class StrUnsertAppend {

public static void main(String[] args) {
StringBuffer strBf= new StringBuffer(“startnews24”);
System.out.println(“insert()を呼び出し前:\n”+strBf);
strBf.insert(1, “study skill”);
System.out.println(“insert()を呼び出し後:\n”+strBf);
System.out.println(“——————————————-“);
Employee stu = new Employee();
System.out.println(“toString()を呼び出す:\n”+stu);
strBf.append(stu);
System.out.println(“出力結果:\n”+strBf);
}

}
insert()を呼び出し前:
startnews24
insert()を呼び出し後:
sstudy skilltartnews24
——————————————-
toString()を呼び出す:
社員年齢:24
出力結果:
sstudy skilltartnews24社員年齢:24

OracleでDUAL表の使い方

1.現在ユーザを取得
select user from dual
—————————————– ——– —————————-
USER
STARTNEWS24USER88

2.システム時刻を取得
select to_char(SYSDATE,’yyyy-mm-dd hh24:mi:ss’) from dual
—————————————– ——– —————————-
TO_CHAR(SYSDATE,’YYYY-MM-DDHH24:MI:SS’)
2016-08-25 10:31:14

select sysdate from dual
—————————————– ——– —————————-
SYSDATE
16-08-25

3.ロケール(locale)を取得
select SYS_CONTEXT(‘USERENV’,’language’) from dual;
—————————————– ——– —————————-
SYS_CONTEXT(‘USERENV’,’LANGUAGE’)
JAPANESE_JAPAN.JA16SJIS

4.ランダム数字を取得
select SYS.DBMS_RANDOM.RANDOM from dual
—————————————– ——– —————————-
RANDOM
167899999

5.電卓として利用
select 2*3 from dual
—————————————– ——– —————————-
2*3
6.dualのオブジェクト名とオブジェクトタイプを取得
select owner, object_name , object_type from dba_objects where object_name like ‘%DUAL%’; OWNER OBJECT_NAME OBJECT_TYPE
—————————————– ——– —————————-
OWNER OBJECT_NAME OBJECT_TYPE
SYS DUAL TABLE
PUBLIC DUAL SYNONYM

select dummy from dual
—————————————– ——– —————————-
DUMMY
X

7.DUALの英語ドキュメント
There is internalized code that makes this happen. Code checks that ensure that a table scan of SYS.DUAL only returns one row. Svrmgrl behaviour is incorrect but this is now an obsolete product. The base issue you should always remember and keep is: DUAL table should always have 1 ROW. Dual is a normal table with one dummy column of varchar2(1). This is basically used from several applications as a pseudo table for getting results from a select statement that use functions like sysdate or other prebuilt or application functions. If DUAL has no rows at all some applications (that use DUAL) may fail with NO_DATA_FOUND exception. If DUAL has more than 1 row then applications (that use DUAL) may fail with TOO_MANY_ROWS exception. So DUAL should ALWAYS have 1 and only 1 row.

asp.netでGridViewのWidthを設定する方法

参考コード
/// <summary>
/// </summary>
/// <param name=”srcView”></param>
/// <param name=”width”></param>
public void SetGridViewColumnWidth(GridView srcView)
{
double cellWidth = srcView.Width.Value;
for (int i = 0; i < srcView.Rows.Count; i++)
{
for(int j = 0;j < srcView.Rows[i].Cells.Count;j++)
{
srcView.Rows[i].Cells[j].Width = Convert.ToInt16(cellWidth);
}
}
}

asp.netでサーバー(クライアント)のIPアドレスを取得する方法

1.サーバのIPアドレスを取得
using System.Net;

string strIpinfo,strMacinfo;
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
if ( addressList.Length>1)
{
strIpinfo = addressList[0].ToString();
strMacinfo = addressList[1].ToString();
}
else
{
strIpinfo = addressList[0].ToString();
strMacinfo = “接続できません”;
}
2.クライアントのIPアドレスを取得
string strclientIp = Request.ServerVariables[“REMOTE_ADDR”].ToString();