-
Hello, Java 2D World!
Posted on 11月 5th, 2012 by cx20
Java 2D
Java 2D は Java による 2D グラフィックライブラリである。
ソースコード
import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.*; public class Hello extends JFrame { public static void main(String[] args) { Hello frame = new Hello("Hello, World"); frame.setVisible(true); } Hello( String title ) { super( title ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); HelloPanel panel = new HelloPanel(); add( panel ); } } class HelloPanel extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.drawString("Hello, Java2D World!", 0, 16); } }
コンパイル&実行方法
C:¥> javac Hello.java C:¥> javaw Hello
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Java 2D World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, JavaFX(Java) World!
Posted on 11月 4th, 2012 by cx20
JavaFX(Java)
JavaFX は Java による RIA 向けの GUI ライブラリならびにプラットフォームである。
類似の RIA プラットフォームとしては Adobe Flex や Microsoft Silverlight などがある。ソースコード
import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.control.Label; public class Hello extends Application { public static void main(String[] args) { Application.launch(Hello.class, args); } @Override public void start(Stage stage) { HBox hbox = new HBox(); Scene scene = new Scene(hbox, 640, 480); hbox.getChildren().add( new Label("Hello, JavaFX World!") ); stage.setScene(scene); stage.setTitle("Hello, World"); stage.show(); } }
コンパイル&実行方法
C:¥> SET JAVAFX_HOME=C:\Program Files (x86)\Oracle\JavaFX 2.0 SDK C:¥> javac -cp "%JAVAFX_HOME%\rt\lib\jfxrt.jar;." Hello.java C:¥> javaw -cp "%JAVAFX_HOME%\rt\lib\jfxrt.jar;." Hello
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, JavaFX World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, SWT(Java) World!
Posted on 11月 3rd, 2012 by cx20
SWT(Java)
SWT(Standard Widget Toolkit) は Java で GUI を扱うためのライブラリである。
IBM により AWT や Swing を置き換える目的で作成された。ソースコード
import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; public class Hello { public static void main (String [] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Hello, World"); FillLayout layout = new FillLayout(SWT.VERTICAL); shell.setLayout(layout); Label label = new Label(shell,SWT.BORDER); label.setText("Hello, SWT World!"); shell.setSize( 640, 480 ); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }
コンパイル&実行方法
C:¥> javac -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello.java C:¥> javaw -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, SWT World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Swing(Java) World!
Posted on 11月 2nd, 2012 by cx20
Swing(Java)
Swing は AWT(Abstract Window Toolkit) を拡張したものであり Java で GUI を扱うためのライブラリである。
J2SE 1.2 以降は AWT よりも Swing が使われることが多くなっている。ソースコード
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Hello extends JFrame { public static void main( String args [] ) { Hello frame = new Hello( "Hello, World" ); frame.setVisible( true ); } Hello( String title ) { super( title ); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setLocationRelativeTo( null ); setSize( 640, 480 ); JLabel label = new JLabel( "Hello, Swing World!" ); label.setVerticalAlignment(JLabel.TOP); add( label ); } }
コンパイル&実行方法
C:¥> javac Hello.java C:¥> javaw Hello
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Swing World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, AWT(Java) World!
Posted on 11月 1st, 2012 by cx20
AWT(Java)
AWT(Abstract Window Toolkit) は Java で GUI を扱うためのライブラリである。
J2SE 1.2 以降は AWT を拡張した Swing が使われることが多くなっている。ソースコード
import java.awt.*; import java.awt.event.*; public class Hello extends Frame { public static void main(String [] args) { Hello frame = new Hello( "Hello, World" ); frame.setVisible(true); } Hello( String title ) { super( title ); addWindowListener(new HelloWindowAdapter()); setSize(640, 480); setLayout(new FlowLayout(FlowLayout.LEFT)); Label label = new Label("Hello, AWT World!"); add(label); } } class HelloWindowAdapter extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } }
コンパイル&実行方法
C:¥> javac Hello.java C:¥> javaw Hello
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, AWT World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, TypeScript World!
Posted on 10月 31st, 2012 by cx20
TypeScript
TypeScript はマイクロソフト社により開発された静的型付けを特徴としたスクリプト言語である。JavaScript にコンパイルすることが可能となっている。
プログラムは TypeScript の公式サイト(http://www.typescriptlang.org/Playground/)で試すことができる。ソースコード(TypeScript)
class HelloWorld { message: string; constructor(message: string) { this.message = message; } sayHello() { console.log("Hello, " + this.message + " World!"); } } var hello = new HelloWorld("TypeScript"); hello.sayHello();
上記コードを JavaScript にコンパイルした場合、以下のコードが生成される。
ソースコード(JavaScript)
var HelloWorld = (function () { function HelloWorld(message) { this.message = message; } HelloWorld.prototype.sayHello = function () { console.log("Hello, " + this.message + " World!"); }; return HelloWorld; })(); var hello = new HelloWorld("TypeScript"); hello.sayHello();
コンパイル方法
C:¥> tsc hello.ts
実行方法(Node.js による実行例)
C:¥> node hello.js
実行結果
Hello, TypeScript World!
-
Hello, JDBC Type4(Jasmin) World!
Posted on 10月 30th, 2012 by cx20
JDBC Type4
JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
Type4 は DBMS のクライアントライブラリを使用しない Pure Java の DBMS ドライバである。
以下は Jasmin による JDBC ライブラリの使用例となっている。ソースコード(Jasmin + JDBC Type4 + SQL Server)
; Produced by NeoJasminVisitor (tinapoc) ; http://tinapoc.sourceforge.net ; The original JasminVisitor is part of the BCEL ; http://jakarta.apache.org/bcel/ ; Tue Oct 16 23:33:16 JST 2012 .bytecode 50.0 .source Hello.java .class Hello .super java/lang/Object .method <init>()V .limit stack 1 .limit locals 1 .var 0 is this LHello; from Label0 to Label1 Label0: .line 3 0: aload_0 1: invokespecial java/lang/Object/<init>()V Label1: 4: return .end method .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 4 .var 0 is arg0 [Ljava/lang/String; from Label2 to Label3 Label2: .line 6 0: ldc "com.microsoft.sqlserver.jdbc.SQLServerDriver" 2: invokestatic java/lang/Class/forName(Ljava/lang/String;)Ljava/lang/Class; 5: pop .line 8 6: ldc "jdbc:sqlserver://;serverName=localhost" 8: ldc "sa" 10: ldc "P@ssW0rd" 12: invokestatic java/sql/DriverManager/getConnection(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection; 15: astore_1 .line 9 16: aload_1 17: invokeinterface java/sql/Connection/createStatement()Ljava/sql/Statement; 1 22: astore_2 .line 10 23: aload_2 24: ldc "SELECT 'Hello, JDBC Type4 World!' AS Message" 26: invokeinterface java/sql/Statement/executeQuery(Ljava/lang/String;)Ljava/sql/ResultSet; 2 31: astore_3 Label1: .line 11 32: aload_3 33: invokeinterface java/sql/ResultSet/next()Z 1 38: ifeq Label0 .line 12 41: getstatic java.lang.System.out Ljava/io/PrintStream; 44: aload_3 45: iconst_1 46: invokeinterface java/sql/ResultSet/getString(I)Ljava/lang/String; 2 51: invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V 54: goto Label1 Label0: .line 14 57: aload_3 58: invokeinterface java/sql/ResultSet/close()V 1 .line 15 63: aload_2 64: invokeinterface java/sql/Statement/close()V 1 .line 16 69: aload_1 70: invokeinterface java/sql/Connection/close()V 1 Label3: .line 17 75: return .throws java/lang/Exception .end method
実行方法(Jasmin + JDBC Type4 + SQL Server)
C:¥> java -jar jasmin.jar Hello.j C:¥> java -cp sqljdbc4.jar;. Hello
ソースコード(Jasmin + JDBC Type4 + Oracle)
; Produced by NeoJasminVisitor (tinapoc) ; http://tinapoc.sourceforge.net ; The original JasminVisitor is part of the BCEL ; http://jakarta.apache.org/bcel/ ; Tue Oct 16 23:37:45 JST 2012 .bytecode 50.0 .source Hello.java .class Hello .super java/lang/Object .method <init>()V .limit stack 1 .limit locals 1 .var 0 is this LHello; from Label0 to Label1 Label0: .line 3 0: aload_0 1: invokespecial java/lang/Object/<init>()V Label1: 4: return .end method .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 4 .var 0 is arg0 [Ljava/lang/String; from Label2 to Label3 Label2: .line 6 0: ldc "oracle.jdbc.driver.OracleDriver" 2: invokestatic java/lang/Class/forName(Ljava/lang/String;)Ljava/lang/Class; 5: pop .line 8 6: ldc "jdbc:oracle:thin:@localhost:1521:orcl" 8: ldc "scott" 10: ldc "tiger" 12: invokestatic java/sql/DriverManager/getConnection(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection; 15: astore_1 .line 9 16: aload_1 17: invokeinterface java/sql/Connection/createStatement()Ljava/sql/Statement; 1 22: astore_2 .line 10 23: aload_2 24: ldc "SELECT 'Hello, JDBC Type4 World!' AS Message FROM DUAL" 26: invokeinterface java/sql/Statement/executeQuery(Ljava/lang/String;)Ljava/sql/ResultSet; 2 31: astore_3 Label1: .line 11 32: aload_3 33: invokeinterface java/sql/ResultSet/next()Z 1 38: ifeq Label0 .line 12 41: getstatic java.lang.System.out Ljava/io/PrintStream; 44: aload_3 45: iconst_1 46: invokeinterface java/sql/ResultSet/getString(I)Ljava/lang/String; 2 51: invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V 54: goto Label1 Label0: .line 14 57: aload_3 58: invokeinterface java/sql/ResultSet/close()V 1 .line 15 63: aload_2 64: invokeinterface java/sql/Statement/close()V 1 .line 16 69: aload_1 70: invokeinterface java/sql/Connection/close()V 1 Label3: .line 17 75: return .throws java/lang/Exception .end method
実行方法(Jasmin + JDBC Type4 + Oracle)
C:¥> java -jar jasmin.jar Hello.j C:¥> java -cp ojdbc6.jar;. Hello
ソースコード(Jasmin + JDBC Type4 + MySQL)
; Produced by NeoJasminVisitor (tinapoc) ; http://tinapoc.sourceforge.net ; The original JasminVisitor is part of the BCEL ; http://jakarta.apache.org/bcel/ ; Tue Oct 16 23:37:45 JST 2012 .bytecode 50.0 .source Hello.java .class Hello .super java/lang/Object .method <init>()V .limit stack 1 .limit locals 1 .var 0 is this LHello; from Label0 to Label1 Label0: .line 3 0: aload_0 1: invokespecial java/lang/Object/<init>()V Label1: 4: return .end method .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 4 .var 0 is arg0 [Ljava/lang/String; from Label2 to Label3 Label2: .line 6 0: ldc "com.mysql.jdbc.Driver" 2: invokestatic java/lang/Class/forName(Ljava/lang/String;)Ljava/lang/Class; 5: pop .line 8 6: ldc "jdbc:mysql://localhost:3306" 8: ldc "root" 10: ldc "P@ssW0rd" 12: invokestatic java/sql/DriverManager/getConnection(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection; 15: astore_1 .line 9 16: aload_1 17: invokeinterface java/sql/Connection/createStatement()Ljava/sql/Statement; 1 22: astore_2 .line 10 23: aload_2 24: ldc "SELECT 'Hello, JDBC Type4 World!' AS Message" 26: invokeinterface java/sql/Statement/executeQuery(Ljava/lang/String;)Ljava/sql/ResultSet; 2 31: astore_3 Label1: .line 11 32: aload_3 33: invokeinterface java/sql/ResultSet/next()Z 1 38: ifeq Label0 .line 12 41: getstatic java.lang.System.out Ljava/io/PrintStream; 44: aload_3 45: iconst_1 46: invokeinterface java/sql/ResultSet/getString(I)Ljava/lang/String; 2 51: invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V 54: goto Label1 Label0: .line 14 57: aload_3 58: invokeinterface java/sql/ResultSet/close()V 1 .line 15 63: aload_2 64: invokeinterface java/sql/Statement/close()V 1 .line 16 69: aload_1 70: invokeinterface java/sql/Connection/close()V 1 Label3: .line 17 75: return .throws java/lang/Exception .end method
実行方法(Jasmin + JDBC Type4 + MySQL)
C:¥> java -jar jasmin.jar Hello.j C:¥> java -cp mysql-connector-java-5.1.22-bin.jar;. Hello
実行結果
Hello, JDBC Type4 World!
-
Hello, JDBC Type2(Jasmin) World!
Posted on 10月 29th, 2012 by cx20
JDBC Type2
JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
Type2 は JDBC と DBMS クライアントの API をマップさせたブリッジドライバである。
例えば、Oracle であればクライアントライブラリとして OCI が使用される。
以下は Jasmin による JDBC ライブラリの使用例となっている。ソースコード(Jasmin + JDBC Type2 + Oracle)
; Produced by NeoJasminVisitor (tinapoc) ; http://tinapoc.sourceforge.net ; The original JasminVisitor is part of the BCEL ; http://jakarta.apache.org/bcel/ ; Tue Oct 16 23:37:45 JST 2012 .bytecode 50.0 .source Hello.java .class Hello .super java/lang/Object .method <init>()V .limit stack 1 .limit locals 1 .var 0 is this LHello; from Label0 to Label1 Label0: .line 3 0: aload_0 1: invokespecial java/lang/Object/<init>()V Label1: 4: return .end method .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 4 .var 0 is arg0 [Ljava/lang/String; from Label2 to Label3 Label2: .line 6 0: ldc "oracle.jdbc.driver.OracleDriver" 2: invokestatic java/lang/Class/forName(Ljava/lang/String;)Ljava/lang/Class; 5: pop .line 8 6: ldc "jdbc:oracle:oci:@orcl" 8: ldc "scott" 10: ldc "tiger" 12: invokestatic java/sql/DriverManager/getConnection(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection; 15: astore_1 .line 9 16: aload_1 17: invokeinterface java/sql/Connection/createStatement()Ljava/sql/Statement; 1 22: astore_2 .line 10 23: aload_2 24: ldc "SELECT 'Hello, JDBC Type4 World!' AS Message FROM DUAL" 26: invokeinterface java/sql/Statement/executeQuery(Ljava/lang/String;)Ljava/sql/ResultSet; 2 31: astore_3 Label1: .line 11 32: aload_3 33: invokeinterface java/sql/ResultSet/next()Z 1 38: ifeq Label0 .line 12 41: getstatic java.lang.System.out Ljava/io/PrintStream; 44: aload_3 45: iconst_1 46: invokeinterface java/sql/ResultSet/getString(I)Ljava/lang/String; 2 51: invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V 54: goto Label1 Label0: .line 14 57: aload_3 58: invokeinterface java/sql/ResultSet/close()V 1 .line 15 63: aload_2 64: invokeinterface java/sql/Statement/close()V 1 .line 16 69: aload_1 70: invokeinterface java/sql/Connection/close()V 1 Label3: .line 17 75: return .throws java/lang/Exception .end method
実行方法
C:¥> java -jar jasmin.jar Hello.j C:¥> java -cp ojdbc6.jar;. Hello
実行結果
Hello, JDBC Type2 World!
-
Hello, JDBC Type1(Jasmin) World!
Posted on 10月 28th, 2012 by cx20
JDBC Type1
JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
Type1 は JDBC と ODBC の API をマップさせたブリッジドライバである。
以下は Jasmin による JDBC ライブラリの使用例となっている。ソースコード(Jasmin + JDBC Type1 + ODBC + Jet データベース)
; Produced by NeoJasminVisitor (tinapoc) ; http://tinapoc.sourceforge.net ; The original JasminVisitor is part of the BCEL ; http://jakarta.apache.org/bcel/ ; Tue Oct 16 02:25:36 JST 2012 .bytecode 50.0 .source Hello.java .class Hello .super java/lang/Object .method <init>()V .limit stack 1 .limit locals 1 .var 0 is this LHello; from Label0 to Label1 Label0: .line 4 0: aload_0 1: invokespecial java/lang/Object/<init>()V Label1: 4: return .end method .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 4 .var 0 is arg0 [Ljava/lang/String; from Label2 to Label3 Label2: .line 7 0: ldc "sun.jdbc.odbc.JdbcOdbcDriver" 2: invokestatic java/lang/Class/forName(Ljava/lang/String;)Ljava/lang/Class; 5: pop .line 9 6: ldc "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=hello.mdb" 8: ldc "" 10: ldc "" 12: invokestatic java/sql/DriverManager/getConnection(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection; 15: astore_1 .line 10 16: aload_1 17: invokeinterface java/sql/Connection/createStatement()Ljava/sql/Statement; 1 22: astore_2 .line 11 23: aload_2 24: ldc "SELECT 'Hello, JDBC Type1 World!' AS Message" 26: invokeinterface java/sql/Statement/executeQuery(Ljava/lang/String;)Ljava/sql/ResultSet; 2 31: astore_3 Label1: .line 12 32: aload_3 33: invokeinterface java/sql/ResultSet/next()Z 1 38: ifeq Label0 .line 13 41: getstatic java.lang.System.out Ljava/io/PrintStream; 44: aload_3 45: iconst_1 46: invokeinterface java/sql/ResultSet/getString(I)Ljava/lang/String; 2 51: invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V 54: goto Label1 Label0: .line 15 57: aload_3 58: invokeinterface java/sql/ResultSet/close()V 1 .line 16 63: aload_2 64: invokeinterface java/sql/Statement/close()V 1 .line 17 69: aload_1 70: invokeinterface java/sql/Connection/close()V 1 Label3: .line 18 75: return .throws java/lang/Exception .end method
ソースコード(Jasmin + JDBC Type1 + ODBC + ACE データベース)
; Produced by NeoJasminVisitor (tinapoc) ; http://tinapoc.sourceforge.net ; The original JasminVisitor is part of the BCEL ; http://jakarta.apache.org/bcel/ ; Tue Oct 16 02:25:49 JST 2012 .bytecode 50.0 .source Hello.java .class Hello .super java/lang/Object .method <init>()V .limit stack 1 .limit locals 1 .var 0 is this LHello; from Label0 to Label1 Label0: .line 4 0: aload_0 1: invokespecial java/lang/Object/<init>()V Label1: 4: return .end method .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 4 .var 0 is arg0 [Ljava/lang/String; from Label2 to Label3 Label2: .line 7 0: ldc "sun.jdbc.odbc.JdbcOdbcDriver" 2: invokestatic java/lang/Class/forName(Ljava/lang/String;)Ljava/lang/Class; 5: pop .line 11 6: ldc "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=.\hello.accdb" 8: ldc "" 10: ldc "" 12: invokestatic java/sql/DriverManager/getConnection(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection; 15: astore_1 .line 13 16: aload_1 17: invokeinterface java/sql/Connection/createStatement()Ljava/sql/Statement; 1 22: astore_2 .line 14 23: aload_2 24: ldc "SELECT 'Hello, JDBC Type1 World!' AS Message" 26: invokeinterface java/sql/Statement/executeQuery(Ljava/lang/String;)Ljava/sql/ResultSet; 2 31: astore_3 Label1: .line 15 32: aload_3 33: invokeinterface java/sql/ResultSet/next()Z 1 38: ifeq Label0 .line 16 41: getstatic java.lang.System.out Ljava/io/PrintStream; 44: aload_3 45: iconst_1 46: invokeinterface java/sql/ResultSet/getString(I)Ljava/lang/String; 2 51: invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V 54: goto Label1 Label0: .line 18 57: aload_3 58: invokeinterface java/sql/ResultSet/close()V 1 .line 19 63: aload_2 64: invokeinterface java/sql/Statement/close()V 1 .line 20 69: aload_1 70: invokeinterface java/sql/Connection/close()V 1 Label3: .line 21 75: return .throws java/lang/Exception .end method
ソースコード(Jasmin + JDBC Type1 + ODBC + SQL Server)
; Produced by NeoJasminVisitor (tinapoc) ; http://tinapoc.sourceforge.net ; The original JasminVisitor is part of the BCEL ; http://jakarta.apache.org/bcel/ ; Tue Oct 16 02:08:35 JST 2012 .bytecode 50.0 .source Hello.java .class Hello .super java/lang/Object .method <init>()V .limit stack 1 .limit locals 1 .var 0 is this LHello; from Label0 to Label1 Label0: .line 4 0: aload_0 1: invokespecial java/lang/Object/<init>()V Label1: 4: return .end method .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 4 .var 0 is arg0 [Ljava/lang/String; from Label2 to Label3 Label2: .line 8 0: ldc "sun.jdbc.odbc.JdbcOdbcDriver" 2: invokestatic java/lang/Class/forName(Ljava/lang/String;)Ljava/lang/Class; 5: pop .line 11 6: ldc "jdbc:odbc:Driver={SQL Server};SERVER=(local)" 8: ldc "sa" 10: ldc "P@ssW0rd" 12: invokestatic java/sql/DriverManager/getConnection(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection; 15: astore_1 .line 13 16: aload_1 17: invokeinterface java/sql/Connection/createStatement()Ljava/sql/Statement; 1 22: astore_2 .line 14 23: aload_2 24: ldc "SELECT 'Hello, JDBC Type1 World!' AS Message" 26: invokeinterface java/sql/Statement/executeQuery(Ljava/lang/String;)Ljava/sql/ResultSet; 2 31: astore_3 Label1: .line 15 32: aload_3 33: invokeinterface java/sql/ResultSet/next()Z 1 38: ifeq Label0 .line 16 41: getstatic java.lang.System.out Ljava/io/PrintStream; 44: aload_3 45: iconst_1 46: invokeinterface java/sql/ResultSet/getString(I)Ljava/lang/String; 2 51: invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V 54: goto Label1 Label0: .line 18 57: aload_3 58: invokeinterface java/sql/ResultSet/close()V 1 .line 19 63: aload_2 64: invokeinterface java/sql/Statement/close()V 1 .line 20 69: aload_1 70: invokeinterface java/sql/Connection/close()V 1 Label3: .line 21 75: return .throws java/lang/Exception .end method
ソースコード(Jasmin + JDBC Type1 + ODBC + Oracle)
; Produced by NeoJasminVisitor (tinapoc) ; http://tinapoc.sourceforge.net ; The original JasminVisitor is part of the BCEL ; http://jakarta.apache.org/bcel/ ; Tue Oct 16 02:41:22 JST 2012 .bytecode 50.0 .source Hello.java .class Hello .super java/lang/Object .method <init>()V .limit stack 1 .limit locals 1 .var 0 is this LHello; from Label0 to Label1 Label0: .line 4 0: aload_0 1: invokespecial java/lang/Object/<init>()V Label1: 4: return .end method .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 4 .var 0 is arg0 [Ljava/lang/String; from Label2 to Label3 Label2: .line 7 0: ldc "sun.jdbc.odbc.JdbcOdbcDriver" 2: invokestatic java/lang/Class/forName(Ljava/lang/String;)Ljava/lang/Class; 5: pop .line 8 6: ldc "jdbc:odbc:Driver={Oracle in OraDb11g_home1};DBQ=ORCL" 8: ldc "scott" 10: ldc "tiger" 12: invokestatic java/sql/DriverManager/getConnection(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection; 15: astore_1 .line 9 16: aload_1 17: invokeinterface java/sql/Connection/createStatement()Ljava/sql/Statement; 1 22: astore_2 .line 10 23: aload_2 24: ldc "SELECT 'Hello, JDBC Type1 World!' AS Message FROM DUAL" 26: invokeinterface java/sql/Statement/executeQuery(Ljava/lang/String;)Ljava/sql/ResultSet; 2 31: astore_3 Label1: .line 11 32: aload_3 33: invokeinterface java/sql/ResultSet/next()Z 1 38: ifeq Label0 .line 12 41: getstatic java.lang.System.out Ljava/io/PrintStream; 44: aload_3 45: iconst_1 46: invokeinterface java/sql/ResultSet/getString(I)Ljava/lang/String; 2 51: invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V 54: goto Label1 Label0: .line 14 57: aload_3 58: invokeinterface java/sql/ResultSet/close()V 1 .line 15 63: aload_2 64: invokeinterface java/sql/Statement/close()V 1 .line 16 69: aload_1 70: invokeinterface java/sql/Connection/close()V 1 Label3: .line 17 75: return .throws java/lang/Exception .end method
ソースコード(Jasmin + JDBC Type1 + ODBC + MySQL)
; Produced by NeoJasminVisitor (tinapoc) ; http://tinapoc.sourceforge.net ; The original JasminVisitor is part of the BCEL ; http://jakarta.apache.org/bcel/ ; Tue Oct 16 02:26:06 JST 2012 .bytecode 50.0 .source Hello.java .class Hello .super java/lang/Object .method <init>()V .limit stack 1 .limit locals 1 .var 0 is this LHello; from Label0 to Label1 Label0: .line 4 0: aload_0 1: invokespecial java/lang/Object/<init>()V Label1: 4: return .end method .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 4 .var 0 is arg0 [Ljava/lang/String; from Label2 to Label3 Label2: .line 7 0: ldc "sun.jdbc.odbc.JdbcOdbcDriver" 2: invokestatic java/lang/Class/forName(Ljava/lang/String;)Ljava/lang/Class; 5: pop .line 8 6: ldc "jdbc:odbc:Driver={MySQL ODBC 5.1 Driver};Server=localhost" 8: ldc "root" 10: ldc "P@ssW0rd" 12: invokestatic java/sql/DriverManager/getConnection(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection; 15: astore_1 .line 9 16: aload_1 17: invokeinterface java/sql/Connection/createStatement()Ljava/sql/Statement; 1 22: astore_2 .line 10 23: aload_2 24: ldc "SELECT 'Hello, JDBC Type1 World!' AS Message" 26: invokeinterface java/sql/Statement/executeQuery(Ljava/lang/String;)Ljava/sql/ResultSet; 2 31: astore_3 Label1: .line 11 32: aload_3 33: invokeinterface java/sql/ResultSet/next()Z 1 38: ifeq Label0 .line 12 41: getstatic java.lang.System.out Ljava/io/PrintStream; 44: aload_3 45: iconst_1 46: invokeinterface java/sql/ResultSet/getString(I)Ljava/lang/String; 2 51: invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V 54: goto Label1 Label0: .line 14 57: aload_3 58: invokeinterface java/sql/ResultSet/close()V 1 .line 15 63: aload_2 64: invokeinterface java/sql/Statement/close()V 1 .line 16 69: aload_1 70: invokeinterface java/sql/Connection/close()V 1 Label3: .line 17 75: return .throws java/lang/Exception .end method
実行方法
C:¥> java -jar jasmin.jar Hello.j C:¥> java -jar Hello
実行結果
Hello, JDBC Type1 World!
-
Hello, JDBC Type4(Fantom) World!
Posted on 10月 27th, 2012 by cx20
JDBC Type4
JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
Type4 は DBMS のクライアントライブラリを使用しない Pure Java の DBMS ドライバである。
以下は Fantom による JDBC ライブラリの使用例となっている。ディレクトリ構成
%FANTOM_HOME% /etc /sql … DB ライブラリ設定ファイル配置場所 /lib /java /ext … ライブラリ配置場所 1. %FANTOM_HOME%etcsql に DB ライブラリ設定ファイルを配置 config.props 2. %FANTOM_HOME%libjavaetc にライブラリ配置 sqljdbc4.jar ojdbc6.jar mysql-connector-java-5.1.22-bin.jarソースコード(Fantom + JDBC Type4 + SQL Server)
using sql class Hello { static Void main() { db := SqlConn.open("jdbc:sqlserver://;serverName=localhost", "sa", "P@ssW0rd") stmt := db.sql("SELECT 'Hello, JDBC Type4 World!' AS Message").query stmt.each |Obj row| { echo(row->Message) } } }
設定ファイル(Fantom + JDBC Type4 + SQL Server)
java.drivers=com.microsoft.sqlserver.jdbc.SQLServerDriver
ソースコード(Fantom + JDBC Type4 + Oracle)
using sql class Hello { static Void main() { db := SqlConn.open("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger") stmt := db.sql("SELECT 'Hello, JDBC Type4 World!' AS Message FROM DUAL").query stmt.each |Obj row| { echo(row->Message) } } }
設定ファイル(Fantom + JDBC Type4 + Oracle)
java.drivers=oracle.jdbc.driver.OracleDriver
ソースコード(Fantom + JDBC Type4 + MySQL)
using sql class Hello { static Void main() { db := SqlConn.open("jdbc:mysql://localhost:3306", "root", "P@ssW0rd") stmt := db.sql("SELECT 'Hello, JDBC Type4 World!' AS Message").query stmt.each |Obj row| { echo(row->Message) } } }
設定ファイル(Fantom + JDBC Type4 + MySQL)
java.drivers=com.mysql.jdbc.Driver
実行方法
C:¥> fan hello.fan
実行結果
Hello, JDBC Type4 World!