首页 > 数据库技术 > 详细

Java连接sqlite数据库

时间:2014-05-05 23:07:15      阅读:524      评论:0      收藏:0      [点我收藏+]

  在java中使用sqlite,先下载sqlite的jdbc驱动 sqlite-jdbc-3.7.2.jar,在Myeclipse中添加到Build Path中,然后就可以在项目中使用。

bubuko.com,布布扣
package cn.hitech.sqlite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqliteDemo {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("org.sqlite.JDBC");
            try {
                connection = DriverManager.getConnection("jdbc:sqlite:unit.db");
                statement = connection.createStatement();
                // statement.execute("insert into uinit(uid,uname) values(1,‘adam‘");
                resultSet = statement.executeQuery("select * from uinit");
                while (resultSet.next()) {
                    System.out.println("编号:" + resultSet.getString(1));
                    System.out.println("姓名:" + resultSet.getString(2));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                resultSet = null;
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                connection = null;
            }
        }
    }
}
bubuko.com,布布扣

 

 

Java连接sqlite数据库,布布扣,bubuko.com

Java连接sqlite数据库

原文:http://www.cnblogs.com/magics/p/3704705.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!