收藏 分享(赏)

在Java下连接SQLite数据库.docx

上传人:HR专家 文档编号:6211702 上传时间:2019-04-02 格式:DOCX 页数:2 大小:16.18KB
下载 相关 举报
在Java下连接SQLite数据库.docx_第1页
第1页 / 共2页
在Java下连接SQLite数据库.docx_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

1、在 Java 下连接 SQLite 数据库一、下载 SQLite 数据库的 JDBC:http:/ jar 包放到%JAVA_HOME%lib 下,并且将其添加到 ClassPath系统环境变量中。一定要保证在类路径 ClassPath 中有该 jar 包,并且保证在 JAVA 库路径JAVA Library Path 中有本地库 Native Library(workspace“Web 应用“WebRootWEB-INFlib下最好也要加入该 jar 包) 。“SQLite.JDBCDriver“ 作为 JDBC 的驱动程序类名。连接 JDBC 的URL 格式为 jdbc:sqlite:/p

2、ath。这里的 path 为指定到 SQLite 数据库文件的路径,例如:jdbc:sqlite:/dirA/dirB/dbfilejdbc:sqlite:/DRIVE:/dirA/dirB/dbfilejdbc:sqlite:/COMPUTERNAME/shareA/dirB/dbfile三、下面是使用 SQLite 的两段代码以供参考:代码段 1:1 import java.sql.*;2 import org.sqlite.JDBC;3 4 public class SQLiteTest 5 public static void main(String args) 6 try 7 / T

3、he SQLite (3.3.8) Database File8 / This database has one table (pmp_countries) with 3 columns (country_id, country_code, country_name)9 / It has like 237 records of all the countries I could think of.10 String fileName = “c:/pmp.db“;11 / Driver to Use12 / http:/ Class.forName(“org.sqlite.JDBC“);14 /

4、 Create Connection Object to SQLite Database15 / If you want to only create a database in memory, exclude the +fileName16 Connection conn = DriverManager.getConnection(“jdbc:sqlite:“+fileName);17 / Create a Statement object for the database connection, dunno what this stuff does though.18 Statement

5、stmt = conn.createStatement();19 / Create a result set object for the statement20 ResultSet rs = stmt.executeQuery(“SELECT * FROM pmp_countries ORDER BY country_name ASC“);21 / Iterate the result set, printing each column22 / if the column was an int, we could do rs.getInt(column name here) as well,

6、 etc.23 while (rs.next() 24 String id = rs.getString(“country_id“); / Column 125 String code = rs.getString(“country_code“); / Column 226 String name = rs.getString(“country_name“); / Column 327 System.out.println(“ID: “+id+“ Code: “+code+“ Name: “+name);28 29 30 / Close the connection31 conn.close();32 33 catch (Exception e) 34 / Print some generic debug info35 System.out.println(e.getMessage();36 System.out.println(e.toString();37 38 39

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 网络科技 > Java

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报