Java连接sqlite数据库

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

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;
            }
        }
    }
}

 

 

Java连接sqlite数据库,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。