Chinaunix

标题: java连接sqlite [打印本页]

作者: flreey    时间: 2009-11-26 21:46
标题: java连接sqlite

                                                                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




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2