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 需要导入的驱动包

posted @ 2015-01-22 10:34  姑奶奶  阅读(711)  评论(0编辑  收藏  举报