关闭

【J2EE】JDBC vs FMDB

358人阅读 评论(1) 收藏 举报
分类:

其实可比性并不大,但是在实现相同功能上一个是规范,一个是具体实现。其实JDBC和ODBC更有可比性。

What?

JDBC是J2EE13规范之一,FMDB是ios平台SQLite数据库框架。

相同点:
1)实现对数据库的链接、访问;
2)链接、访问方式非常类似(原理一样,所以第三方链接都一个套路),都是工程里写好sql增删改查,放在框架里调用。

不同点:
1)JDBC是J2EE规范,所有对数据库的控制底层都是用的JDBC,FMDB是框架(API),只能在SQLite数据库上用,不用可以实现;

How?

JDBC建表

//引入包
import java.sql.*;

public class JDBCExample {
   // 1、定义JDBC driver名字和数据库地址、用户名密码等
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/";

   static final String USER = "username";
   static final String PASS = "password";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      // 2、注册JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
      // 3、打开连接
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      // 4、建表
      stmt = conn.createStatement();
      String sql = "CREATE DATABASE STUDENTS";
      stmt.executeUpdate(sql);
   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }finally{
      try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
}
}

JDBC添加

import java.sql.*;

public class JDBCExample {
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";

   static final String USER = "username";
   static final String PASS = "password";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      Class.forName("com.mysql.jdbc.Driver");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      stmt = conn.createStatement();

      String sql = "INSERT INTO Registration " +
                   "VALUES (100, 'Zara', 'Ali', 18)";
      stmt.executeUpdate(sql);

   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }finally{
      try{
         if(stmt!=null)
            conn.close();
      }catch(SQLException se){
      }
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
}
}

JDBC查询

FMDB建表

#pragma  —建立数据库
        NSArray *pathArray = NSSearchPathForDirectoriesInDomains(
        NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *document = [pathArray lastObject];
        NSString *filePath = [document stringByAppendingPathComponent:@"database.db"];
        // NSLog(@"数据库路径:%@", filePath);

FMDB添加

+ (void)insertSynData:(NSArray *)models
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.db open];
    for (int i = 0; i < models.count; i++) {
        PhotoModel *model = [models objectAtIndex:i];
        NSString *sql = [NSString stringWithFormat:@"insert into t_photo(category,myRowid,timeMark,photoName,billID) values('%@','%@','%@','%@','%@')", model.category, model.myRowid, model.timeMark, model.photoName, model.billID];
            [delegate.db executeUpdate:sql];

    }
    [delegate.db close];
}

FMDB查询

#pragma  mark 所有团流入记录 (含每个团的收入总额)
+ (double)getAllIncome:(NSString *)TID
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.db open];
    NSString *sql = [NSString
        stringWithFormat:@"SELECT sum(a.money) income FROM (SELECT * FROM t_BillItem) a, (SELECT * FROM t_Bill) b, (SELECT * FROM t_Order) c WHERE a.BillID = b.ID AND b.orderid = c.id AND b.orderid='%@'and a.selectCategory = '1' GROUP BY c.id   ", TID];
    FMResultSet *result = [delegate.db executeQuery:sql];
    double borrows = 0.0;
    while ([result next]) {
        double borrow = [[result stringForColumn:@"income"] doubleValue];
        borrows = borrow + borrows;
    }
    [delegate.db close];
    return borrows;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37499次
    • 积分:1899
    • 等级:
    • 排名:千里之外
    • 原创:51篇
    • 转载:1篇
    • 译文:0篇
    • 评论:686条
    最新评论