javafx學習之簡單數據操作示例


今天來學習一下用javafx來開發操作數據庫的代碼,示例是用oracle10g數據庫,開發工具是 netbeans 6 beat 2.

注意:在代碼中我會把中文注解刪掉,在netbeans 6 beat2 中有個BUG,有中文編譯通不過,原工程式也會同時上傳.

import java.lang.Thread;
import java.lang.Exception;
import java.sql.*;
//驅動類
var driverClassName = 'oracle.jdbc.OracleDriver';
//驅動url
var jdbcUrl         = 'jdbc:oracle:thin:@localhost:1521:orcl';
//用戶名
var user            = 'chen';
//密碼
var password        = 'chen';


//定義數據對象
var driver:Driver     = null;
var conn  :Connection = null;
var stmt  :Statement  = null;
var rs    :ResultSet  = null;

var rows  :Number;


try {
 
    var thread      = Thread.currentThread();
    var classLoader = thread.getContextClassLoader();
    var driverClass = classLoader.loadClass(driverClassName);
   
    //注冊當前數據庫驅動
    driver = (Driver) driverClass.instantiate();
    DriverManager.registerDriver(driver);
   
    //以下代碼跟jdbc相似
    try {
        //獲得Connection
        conn = DriverManager.getConnection(jdbcUrl, user, password);
        stmt = conn.createStatement();
        //獲得Statement
       
        //創建一個Uuser表
        rows = stmt.executeUpdate("CREATE TABLE Uuser(id INT NOT NULL ,UName VARCHAR2(50))");
        println("CREATE TABLE rows: {rows}");
       
        //插一條記錄
        rows = stmt.executeUpdate("INSERT INTO Uuser VALUES(1, 'chen')");
        println("INSERT rows: {rows}");
       
        //插另一條記錄
        rows = stmt.executeUpdate("INSERT INTO Uuser VALUES(2, 'zhan')");
        println("INSERT rows: {rows}");
       
        //查詢Uuser表
        rs = stmt.executeQuery("SELECT * FROM Uuser");
        while(rs.next()) {
            println("id: {rs.getInt('id')} task: {rs.getString('UName')}");
        }
    } catch(e:SQLException) {
        e.printStackTrace();
    } finally {
        //ResultSet不為空關閉
        if(null <> rs){rs.close();}
        //Statement不為空關閉
        if(null <> stmt) {
            try {
                stmt.close();
            } catch(e:SQLException) {
                e.printStackTrace();
            }
        }
       
        if(null <> conn){conn.close();}//Connection不為空關閉
    }
} catch(e:Exception) {
    e.printStackTrace();
}

程序中的源碼,如下:

import java.lang.Thread;
import java.lang.Exception;
import java.sql.*;

var driverClassName = 'oracle.jdbc.OracleDriver';

var jdbcUrl         = 'jdbc:oracle:thin:@localhost:1521:orcl';

var user            = 'chen';

var password        = 'chen';

 

var driver:Driver     = null;
var conn  :Connection = null;
var stmt  :Statement  = null;
var rs    :ResultSet  = null;

var rows  :Number;


try {
 
    var thread      = Thread.currentThread();
    var classLoader = thread.getContextClassLoader();
    var driverClass = classLoader.loadClass(driverClassName);
   
   
    driver = (Driver) driverClass.instantiate();
    DriverManager.registerDriver(driver);
   
   
    try {
      
        conn = DriverManager.getConnection(jdbcUrl, user, password);
        stmt = conn.createStatement();
      
       
       
        rows = stmt.executeUpdate("CREATE TABLE Uuser(id INT NOT NULL ,UName VARCHAR2(50))");
        println("CREATE TABLE rows: {rows}");
       
       
        rows = stmt.executeUpdate("INSERT INTO Uuser VALUES(1, 'chen')");
        println("INSERT rows: {rows}");
       
       
        rows = stmt.executeUpdate("INSERT INTO Uuser VALUES(2, 'zhan')");
        println("INSERT rows: {rows}");
       
       
        rs = stmt.executeQuery("SELECT * FROM Uuser");
        while(rs.next()) {
            println("id: {rs.getInt('id')} task: {rs.getString('UName')}");
        }
    } catch(e:SQLException) {
        e.printStackTrace();
    } finally {
      
        if(null <> rs){rs.close();}
     
        if(null <> stmt) {
            try {
                stmt.close();
            } catch(e:SQLException) {
                e.printStackTrace();
            }
        }
       
        if(null <> conn){conn.close();}
    }
} catch(e:Exception) {
    e.printStackTrace();
}

控制台打印結果如下:

init:
deps-jar:
compile:
Copy libraries to C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/JavaFXApplication1/dist/lib.
To run this application from the command line without Ant, try:
java -jar "C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/JavaFXApplication1/dist/JavaFXApplication1.jar" javafxDemo
jar:
compile thread: Thread[AWT-EventQueue-0,6,main]
compile 0.171
run:
compile thread: Thread[AWT-EventQueue-0,6,main]
compile 0.203
CREATE TABLE rows: 0
INSERT rows: 1
INSERT rows: 1
id: 1 task: chen
id: 2 task: zhan
init: 5.938
BUILD SUCCESSFUL (total time: 17 seconds)


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com