【J2EE】JDBC vs FMDB

本文对比了JDBC和FMDB的功能与使用场景。JDBC是J2EE规范的一部分,用于实现对各种数据库的标准化访问;FMDB则是iOS平台上的SQLite数据库框架。两者都能实现数据库的链接与操作,但JDBC更通用,而FMDB仅适用于SQLite。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实可比性并不大,但是在实现相同功能上一个是规范,一个是具体实现。其实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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值