「Java」org.apache.derby.jdbc.EmbeddedDriverでDBに接続するサンプルコード

Javaコード
package com.cftj;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class JavaDbDemo {
static Connection conn;

public static void main(String[] args) {
String driver = “org.apache.derby.jdbc.EmbeddedDriver”;
String connectionURL = “jdbc:derby:cftDatabase;create=true”;
String createString = “CREATE TABLE StudentTbl (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)”;
try {
Class.forName(driver);
} catch (java.lang.ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(connectionURL);
Statement stmt = conn.createStatement();
stmt.executeUpdate(createString);

PreparedStatement insertSQL = conn.prepareStatement(“insert into StudentTbl values (?,?)”);

insertSQL.setString(1, args[0]);
insertSQL.setString(2, args[1]);

insertSQL.executeUpdate();

Statement stmt2 = conn.createStatement();
ResultSet rs = stmt2.executeQuery(“select * from StudentTbl”);
int num = 0;
while (rs.next()) {
System.out.println(++num + “: Name: ” + rs.getString(1) + “\n Address” + rs.getString(2));
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}

「python」zipファイルを読むサンプルコード

pythonコード
#!/usr/bin/env python
import zipfile
z = zipfile.ZipFile(“test.zip”,”r”)
for filename in z.namelist():
print ‘File:’,filename,
bytes = z.read(filename)
print ‘has’,len(bytes),’bytes’

「Java」Vectorから文字列配列に変換するコード

Javaコード
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

@SuppressWarnings(“unused”)
public class Vector2ArrayDemo {

public static void main(String[] args) {
// Creating a Vector of String elements
Vector<String> vector = new Vector<String>();

// Add elements to Vector
vector.add(“Test Data one”);
vector.add(“Test Data two”);
vector.add(“Test Data three”);
vector.add(“Test Data four”);
vector.add(“Test Data five”);
vector.add(“Test Data six”);

//Converting Vector to String Array
String[] array = vector.toArray(new String[vector.size()]);

//Displaying Array Elements
System.out.println(“String Array Elements :”);
for(int i=0; i < array.length ; i++){
System.out.println(array[i]);
}
}

}
実行結果
String Array Elements :
Test Data one
Test Data two
Test Data three
Test Data four
Test Data five
Test Data six

「Java」コレクションLinkedListの要素を削除するコード

Javaコード
import java.util.LinkedList;
public class RemoveElementFromLinkedList {

public static void main(String[] args) {

// Create a LinkedList
LinkedList<String> lnkPa = new LinkedList<String>();

// Add elements to LinkedList
lnkPa.add(“Jquery”);
lnkPa.add(“Objective-C”);
lnkPa.add(“C言語”);
lnkPa.add(“PHP”);
lnkPa.add(“Java”);

// Displaying Elements before replace
System.out.println(“LinkedList Elements:”);
for(String str: lnkPa){
System.out.println(str);
}

// Removing 3rd element
Object e1 = lnkPa.remove(2);
System.out.println(“\nElement “+ e1+ ” removed from the list\n”);

// LinkedList elements after remove
System.out.println(“After removal:”);
for(String str2: lnkPa){
System.out.println(str2);
}
}

}
実行結果
LinkedList Elements:
Jquery
Objective-C
C言語
PHP
Java

Element C言語 removed from the list

After removal:
Jquery
Objective-C
PHP
Java

「html5」input type=”number”のmaxlengthを設定するメモ

1.max属性
<input name=”emp_seq” type=”number” max=”999″ />

2.maxとmin
<input name=”emp_seq” type=”number” min=”1″ max=”999″ />

「java」LinkedHashMapの使い方

Javaコード

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class DemoLinkedHashMap {

public static void main(String[] args) {
// HashMap Declaration
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>();

// Adding elements to LinkedHashMap
lhmap.put(11, “データ AA”);
lhmap.put(21, “データ BB”);
lhmap.put(1, “データ CC”);
lhmap.put(2, “データ DD”);
lhmap.put(50, “データ EE”);

// Generating a Set of entries
Set<Entry<Integer, String>> set = lhmap.entrySet();

// Displaying elements of LinkedHashMap
Iterator<Entry<Integer, String>> iterator = set.iterator();
while (iterator.hasNext()) {
@SuppressWarnings(“rawtypes”)
Map.Entry me = (Map.Entry) iterator.next();
System.out.print(“Key is: ” + me.getKey() + ”  Value: ” + me.getValue() + “\n”);
}
}

}

実行結果

Key is: 11  Value: データ AA
Key is: 21  Value: データ BB
Key is: 1  Value: データ CC
Key is: 2  Value: データ DD
Key is: 50  Value: データ EE

「JavaScript」indexOf()とlastIndexOf()の使い方

1.indexOf()
<script type=”text/javascript”>
var str=”Welcome Tom!”
document.write(str.indexOf(“Welcome”) + “<br />”)
document.write(str.indexOf(“Tom”) + “<br />”)
document.write(str.indexOf(“tom”))

</script>
結果
0
8
-1

2.lastIndexOf()
<script type=”text/javascript”>
var str=”Welcome Tom!”
document.write(str.lastIndexOf(“Welcome”) + “<br />”)
document.write(str.lastIndexOf(“Tom”) + “<br />”)
document.write(str.lastIndexOf(“tom”))
結果
0
8
-1

「JavaScript」対応ブラウザ(ie、chrome、firefox)を判定するサンプルコード

サンプルコード
var client = function(){
var engine = {
ie:0,
gecko:0,
webkit:0,
khtml:0,
opera:0,
ver:null
};
var browser = {
ie: 0,
firefox: 0,
safari: 0,
konq: 0,
opera: 0,
chrome: 0,
ver:null
};
var ua = navigator.userAgent;
if(window.opera){
engine.ver = browser.ver = window.opera.version();
engine.opera = browser.opera = parseFloat(engine.ver);
} else if(/AppleWebKit/(S+)/.test(ua)){
engine.ver = RegExp[“$1”];
engine.webkit = parseFloat(engine.ver);
// Chrome/Safari判定
if (/Chrome/(S+)/.test(ua)){
browser.ver = RegExp[“$1”];
browser.chrome = parseFloat(browser.ver);
} else if (/Version/(S+)/.test(ua)){
browser.ver = RegExp[“$1”];
browser.safari = parseFloat(browser.ver);
} else {
//バージョン確認
var safariVersion = 1;
if (engine.webkit < 100){
safariVersion = 1;
} else if (engine.webkit < 312){
safariVersion = 1.2;
} else if (engine.webkit < 412){
safariVersion = 1.3;
} else {
safariVersion = 2;
}
browser.safari = browser.ver = safariVersion;
}
}else if (/KHTML/(S+)/.test(ua) || /Konqueror/([^;]+)/.test(ua)){
engine.ver = browser.ver = RegExp[“$1”];
engine.khtml = browser.konq = parseFloat(engine.ver);
}else if (/rv:([^)]+)) Gecko/d{8}/.test(ua)){
engine.ver = RegExp[“$1”];
engine.gecko = parseFloat(engine.ver);
// Firefox判定
if (/Firefox/(S+)/.test(ua)){
browser.ver = RegExp[“$1”];
browser.firefox = parseFloat(browser.ver);
}
}else if (/MSIE ([^;]+)/.test(ua)){
engine.ver = browser.ver = RegExp[“$1”];
engine.ie = browser.ie = parseFloat(engine.ver);
}
return {
engine:engine,
browser: browser
};
}();

if (client.engine.webkit) { //if it’s WebKit
if (client.browser.chrome){
//chrome some code
}
} else if (client.engine.gecko){
if (client.browser.firefox){
// Firefox some code
} else {
// Gecko some code
}
}

「Java」Collections.synchronizedList() のサンプルコード

javaコード
public class SynchronizedListDemo {

public static void main(String[] args) {
List<String> cft =
Collections.synchronizedList(new ArrayList<String>());

//Adding elements to synchronized ArrayList
cft.add(“Data AA”);
cft.add(“Data BB”);
cft.add(“Data CC”);
synchronized(cft) {
Iterator<String> iterator = cft.iterator();
while (iterator.hasNext())
System.out.println(iterator.next());
}
}
}

実行結果
Data AA
Data BB
Data CC

「PHP」デバッグに便利なメソッド–debug_backtrace()

phpコード
function loginfo($format) {
$args = func_get_args();
array_shift($args);
$d = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1)[0];
$info = vsprintf($format, $args);
$data = sprintf(“%s %s,%d: %s\n”, date(“Ymd His”), $d[“file”], $d[“line”], $info);
file_put_contents(__DIR__.”/newslog.txt”, $data, FILE_APPEND);
}