免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3151 | 回复: 0
打印 上一主题 下一主题

java连接sqlite [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-26 21:46 |只看该作者 |倒序浏览

                                                                1.去网站http://files.zentus.com/sqlitejdbc/下载sqlitejdbc.根据自已需要,下载linux或Win或Mac版的JDBC
2.
下载后将解压的sqlitejdbc.jar放到/home/jdk/jre/lib/ext(这是我的安装jdk目录,根据自已jdk安装目录修改地
址)下。若是Window下的,除了放sqlitejdbc.jar外还要将解压的sqlite.dll放到X:/jdk/bin下。若是Mac的要将
libsqlitejdbc.jnilib 放到哪呢?应该也是jdk/bin下吧。Mac没用过。
3.如果你只是随便的将sqlitejdbc.jar放在任一目录,并且你又是run in pure-java mode(纯java模式下运行?是这么译吗)请参考http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC
4.如果你已看了第三步的网站,以下可以不用看了,下面的介绍只是复述3里面的而已。
5.转自http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC 的实例经验证可以运行。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sample
{
  public static void main(String[] args) throws ClassNotFoundException
  {
    // load the sqlite-JDBC driver using the current class loader
    Class.forName("org.sqlite.JDBC");
   
    Connection connection = null;
    try
    {
      // create a database connection
      connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
      Statement statement = connection.createStatement();
      statement.setQueryTimeout(30);  // set timeout to 30 sec.
      
      statement.executeUpdate("drop table if exists person");
      statement.executeUpdate("create table person (id integer, name string)");
      statement.executeUpdate("insert into person values(1, 'leo')");
      statement.executeUpdate("insert into person values(2, 'yui')");
      ResultSet rs = statement.executeQuery("select * from person");
      while(rs.next())
      {
        // read the result set
        System.out.println("name = " + rs.getString("name"));
        System.out.println("id = " + rs.getInt("id"));
      }
    }
    catch(SQLException e)
    {
      // if the error message is "out of memory",
      // it probably means no database file is found
      System.err.println(e.getMessage());
    }
    finally
    {
      try
      {
        if(connection != null)
          connection.close();
      }
      catch(SQLException e)
      {
        // connection close failed.
        System.err.println(e);
      }
    }
  }
}6.sqlite中国网站http://www.sqlite.com.cn/,不过这网站不爽,动不动就要求你登陆(如果你没登陆的话)。
               
               
               
               
               
               
               
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/106679/showart_2104934.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP