Java活用生活

ログイン

オンライン状況

オンラインユーザー2人
ログインユーザー0人
登録ユーザー2人

カウンタ

COUNTER370929

日誌

MyDoc(備忘録)
123456
2025/05/18

実行したSQLを取得する

固定リンク | by:aramaki
実行したSQLを取得する
------------------------------------
抜粋)Web開発における知見共有系ページ
try (SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession()) {
            Configuration configuration = session.getConfiguration();
            MappedStatement ms = configuration.getMappedStatement("testUserMapper.selectTestUser");
            String sql = ms.getBoundSql(null).getSql();
            System.out.println(sql);
        }
11:17 | 投票する | 投票数(0) | コメント(0)
2025/05/15

配列ソートサンプル

固定リンク | by:aramaki
配列ソートサンプル
-------------------------------
/**
 *
 * CSVファイルを読み込んで、そのデータ(年齢)をもとにソートしてCSVファイルに書き出す
 * @author aramaki
 *
 */
public class CsvReadWriteSortCsvTest {

private static final String INPUTCSV="C:/data.csv";

private static final String OUTPUTCSV="C:/tmp/outputsortdata.csv";





public static void main(String[] args) {
sortCsvProcessor(INPUTCSV, OUTPUTCSV);

}

public static void sortCsvProcessor(String inFilePath,String outFilePath){

Path inputCsvPath=new File(inFilePath).toPath();
Path outputCsvPath=new File(outFilePath).toPath();
List<String[]> sortList=null;


try(Stream<String> stream=Files.lines(inputCsvPath,StandardCharsets.UTF_8)){

sortList=stream.map(s -> s.split(",", -1)).collect(Collectors.toList());
}catch (Exception e) {
e.printStackTrace();
}
// 配列4番目の年齢でソートする
sortList.sort(Comparator.comparingInt(s -> Integer.valueOf(s[3].trim())));

// 降順ソート
Collections.reverse(sortList);

// ソートデータ上位5件を書き出し
// Files.write は、内部でBuferedWriter使用
try {
Files.write(outputCsvPath,
sortList.stream().map(s -> String.join(",", s)).limit(5).collect(Collectors.toList()),
StandardCharsets.UTF_8, // これはUTF8なら必要なし(ディフォルトがUTF-8)
// 書込みモード
StandardOpenOption.WRITE,
// ファイルが存在しないときは作成
StandardOpenOption.CREATE,
// ファイルが存在したら削除
StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {

e.printStackTrace();
}

}

}

22:54 | 投票する | 投票数(0) | コメント(0)
2025/05/12

resultMapサンプル

固定リンク | by:aramaki
resultMapサンプル
--------------------------------
<resultMap type="jp.co.websupport.entity.Customer"
id="CustomerResult">
<result column="custno" property="custno" jdbcType="NUMERIC" />
<result column="custname" property="custName"
jdbcType="VARCHAR" />
<result column="zip" property="zip" jdbcType="CHAR" />
<result column="address" property="address" jdbcType="VARCHAR" />
<result column="tel" property="tel" jdbcType="CHAR" />
</resultMap>
22:45 | 投票する | 投票数(0) | コメント(0)
2025/05/07

メモ

固定リンク | by:aramaki
メモ
---------------------
MultiValueMap<String, TestValue> multiMap=new LinkedMultiValueMap<String, TransFormerValue>();
for(BaseJsonNode baseNode : baseNodeList) {
if(baseNode.isArray()) {
for(JsonNode element : baseNode) {
TestValue value=new TestValue();
String key="";
if (element.isDouble()) {
// 数値の場合
//int numberValue=element.intValue();
float numberValue=element.floatValue();
value.setDecimal(new BigDecimal(numberValue));
System.out.println("数値: " + numberValue);
}else if(element.isTextual()) {
// 文字列の場合
String textValue = element.textValue().trim();
if(textValue.substring(textValue.length()-2).equals("00")){
key=textValue;
}else {
continue;
}
21:29 | 投票する | 投票数(0) | コメント(0)
2025/04/20

ファイル読み込み

固定リンク | by:aramaki
ファイル読み込み
-------------------------
@PostMapping("/rest/json")
public String recieveJson(@RequestBody Info info) {


String res=null;
try(
// out.jsonファイルを読み込み、読み込んだデータをresponseとして返す。
FileReader fileReader=new FileReader("C:/out.json");
BufferedReader bufferedReader=new BufferedReader(fileReader)
) {
// out.jsonファイルを読み込み、読み込んだデータをresponseとして返す。
String line=null;
StringBuilder stringBuffer=new StringBuilder();
while((line=bufferedReader.readLine())!=null) {
stringBuffer.append(line);
}
res=stringBuffer.toString();

return res;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
//String res="{\"resultMessage\":\"OK\"}";
return null;
22:35 | 投票する | 投票数(0) | コメント(0)
2025/04/20

指定した位置から文字列取得

固定リンク | by:aramaki
指定した位置から先の文字を取得する
--------------------------------------------------
public static String getRightString(String strData,int index) {
// 切り出し結果
String result=null;
if(null==strData || strData.length()==0) {
// 切り出し対象文字列がnull、または、空文字の場合、nullを返却する。
return result;
}
if(index > strData.length()-1) {
return result;
}
result=strData.substring(index);
return result;
}
10:48 | 投票する | 投票数(0) | コメント(0)
2025/04/20

文字のきりだしNO2

固定リンク | by:aramaki
文字列の先頭から指定した桁数切り出す。
----------------------------------------------------------
public static String getLeftString(String strData,int index) {
String result=null;
if(null==strData || strData.length()==0) {
// 切り出し対象文字列がnull、または、空文字の場合、nullを返却する。
return result;
}
if(index > strData.length()) {
return result;
}
if(strData.length()>0) {
result=strData.substring(0,index);
}
return result;
}
10:26 | 投票する | 投票数(0) | コメント(0)
2025/04/19

文字列の先頭から指定した桁数切り出す

固定リンク | by:aramaki
文字列の先頭から指定した桁数切り出す
------------------------------------------------------
/**
* 文字列の先頭から指定した桁数切り出す。
* @param strData
* @param index
* @return
*/
public static String getLeftString(String strData,int index) {
String result=null;
if(index > strData.length()) {
return result;
}
if(null!=strData && strData.length()>0) {
result=strData.substring(0,index);
}
return result;
}
22:11 | 投票する | 投票数(0) | コメント(0)
2025/04/16

日時データ部品

固定リンク | by:aramaki
日時データ部品
-----------------------
public class DateTime {
private static SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

private static List<Calendar> calList=new LinkedList<Calendar>();
public static void main(String[] args) {
// 年度開始日と終了日
/*
List<String> list=getYearRange(2025);
for(String date : list) {
System.out.println(date);
}
*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
 
Calendar startCal= makeStartCalender(2025);
System.out.println(sdf.format(startCal.getTime()));
calList.add(startCal);
while(true) {
Calendar cal=getNext30Minite(startCal,30);
System.out.println(sdf.format(cal.getTime()));
int diff=startCal.compareTo(cal);
if(diff==0) {
// startCalとcalが同じ日時
break;
}
calList.add(cal);
}
for(Calendar cal: calList) {
// 日付出力する
String strDate=dateFormat.format(cal);
System.out.println(strDate);
}

}
public static Calendar makeStartCalender(int startYear) {
// 指定した年の4月1日
// 2025年の4月1日の場合
Calendar calendar=Calendar.getInstance();
calendar.set(startYear,3,1,0,0,0);
return calendar;
}
public static Calendar makeEndDate(int year) {
Calendar calendar=Calendar.getInstance();
calendar.set(year+1, 2,31,11,59,30);
return calendar;
}
public static Calendar getNext30Minite(Calendar calendar,int addMinite) {
calendar.add(Calendar.MINUTE, addMinite);
return calendar;
}
23:49 | 投票する | 投票数(0) | コメント(0)
2025/04/16

年度の開始日と終了日を取得する

固定リンク | by:aramaki
年度の開始日と終了日を取得する
----------------------------------------------------
public static List<String> getYearRange(int year) {
List<String> list=new ArrayList<String>();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
LocalDateTime startOfYear=LocalDateTime.of(year, Month.APRIL, 1,0,0);
LocalDateTime endOfYear=LocalDateTime.of(year+1, Month.MARCH, 31,0,0);
String strStartOfYear= startOfYear.format(formatter);
String strEndOfYear= endOfYear.format(formatter);
list.add(strStartOfYear);
list.add(strEndOfYear);
return list;
}
22:06 | 投票する | 投票数(0) | コメント(0)
123456
Copyright © Java活用生活 All Rights Reserved .