Java连接sqlite

1.去网站http://files.zentus.com/sqlitejdbc/下载sqlitejdbc.根据自已需要,下载sqlitejdbc.(sqlitejdbc有两个版本,一种是pure的一种是native的。native的速度快。它放在**-bin.tgz里。我下的就是这种sqlitejdbc-v056-bin.tgz

2. 下载后解压sqlitejdbc-v056-bin.tg ,有以下几个文件: libsqlitejdbc.so,sqlitejdbc.dll,
libsqlitejdbc.jnilib,sqlitejdbc-v056-native.jar。
其中libsqlitejdbc.so是linux下的动态链接库,sqlitejdbc.dll是windows下的动态链接库, libsqlitejdbc.jnilib是Mac下的动态链接库。

3.根据自已的操作系统将sqlitejdbc-v056-native.jar与对应动态链接库放到jdk/jre/lib/ext里。(我的是linux,则将sqlitejdbc-v056-native.jar 与libsqlitejdbc.so 放到jdk/jre/lib/ext中)。

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/,不过这网站不爽,动不动就要求你登陆(如果你没登陆的话)。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
连接SQLite数据库,需要使用Java中的JDBC驱动程序。以下是连接SQLite数据库的步骤: 1.下载SQLite JDBC驱动程序:从以下网址下载SQLite JDBC驱动程序:https://bitbucket.org/xerial/sqlite-jdbc/downloads/ 2.将驱动程序添加到项目中:将下载的JAR文件添加到您的Java项目的类路径中。 3.建立连接:使用以下代码在Java中建立与SQLite数据库的连接: ``` import java.sql.*; public class SQLiteJDBC { public static void main( String args[] ) { Connection c = null; try { Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:test.db"); } catch ( Exception e ) { System.err.println( e.getClass().getName() + ": " + e.getMessage() ); System.exit(0); } System.out.println("Opened database successfully"); } } ``` 以上代码使用了SQLite JDBC驱动程序中的“org.sqlite.JDBC”类来加载驱动程序并建立连接连接字符串“jdbc:sqlite:test.db”指定要连接的数据库文件的路径。 4.执行SQL语句:使用以下代码执行SQL语句并获取结果: ``` Statement stmt = null; ResultSet rs = null; try { stmt = c.createStatement(); rs = stmt.executeQuery( "SELECT * FROM mytable;" ); while ( rs.next() ) { int id = rs.getInt("id"); String name = rs.getString("name"); float salary = rs.getFloat("salary"); } } catch ( Exception e ) { System.err.println( e.getClass().getName() + ": " + e.getMessage() ); System.exit(0); } finally { rs.close(); stmt.close(); c.close(); } ``` 以上代码使用了JDBC的Statement对象来执行SQL查询,并使用ResultSet对象来获取结果。请注意,必须在使用完ResultSet和Statement对象后关闭它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值