Perl で例えば 2012年9月1日 のような日付を、2012/09/01 のような形式に変換したい場合があります。以下はそのための、sscanf を使ったサンプルです。
use String::Scanf; my $hiduke0 = "2012年9月1日"; my ($a,$b,$c)=sscanf('%d年%d月%d日',$hiduke0); printf('%4d/%02d/%02d'. "\n" , $a,$b,$c);
use String::Scanf; my $hiduke0 = "2012年9月1日"; my ($a,$b,$c)=sscanf('%d年%d月%d日',$hiduke0); printf('%4d/%02d/%02d'. "\n" , $a,$b,$c);
sub main7 Dim DatabaseContext as object Dim DataSource as Object Dim Connection as Object Dim Statement as Object Dim sSQL as String Dim oResultSet as Object Dim nDlgResult As Integer Dim sURL as String Dim oProps(2) as new com.sun.star.beans.PropertyValue ' ***** データベースのコネクション ***** DatabaseContext=createUnoService("com.sun.star.sdbc.DriverManager") sURL = "jdbc:mysql://127.0.0.1:3306/stock_db?useUnicode=true&characterEncoding=UTF-8" oProps(0).Name = "user" oProps(0).value = "root" oProps(1).Name = "password" oProps(1).value = "root" oProps(2).name = "JavaDriverClass" oProps(2).value = "com.mysql.jdbc.Driver" Connection = DatabaseContext.getConnectionWithInfo(sURL, oProps()) ' ***** SQLの実行 ***** Statement = Connection.createStatement() nResult = Statement.executeUpdate("INSERT INTO stock (code,hiduke,jikoku,price )" & _ "VALUES (1003, '2012/09/24','15:00',12400)") ' oResultSet = Statement.executeQuery("SELECT * FROM stock;") ' oResultSet.Next ' Msgbox(oResultSet.getString(1)) ' ***** データベースを閉じる ***** Statement.Close() Connection.Close() Connection.Dispose() end sub
import java.sql.*; public class password { public static void main(String[] args) { try{ // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Connection con=DriverManager.getConnection("jdbc:odbc:passwd"); // Statement stmt = con.createStatement(); // String sql = "SELECT * FROM password"; // ResultSet rs = stmt.executeQuery(sql); // while(rs.next()){ // int uniq_id = rs.getInt("uniq_id"); // String keyword = rs.getString("keyword"); // String password = rs.getString("password"); // String date = rs.getString("date"); // System.out.println(uniq_id + " " + keyword + " " + password + " " + date); } stmt.close(); con.close(); }catch(Exception e){ e.printStackTrace(); } } }
#!/usr/bin/perl use DBI; $dbuser="root"; $dbpass="root"; $dbname="stock_db"; $sql="select * from stock"; $dsn = "DBI:mysql:$dbname"; $dsh = DBI -> connect ( $dsn, $dbuser, $dbpass , { RaiseError => 0 } ); $sth = $dsh->prepare($sql); $sth->execute; $num_rows = $sth->rows; print "Content-type: text/html\n\n"; print <<HTML_HEAD; HTML_HEAD if ($dsh) { print "successfully connected to the database<br>\n"; } else { print "fail to connect to the database<br>\n"; } print "number of records : $num_rows <br>\n"; for ($i=0; $i<$num_rows; $i++) { @a = $sth->fetchrow_array; print "code=$a[0], hiduke=$a[1] ,jikoku=$a[2] ,price=$a[3]<br>\n"; } print <<HTML_TAIL; HTML_TAIL $sth->finish; $dsh -> disconnect; exit(0);
使用したバージョン Ubuntu 12.04 LibreOffice 3.4.5.2
sudo apt-get install unixodbc unixodbc-bin libmyodbc
ドライバーの情報およびODBC登録した結果は/etc/odbcinst.ini および/etc/odbc.ini に記録されます。以下は、私のマシンでの/etc/odbcinst.ini の内容です。
[MySQL ODBC Driver] Description = Driver = /usr/lib/i386-linux-gnu/odbc/libmyodbc.so Setup = /usr/lib/i386-linux-gnu/odbc/libodbcmyS.so UsageCount = 1 CPTimeout = CPReuse =
以下のサイトを参照しました。
http://sourceforge.jp/magazine/07/02/28/0121236
●JDBC経由でMySqlに接続しようとして、はじめはうまくいきませんでしたが、以下のサイトを参照してできるようになりました。sudo apt-get install libmysql-java
●ダイレクトに接続する方法としては、以下のサイトを参照しました。
http://ns.uchb.net/os/libreoffice.html
Synapticパッケージマネージャから"openclipart-libreoffice"をインストールします。
●参考
MySQLへのODBC経由での接続
http://chaos-fractal.blogspot.jp/2011/11/mysql-odbc.html
OpenOfficeからSQLiteに接続する
http://www.serendip.ws/archives/5250
import java.io.*; public class ReadLine{ public static void main(String[] args){ try{ BufferedReader bf = new BufferedReader (new InputStreamReader(System.in)); System.out.print("Enter your name:"); String yourname = bf.readLine(); System.out.println("helo:" + yourname); } catch(Exception e){ e.printStackTrace(); } } }
import java.sql.*; public class GetAllRows{ public static void main(String[] args) { System.out.println("Getting All Rows from a table!"); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db = "employee_db"; String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; try{ Class.forName(driver).newInstance(); con = DriverManager.getConnection(url+db, user, pass); try{ Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM employee"); System.out.println("code: " + "\t" + "employee: "); while (res.next()) { int i = res.getInt("id"); String s = res.getString("employee"); System.out.println(i + "\t\t" + s); } con.close(); } catch (SQLException s){ System.out.println("SQL code does not execute."); } } catch (Exception e){ e.printStackTrace(); } } }
import java.util.Calendar; public class checktime{ public static void main(String[] args) { String Timedate = checkTime(); System.out.println(Timedate); } private static String checkTime(){ String modori; Calendar cal1 = Calendar.getInstance(); int year = cal1.get(Calendar.YEAR); int month = cal1.get(Calendar.MONTH) + 1; int day = cal1.get(Calendar.DATE); int hour = cal1.get(Calendar.HOUR_OF_DAY); int minute = cal1.get(Calendar.MINUTE); int second = cal1.get(Calendar.SECOND); StringBuffer dow = new StringBuffer(); switch (cal1.get(Calendar.DAY_OF_WEEK)) { case Calendar.SUNDAY: dow.append("日曜日"); break; case Calendar.MONDAY: dow.append("月曜日"); break; case Calendar.TUESDAY: dow.append("火曜日"); break; case Calendar.WEDNESDAY: dow.append("水曜日"); break; case Calendar.THURSDAY: dow.append("木曜日"); break; case Calendar.FRIDAY: dow.append("金曜日"); break; case Calendar.SATURDAY: dow.append("土曜日"); break; } modori =String.format("%04d/%02d/%02d%s%02d:%02d:%02d" ,year,month,day,dow,hour,minute,second); return modori; } }
import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Locale; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.write.Label; //import jxl.write.Number; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class ExcelWrite { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try{ WorkbookSettings settings = new WorkbookSettings(); settings.setLocale(new Locale("ja", "JP")); settings.setEncoding("Windows-31J"); OutputStream os = new FileOutputStream("output.xls"); WritableWorkbook workbook = Workbook.createWorkbook(os, settings); // シートの新規作成 WritableSheet sheet = workbook.createSheet("sheet1", 0); // ※ここからシートの書き込み処理を記述する Label label = new Label(0, 0, "サンプル"); sheet.addCell(label); // 終了処理 workbook.write(); workbook.close(); }catch(Exception ex){ System.err.println("失敗しました.:" + ex.toString()); }finally{ System.out.println("終了しました.:"); ; } } }
import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Locale; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.write.Label; import jxl.write.Number; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; /* import java.io.File; import java.util.Date; */ import jxl.*; import jxl.write.*; public class ExcelWrite { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub WritableWorkbook workbook=null; String outputFileName = "output.xls"; try{ workbook = Workbook.createWorkbook(new File(outputFileName)); WritableSheet sheet = workbook.createSheet("Sheet1", 0); // WritableSheet sheet = workbook.createSheet("First Sheet", 0); Label label = new Label(0, 2, "サンプル"); sheet.addCell(label); Number number = new Number(3, 4,12800); sheet.addCell(number); workbook.write(); workbook.close(); }catch(Exception ex){ System.err.println("失敗しました.:" + ex.toString()); }finally{ System.out.println("終了しました.:"); ; } } }
import java.io.File; import java.io.FileWriter; import java.io.IOException; class FileAppend{ public static void main(String args[]){ try{ File file = new File("test.txt"); FileWriter filewriter = new FileWriter(file, true); filewriter.write("この行を追加します1\n"); filewriter.write("この行も追加します2\n"); filewriter.close(); }catch(IOException e){ System.out.println(e); } } }
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class FileRead { public static void main(String[] args) { String[] strarray = new String[1000]; int i = 0; try{ File file = new File("zzz.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String str= null; i=0; while((str = br.readLine()) != null){ System.out.println(str); strarray[i]=str; i = i +1; } br.close(); }catch(FileNotFoundException e){ System.out.println(e); }catch(IOException e){ System.out.println(e); } } }
import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.Number; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; public class ExcelEdit { public static void main(String[] args) { int line = 8; Workbook workbook = null; try { WorkbookSettings ws = new WorkbookSettings(); ws.setGCDisabled(true); workbook = Workbook.getWorkbook(new File("output.xls"), ws); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (workbook != null) { ; } } File outputFile= new File("output.xls"); WritableWorkbook workbook1=null; try { System.out.println(outputFile + "ブックを新規作成"); workbook1 = Workbook.createWorkbook(outputFile,workbook); if (workbook1 == null) { System.err.println(outputFile + "ブック作成に失敗"); } } catch (IOException ex) { System.err.println(ex.toString()); } WritableSheet sheet = workbook1.getSheet(0); if (sheet == null) { System.err.println(outputFile + "シート設定に失敗"); } Label label = new Label(1, line, "サンプル"); try { sheet.addCell(label); } catch (WriteException ex) { System.err.println("セルの書き込みに失敗" + ex.toString()); } Number number = new Number(0, line, 12800); try { sheet.addCell(number); } catch (WriteException ex) { System.err.println("line セルの書き込みに失敗" + ex.toString()); } System.out.println("日付セルを新規作成"); SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); String dateStr = format.format(new Date()); Label label2 = new Label(2, line, dateStr); try { sheet.addCell(label2); } catch (WriteException ex) { System.err.println("セルの書き込みに失敗" + ex.toString()); } try { workbook1.write(); } catch (IOException ex) { if (workbook1 != null) { System.err.println(outputFile + "ブックの書き込みに失敗" + ex.toString()); } } try { workbook1.close(); workbook.close(); } catch (Exception ex) { System.err.println(outputFile + "ブックのクローズに失敗" + ex.toString()); } } }