zoukankan      html  css  js  c++  java
  • jdbc 对sqlite的基本操作

    1.向数据库中创建表

    public void addTable( String dbpath)
      {
    
    //创建表单的sql语句
      
      String createtablesql= " CREATE TABLE T_AFAF_AREA(PID TEXT,STDNAME TEXT) ";
      //这里的TEXT 类型根据需要改变
        try{
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:"+dbpath);   
            Statement stmt = conn.createStatement();
            //巡检表
      stmt.executeUpdate(createtablesql);
      }catch(Exception e)
        {
          e.printStackTrace();
        }
      }

    2.从 .db 文件中删除表

    这里只贴出来语句其他都一样

    //判断巡检表是否存在 存在 则删除
          String deletetablesql = " drop table  if exists T_EXAMINEDATA  ";

    3 . 从 .db 文件中读取信息

    public List <TStreetsExamine> queryTStreetsExamine(String dbpath)
      {
        List <TStreetsExamine> list = new ArrayList<TStreetsExamine>();
        try{
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:"+dbpath);   
          Statement stmt = conn.createStatement();
          ResultSet res = stmt.executeQuery(" select * from T_STREETS_SURVEY    ");
          while(res.next())
          {
            //公共部分
            String pid =	res.getString("PID");
      //......................................这里省略n行					 list.add(te);
          }
          res.close();
          stmt.close();
          res.close();
        }
        catch(Exception e)
        { 
          e.printStackTrace();
        }
        
        return list;
        
      }

    4.向.db文件中写入数据

    public void addStreets(TStreets te ,String dbpath)
      {
        try{
    
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:"+"E://toponymsurvey11122.db");   
           Statement stmt = conn.createStatement();
           PreparedStatement pstmt = null ;	// 数据库操作
                  String sql = " INSERT INTO T_STREETS(PID ,STDENAME) VALUES (?,?)  " ;
      pstmt = conn.prepareStatement(sql) ;
                       pstmt.setString(1,te.getPid()) ;
           pstmt.setString(2,te.getStdname()) ;
                 pstmt.executeUpdate() ;	// 执行更新
           pstmt.close() ;
           stmt.close();
           conn.close();
        }catch(Exception e)
        {
          e.printStackTrace();
          
        }
      }

    5 需要导入的驱动包

  • 相关阅读:
    List装form
    《Java设计模式》之调停者模式(Mediator)
    android 4.0 禁用系统home键
    最大权二分匹配
    hdu 3667 /2010哈尔滨赛区H题 费用与流量为非线性关系/费用流
    【python】filter()
    【python】linux将python改为默认3.4版本
    【linux】VMware12.0安装
    【python】lxml-The E-factory
    【xml】python的lxml库使用
  • 原文地址:https://www.cnblogs.com/bokejiayuan/p/4240984.html
Copyright © 2011-2022 走看看