Category Archives: Java

「Java入門」JavaでJDBCを利用してOracleデータベースに接続する方法

Javaコード:
public class OracleJdbcTest
{
String driverClass = “oracle.jdbc.driver.OracleDriver”;

Connection con;

public void init

(FileInputStream fs) throws ClassNotFoundException,

SQLException, FileNotFoundException, IOException
{
Properties props = new Properties();
props.load(fs);
String url = props.getProperty(“db.url”);
String userName = props.getProperty(“db.user”);
String password = props.getProperty(“db.password”);
Class.forName(driverClass);
con=DriverManager.getConnection(url, userName, password);
}

public void fetch() throws SQLException, IOException
{
PreparedStatement ps =

con.prepareStatement(“select SYSDATE from dual”);
ResultSet rs = ps.executeQuery();

while
(rs.next())
{
}
rs.close();
ps.close();
}
public
static void main(String[] args)
{
OracleJdbcTest test = new OracleJdbcTest();
test.init();
test.fetch
();
}
}

public class OracleJdbcTest
{
String driverClass = “oracle.jdbc.driver.OracleDriver”;
Connection con;
public void init(FileInputStream fs) throws ClassNotFoundException,
SQLException, FileNotFoundException, IOException
{
Properties props = new Properties();
props.load(fs);
String url = props.getProperty(“db.url”);
String userName = props.getProperty(“db.user”);
String password = props.getProperty(“db.password”);
Class.forName(driverClass);
con=DriverManager.getConnection(url, userName, password);
}
public void fetch() throws SQLException, IOException
{
PreparedStatement ps = con.prepareStatement(“select SYSDATE from dual”);
ResultSet rs = ps.executeQuery();
while (rs.next())
{

}
rs.close();
ps.close

();
}
public static void main(String[] args)
{
OracleJdbcTest test = new OracleJdbcTest

();
test.init();
test.fetch();
}
}

「Java入門」Javaで文字列stringから日付型dateに変換する方法

PHPコード:
java.util.Date =
java.text.DateFormat.getDateInstance().parse(date String);
java.util.Date = java.text.DateFormat.getDateInstance().parse(date
String);or
SimpleDateFormat format = new SimpleDateFormat( “dd.MM.yyyy”
);
Date date = format.parse( myString );
SimpleDateFormat format = new SimpleDateFormat( “dd.MM.yyyy” );
Date date
= format.parse( myString );

「Java入門」Java現在呼び出すメソッド名を取得

javaコード:
String methodName =
Thread.currentThread().getStackTrace()[1].getMethodName();
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName
();

Javaファイルにテキストを追加するサンプル

javaコード:

BufferedWriter out = null;
try
{
out = new BufferedWriter(new FileWriter(”

filename”, true));
out.write(”

aString”);
} catch (IOException e) {
// エラー処理コード
} finally

{
if (out != null) {
out.close();
}
}
BufferedWriter out = null;
try {
out = new BufferedWriter(new

FileWriter(”filename”, true));
out.write(”aString”);
} catch

(IOException e) {
// エラー処理コード
} finally {
if (out

!= null) {
out.close();
}
}

「Java入門」int数値型から文字列、文字列からint数値型へのデータ変換

javaコード:

String a = String.valueOf(2);
//intから文字列
int i = Integer.parseInt(a); //numeric
string to an int
String a = String.valueOf(2);
//integer to numeric string
int i = Integer.parseInt(a); //numeric string to an
int

 

「Java入門」JavaBeanの基本操作

1.Javabeanは「WEB-INF/classes」の中に保存
毎回classesファイルまたはweb.xmlを修正したらサーバの再起動が必要
自動導入モード:Server.xmlに次の内容を追加
<Context path=”…” docBase=”…” reloadable=”true”/> (リアルサーバーを監視するのでパフォーマンスを低下)

2.<jsp:useBean>説明:
<jsp:useBean id=”beanName” scope=”page” class=”fully_classname””/>
3.<jsp:setProperty>説明:
(1)<jsp:setProperty name=”beanName” property=”*”/>

(2)<jsp:setProperty name=”beanName” property=”name1″/>

(3)<jsp:setProperty name=”beanName” property=”name1″ value=”value1″/>

(4)<jsp:setProperty name=”beanName” property=”name1″ param=”param1″/>

4.<jsp:getProperty>説明:
<jsp:getProperty name=”c1″ propety=”name1″/>

5.JavaBeanを削除:
pageConext.removeAttribute(“JavaBean名前”);

「unity開発」JavaでLitJsonの使い方

1.LitJson、DLLファイルをダウンロード

2.クラスを作成
public class JsonData
{
public string result { get; set; }
public List<GameData> info { get; set; }
}

public GameData()
{ }
#region Model
private int _id;
private string _dept;
private string _sid;
private string _oldsid;
private string _userid;
private string _roleid;
private string _rolename;
private string _rolelevel;
private string _gold;
private string _money;
private string _inittime;
private string _lastlogin;
private string _querytime;
/// <summary>
///
/// </summary>
public int ID
{
set { _id = value; }
get { return _id; }
}
/// <summary>
/// プラットフォーム番号
/// </summary>
public string dept
{
set { _dept = value; }
get { return _dept; }
}
/// <summary>
/// サーバー番号
/// </summary>
public string sid
{
set { _sid = value; }
get { return _sid; }
}
/// <summary>
/// 元サーバー番号
/// </summary>
public string oldsid
{
set { _oldsid = value; }
get { return _oldsid; }
}
/// <summary>
/// ユーザーのプラットフォームID
/// </summary>
public string userid
{
set { _userid = value; }
get { return _userid; }
}
/// <summary>
///
/// </summary>
public string roleid
{
set { _roleid = value; }
get { return _roleid; }
}
/// <summary>
/// キャラクター名
/// </summary>
public string rolename
{
set { _rolename = value; }
get { return _rolename; }
}
/// <summary>
/// キャラクターレベル
/// </summary>
public string rolelevel
{
set { _rolelevel = value; }
get { return _rolelevel; }
}
/// <summary>
/// 金貨
/// </summary>
public string gold
{
set { _gold = value; }
get { return _gold; }
}
/// <summary>
/// 銀貨
/// </summary>
public string money
{
set { _money = value; }
get { return _money; }
}
/// <summary>
/// キャラクター作成時間
/// </summary>
public string inittime
{
set { _inittime = value; }
get { return _inittime; }
}
/// <summary>
/// 最終ログイン時間
/// </summary>
public string lastlogin
{
set { _lastlogin = value; }
get { return _lastlogin; }
}
/// <summary>
/// クエリ時間
/// </summary>
public string querytime
{
set { _querytime = value; }
get { return _querytime; }
}
#endregion Model

3.データを読む
string json_str = @”{“”result””:””1″”,””info””:[{“”sid””:””2″”,””rolename””:””ふた””,””roleid””:””900001″”,””lastlogin””:””2014-01-08 10:54:52″”,””inittime””:””2013-11-15 12:01:05.0″”,””money””:””867″”,””rolelevel””:””55″”,””userid””:””athena””,””oldsid””:””1″”,””gold””:””9992837″”,””dept””:””67″”,””querytime””:””2014-01-08 11:56:38″”},

{“”sid””:””2″”,””rolename””:””モンスター””,””roleid””:””5700001″”,””lastlogin””:””2013-11-16 18:38:54″”,””inittime””:””2013-11-16 18:25:45.0″”,””money””:””0″”,””rolelevel””:””27″”,””userid””:””athena””,””oldsid””:””1″”,””gold””:””0″”,””dept””:””67″”,””querytime””:””2014-01-08 11:56:38″”},

{“”sid””:””2″”,””rolename””:””ねこ””,””roleid””:””2800001″”,””lastlogin””:””2013-11-15 15:00:07″”,””inittime””:””2013-11-15 14:59:25.0″”,””money””:””0″”,””rolelevel””:””1″”,””userid””:””athena””,””oldsid””:””1″”,””gold””:””0″”,””dept””:””67″”,””querytime””:””2014-01-08 11:56:38″”}]}”;

GameWindowsService.Common.JsonData jsonData = LitJson.JsonMapper.ToObject<GameWindowsService.Common.JsonData>(json_str);

List<GameData> gameDataList = jsonData.info;
foreach (var gameData in gameDataList)
{
Common.Common.WriteFile(gameData.Add().ToString());
}

javaで複数のファイルを圧縮するサンプル

javaコード:
import java.io.*;
import java.util.zip.*;

public class Startnews24Zip {

public static void main(String[] args) {
// TODO Auto-generated method stub

try {
FileOutputStream fos = new FileOutputStream(“D:/startnews24_File/todo.zip”);
CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(
cos));
zos.setComment(“これは圧縮サンプル.”);
for (int i = 0; i < args.length; i++) {
System.out.println(“———-“);
//brにファイルを書き込む、それからbr一枚ずつから圧縮されたファイルを読み込む
BufferedReader br = new BufferedReader(new FileReader(args[i]));
zos.putNextEntry(new ZipEntry(args[i]));
int c;
while ((c = br.read()) != -1)
zos.write(c);
br.close();
}
zos.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

「Java開発」Word文書をHTMLに変換するプログラム

サンプルコード
@Test
public void docxToHtml()
throws Exception
{
String sourceFileName = “D:\\test\\cft.docx”;
String targetFileName = “E:\\changfa\\test.html”;
String imagePathStr = “E:\\test\\img\\”;
OutputStreamWriter outputStreamWriter = null;
try
{
XWPFDocument document = new XWPFDocument(new FileInputStream(sourceFileName));
XHTMLOptions options = XHTMLOptions.create();
// ディレクトリに画像を保存
options.setExtractor(new FileImageExtractor(new File(imagePathStr)));
// htmlの画像のパス
options.URIResolver(new BasicURIResolver(“img”));
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(targetFileName),
“utf-8”);
XHTMLConverter xhtmlConverter = (XHTMLConverter)XHTMLConverter.getInstance();
xhtmlConverter.convert(document, outputStreamWriter, options);
}
finally
{
if (outputStreamWriter != null)
{
outputStreamWriter.close();
}
}
}

「Java入門」java.time.LocalTimeのサンプル

javaコード
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;

public class LocalDateTimeDemo {

public static void main(String[] args) {
// current
LocalDateTime localDateTime1 = LocalDateTime.now();
System.out.println(localDateTime1);

LocalDateTime localDateTime2 = LocalDateTime.of(2017, Month.JUNE, 21, 16, 12, 34);
System.out.println(localDateTime2);
// from a local date and a local time
LocalDate localDate1 = LocalDate.of(2017, 5, 10);
LocalTime localTime= LocalTime.of(16, 18, 41);
LocalDateTime localDateTime3 = LocalDateTime.of(localDate1, localTime);
System.out.println(localDateTime3);

}

}
結果
2017-12-21T01:50:30.022
2017-06-21T16:12:34
2017-05-10T16:18:41