在Java下连接SQLite数据库

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

一、下载SQLite数据库的JDBC:二、将下载到的包解压后得到jar包放到%JAVA_HOME%\lib下,并且将其添加到ClassPath系统环境变量中。一定要保证在类路径ClassPath中有该jar包,并且保证在JAVA库路径JAVALibraryPath中有本地库NativeLibrary(\workspace\Web应用\WebRoot\WEB-INF\lib\下最好也要加入该jar包)。SQLite.JDBCDriver作为JDBC的驱动程序类名。连接JDBC的URL格式为jdbc:sqlite:/path。这里的path为指定到SQLite数据库文件的路径,例如:jdbc:sqlite://dirA/dirB/dbfilejdbc:sqlite://DRIVE:/dirA/dirB/dbfilejdbc:sqlite://COMPUTERNAME/shareA/dirB/dbfile三、下面是使用SQLite的两段代码以供参考:代码段1:1importjava.sql.*;2importorg.sqlite.JDBC;34publicclassSQLiteTest{5publicstaticvoidmain(String[]args){6try{7//TheSQLite(3.3.8)DatabaseFile8//Thisdatabasehasonetable(pmp_countries)with3columns(country_id,country_code,country_name)9//Ithaslike237recordsofallthecountriesIcouldthinkof.10StringfileName=c:/pmp.db;11//DrivertoUse12//(org.sqlite.JDBC);14//CreateConnectionObjecttoSQLiteDatabase15//Ifyouwanttoonlycreateadatabaseinmemory,excludethe+fileName16Connectionconn=DriverManager.getConnection(jdbc:sqlite:+fileName);17//CreateaStatementobjectforthedatabaseconnection,dunnowhatthisstuffdoesthough.18Statementstmt=conn.createStatement();19//Createaresultsetobjectforthestatement20ResultSetrs=stmt.executeQuery(SELECT*FROMpmp_countriesORDERBYcountry_nameASC);21//Iteratetheresultset,printingeachcolumn22//ifthecolumnwasanint,wecoulddors.getInt(columnnamehere)aswell,etc.23while(rs.next()){24Stringid=rs.getString(country_id);//Column125Stringcode=rs.getString(country_code);//Column226Stringname=rs.getString(country_name);//Column327System.out.println(ID:+id+Code:+code+Name:+name);2829}30//Closetheconnection31conn.close();32}33catch(Exceptione){34//Printsomegenericdebuginfo35System.out.println(e.getMessage());36System.out.println(e.toString());37}38}39}

1 / 2
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功